提交时间:2024-08-19 23:09:23
运行 ID: 168152
#include <bits/stdc++.h> using namespace std; int n,sum,ans; int a[20001],b[20001],t[20001]; int main(){ scanf("%d",&n); memset(a,127,sizeof(a)); memset(b,127,sizeof(b)); for (int i=1,k;i<=n;i++){ scanf("%d",&k); t[k]++; } for (int i=1,k=0;i<=20000;i++) for(;t[i];t[i]--) a[++k]=i; int k1=1,k2=1; for (int i=1,k=0; i<n; i++) { sum=a[k1]<b[k2]?a[k1++]:b[k2++]; sum+=a[k1]<b[k2]?a[k1++]:b[k2++]; b[++k]=sum; ans+=sum; } printf("%d\n",ans); return 0; }