Framer John had a piece of wood that he wanted to use to build a fence, and he had to ask Framer Don for help without a saw. FD request, FJ each cut open a piece of wood, will give the length of the plank of money. FJ want to spend the least, ask you for help.
Idea: Definitely a replica of the merged fruit! It's OK to take the plank as a merged plank. Be careful of the range of L, N, the final ans with int is not fit, to use a long long.
The code is as follows:
#include <iostream>#include <cstdio>#include <queue>using namespace STD;intn;priority_queue<Long Long> q;voidInit () {scanf("%d", &n);inttmp for(intI=1; i<=n;++i) {scanf("%d", &tmp); Q.push (-TMP); }}voidWork () {intans=0; while(!q.empty ()) {intA=q.top (); Q.pop ();if(Q.empty ()) {printf("%lld",-ans);return; }intB=q.top (); Q.pop (); ans+= (A+B); Q.push (A+B); }}intMain () {init (); Work ();return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ3264 Balanced Lineup