首页 > 代码库 > UVa10954

UVa10954

Add All

题意:最优二叉树(priority_queue实现)

#include <stdio.h>#include <string.h>#include <queue>using namespace std;int main(int argc, char *argv[]){    int n, i, j, a, sum[6000];    priority_queue <int> A;    while(scanf("%d", &n) != EOF && n)    {        for(i = 1; i <= n; i++)        {            scanf("%d", &a);            a = -a;            A.push(a);        }        memset(sum, 0, sizeof(sum));        i = 1;        for(j = 1; j <= n - 1; j++)        {            sum[i] += A.top();            A.pop();            sum[i] += A.top();            A.pop();            A.push(sum[i]);            i++;        }        i--;        int max = 0;        for(int j = 1; j <= i; j++)            max += sum[j];        printf("%d\n", -max);        A.pop();    }    return 0;}