50分求助
  • 板块P1651 塔
  • 楼主沉鸣cmh
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/5/11 22:49
  • 上次更新2023/10/28 01:39:00
查看原帖
50分求助
334041
沉鸣cmh楼主2022/5/11 22:49

WA了5个点

#include<bits/stdc++.h>
using namespace std;
int n,a[55],f[500005],g[500005],maxn;
int main(){
	ios::sync_with_stdio(false);memset(f,128,sizeof(f)),memset(g,128,sizeof(g)),f[0]=g[0]=0;
	cin>>n;for(int i=1;i<=n;i++){cin>>a[i];
		for(int j=0;j<=maxn;j++){
			f[j+a[i]]=max(g[j+a[i]],g[j]);
			if(a[i]>=j)f[a[i]-j]=max(g[a[i]-j]+j,g[j]);
			else f[j-a[i]]=max(g[j-a[i]]+a[i],g[j]);
		}maxn+=a[i];
		for(int j=0;j<=maxn;j++)g[j]=f[j];
	}
	cout<<(f[0]==0?-1:f[0]);
	return 0;
} 
2022/5/11 22:49
加载中...