POJ 3253 Fence Repair
每次取最小两个合并
\(n^2\)可过
#include <cstdio>
const int MAXN=20011;
int read(){
int x=0, f=1;char ch=getchar();
while(ch<'0' || ch>'9'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0' && ch<='9'){x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
int N;
int Num[MAXN];
long long Ans=0LL;
int Minat(int l, int r){
int ret=r;
for(int i=l;i<r;++i){
if(Num[i]<Num[ret]) ret=i;
}
return ret;
}
int main(){
N=read();
for(int i=1;i<=N;++i) Num[i]=read();
for(int i=1, j, t;i<N;++i){
j=Minat(i, N);
t=Num[j];Num[j]=Num[i];Num[i]=t;
j=Minat(i+1, N);
t=Num[j];Num[j]=Num[i+1];Num[i+1]=t;
Num[i+1]+=Num[i];
Ans+=(long long)(Num[i+1]);
}
printf("%lld\n", Ans);
return 0;
}