3253:Fence Repair
keyword
greedy C++
概要
プログラミングコンテストチャレンジブックを参考にして解いた。
int main(){ int ns[20001]; int i, n, accum, x; ll ans = 0; scanf("%d",&n); priority_queue<int, vector<int>, greater<int> > Q; REP(i,n){ scanf("%d",&x); Q.push(x); } while(SZ(Q) > 1){ int a = Q.top(); Q.pop(); int b = Q.top(); Q.pop(); Q.push(a+b); ans += a+b; } printf("%lld\n", ans); return 0; }