Fence Repair

这里写图片描述

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

#define MAX_N 2000

typedef long long ll;
int N,L[MAX_N];

void solve()
{
    ll ans=0;

    while(N>1)
    {
        int mii1=0,mii2=1;
        if(L[mii1]>L[mii2])
            swap(mii1,mii2);
        for(int i=2;i<N;i++)
        {
            if(L[i]<L[mii1])
            {
                mii2=mii1;
                mii1=i;
            }
            else if(L[i]<L[mii2])
            {
                mii2=i;
            }
        }

        int t=L[mii1]+L[mii2];
        ans+=t;
        if(mii1==N-1)
            swap(mii1,mii2);
        L[mii1]=t;
        L[mii2]=L[N-1];
        N--;
    }
    printf("%lld\n",ans);
}

int main()
{
    printf("N:\n");
    scanf("%d",&N);

    for(int i=0;i<N;i++)
    {
        printf("L[%d]:\n",i);
        scanf("%d",&L[i]);
    }

    solve();
    return 0;
}
posted @ 2015-05-19 15:55  nomasp  阅读(160)  评论(0编辑  收藏  举报