POJ 3253 之二

#include <stdio.h>

#define N 20000

int count;
int planks[N];


long long get_minimum(void)
{
    long long cost = 0;

    while( count > 1)
    {
        int m1 = 0, m2 = 1;
    
        if(planks[m2] < planks[m1])
        {
            int tmp = m1;
            m1 = m2;
            m2 = tmp;
        }

        int i;
        for(i = 2; i < count; i++)
        {
            if(planks[i] < planks[m1])
             {
                 m2 = m1;
                 m1 = i;
             }
             else if( planks[i] < planks[m2])
             {
                 m2 = i;
             }
        }

        int t =  planks[m1] + planks[m2];
        cost+= t;    
        if( m1 == count-1)
        {
            int tmp = m1;
            m1 = m2;
            m2 = tmp;
        }

        planks[m1] = t;
        planks[m2] = planks[count-1];
        count--;
    }

    return cost;
}

int main(void)
{
    //freopen("1.txt", "r", stdin);
    scanf("%d", &count);

    int i;
    for(i = 0; i < count; i++)
    {
        scanf("%d", planks + i);
    }

    printf("%lld\n", get_minimum());

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值