有一块木块,要切n-1刀,最终变成n块,告诉你最终每一块的长度,每次切的时候需要加上当前切的那一块的长度,求一种切割方案使得和最小。这题需要逆推,最后一刀肯定是长度最小的2块,答案加上这两块的长度,然后回退,把这两块看成一块,得到n-1块,一直这样下去直到最终只有1块为止。
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
int a[20010];
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
priority_queue<LL, vector<LL>, greater<LL> >Q;
int n;
LL ans = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
Q.push(a[i]);
}
while(Q.size() > 1)
{
LL tmp = Q.top();
Q.pop();
tmp += Q.top();
Q.pop();
ans += tmp;
Q.push(tmp);
}
printf("%lld\n", ans);
return 0;
}