天天看點

P1090 [NOIP2004 提高組] 合并果子 / [USACO06NOV] Fence Repair G

​​傳送門​​

思路:

#include<bits/stdc++.h>
using namespace std;
# define ll long long
priority_queue<int,vector<int>,greater<int> >a;
int main()
{
  int n;
  scanf("%d",&n);
  for(int i = 1; i <= n; i++)
  {
    int b;
    scanf("%d",&b);
    a.push(b);
  }
  ll ans = 0;
  while(a.size() != 1)
  {
    int x = a.top();
    a.pop();
    int y = a.top();
    a.pop();
    x = x + y;
    a.push(x);
    ans += x;
  }
  printf("%lld\n",ans);
}      

繼續閱讀