实在没看懂,数组大小开的应该是够的吧,subtask3和4全WA,相当于没加强的数据都能对,加强了的都错了
#include<bits/stdc++.h>
#define maxa 100010
#define N 10000010
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
inline ll read(){
char c=getchar();ll ans=0;
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')ans=ans*10+c-'0',c=getchar();
return ans;
}
ll n,u,mx,a[maxa];
ll q[N<<2],p[N<<2],lq=1,rq,lp=1,rp;
ll sq,sp,x,op,ans;
signed main(){
n=read();
for(int i=1;i<=n;i++){
u=read();
a[u]++;
mx=max(mx,u);
}
for(int i=1;i<=mx;i++){
while(a[i]--){
q[++rq]=i;
// printf("%lld ",q[rq]);
}
}
sq=n;
while(sq+sp>1){
x=inf,op=0;
if(sq>=2){
if(q[lq]+q[lq+1]<x){
x=q[lq]+q[lq+1];
op=1;
}
}
if(sp>=2){
if(p[lp]+p[lp+1]<x){
x=p[lp]+p[lp+1];
op=2;
}
}
if(sq&&sp){
if(q[lq]+p[lp]<x){
x=q[lq]+p[lp];
op=3;
}
}
if(op==1){
p[++rp]=q[lq]+q[lq+1];
lq+=2,sq-=2,sp++;
}else if(op==2){
p[++rp]=p[lp]+p[lp+1];
lp+=2,sp--;
}else{
p[++rp]=q[lq]+p[lp];
lq++,lp++,sq--;
}
ans+=p[rp];
}
printf("%lld\n",ans);
return 0;
}