RT,TLE我能理解,但为什么会WA啊QAQ.
Code:
#include<bits/stdc++.h>
using namespace std;
#define N 121
#define re register
int cnt,n,a[N],b[N],sum,mina,nxt[N];
char vac[N];
bool cmp(int a,int b){return a>b;}
void DFS(int s,int old)
{
re int kick=0;
re int l=1,r=cnt,lef=0;
while(l<=r)
{
re int mid=l+r>>1;
if(a[mid]+old>s) l=mid+1;
else r=mid-1,lef=mid;
}
if(!lef)return;
for(re int i=lef;i<=cnt;++i)
{
if(vac[i]) continue;kick=1;
if(old+a[i]!=s) vac[i]=1,DFS(s,old+a[i]),vac[i]=0;
else vac[i]=1,DFS(s,0),vac[i]=0;
i=nxt[i];
}
if(!kick&&!old) return printf("%d",s),exit(0);
return;
}
int main()
{
cin>>n;
for(re int i=1;i<=n;++i) scanf("%d",b+i);
for(re int i=1;i<=n;++i)
{
if(b[i]<=50)
{
a[++cnt]=b[i],sum+=b[i],mina=max(mina,b[i]);
nxt[cnt]=cnt;
}
}
sort(a+1,a+cnt+1,cmp);
for(int i=1;i<=cnt;++i)
{
for(int j=i+1;j<=cnt;++j)
if(a[i]!=a[j]) {nxt[i]=j-1;break;}
}
for(re int i=mina;i<=sum/2;++i)
if(sum%i==0){DFS(i,0);}
cout<<sum;
return 0;
}