为什么用堆过不了,谁能告诉我原因 ?

洛谷 P1880 [NOI1995] 石子合并

题目链接

我是用大根堆和小根堆来做的,只过了 2 个点,中间三个点 WA 了。

谁能告诉我为什么 ?

本人代码 :

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
priority_queue<LL, vector<LL>, greater<LL> > q;
priority_queue<LL> p;
//struct node
//{
//    int num;
//    bool operator < (const node&a) const
//    {
//        return num < a.num;
//    }
//};

LL n;

int main()
{
    scanf("%lld", &n);
    for(int i = 1; i <= n; i ++)
    {
        LL k;
        scanf("%lld", &k);
        q.push(k);
        p.push(k);
    }
    LL ans = 0, t = 0;
    LL sum = 0, e = 0;
    while(q.size() >= 2)
    {
        ans += q.top();
        t += q.top();
        q.pop();
        ans += q.top();
        t += q.top();
        q.pop();
        q.push(t);
        t = 0;
    }
    while(p.size() >= 2)
    {
        sum += p.top();
        e += p.top();
        p.pop();
        sum += p.top();
        e += p.top();
        p.pop();
        p.push(e);
        e = 0;
    }
    printf("%lld\n%lld\n", ans, sum); 
    return 0;
}

只能合并相邻的两堆啊。。。这个题要用dp