21. 帆布画

Description

After last year's success, Samuel W. E. R. Craft's reputation has grown and he has now funded various projects. His latest idea involves creating an array of canvases with colored patterns without repeating colors.

Samuel bought a set of white canvasses of varying sizes. Since painting them manually would take too much time, he devised a huge machine to automate the painting process. The painting process works as follows:

  1. Assemble all canvasses in a line in the machine’s conveyor belt, disposed in some chosen order.

  1. Pick a color C and a number F (which should be less than the number of color C canvasses).

  1. Going from left to right, all canvasses with color C are painted. The first F color C canvasses are painted with a new color X and the remaining color C canvasses are painted with a new color Y . Colors X and Y are selected by the machine, are distinct, and are different from any color used previously. The amount of ink spent in this step is equal to the sum of the sizes of the painted canvasses.

  2. Repeat 2) and 3) until all canvasses have distinct colors.

Consider for example that Samuel bought four canvasses of sizes 3, 5, 5 and 7. The following picture shows 2 different options for painting them.

Given the sizes of the canvasses Samuel bought, can you help Samuel finding the minimum amount of ink the machine needs to spend in order to have all canvasses with different colors?

Input

The first line consists of a single integer T, the number of test cases. Each test case is composed by two lines. The first line consists of a single integer N representing the number of canvasses. The next line contains N space separated integers representing the sizes of the canvasses.

Output

The output contains T lines, one for each test case: the minimum amount of ink the machine needs in order to have all canvasses with different colors.

Note

# include<stdio.h>
# include<stdlib.h>
long long h[100010];
long long n, ans;
void swap(long long  x, long long y){
    long long t;
    t = h[x];
    h[x] = h[y];
    h[y] = t;
}
void siftdown(int i){
    long long t, flag = 0;
    while(i*2 <= n && flag == 0){
        if(h[i] > h[i*2])
            t = i * 2;
        else
            t = i;
        if(i*2+1 <= n){
            if(h[t] > h[i*2+1])
                t = i * 2 + 1;
        }
        if(t!=i){
            swap(t, i);
            i = t;
        }
        else
            flag = 1;
    }
}
void create(){
    long long i;
    for (i = n / 2; i >= 1;i--){
        siftdown(i);
    }
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        ans = 0;
        long long i, num;
        scanf("%lld", &num);
        for (i = 1; i <= num; i++)
            scanf("%lld", &h[i]);
        if(num == 1){
            printf("0\n");
            continue;
        }
        n = num;
        create();
        while(n > 2){
            if(h[2] < h[3]){
                ans = ans + h[1] + h[2];
                h[2] = h[1] + h[2];
                swap(1, n);
                n--;
                siftdown(2);
                siftdown(1);
            }
            else{
                ans = ans + h[1] + h[3];
                h[3] = h[1] + h[3];
                swap(1, n);
                n--;
                siftdown(3);
                siftdown(1);
            }
        }
        ans += h[1] + h[2];
        printf("%lld\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41207175/article/details/85253027
21.