求调
  • 板块P1651 塔
  • 楼主gyf_QAQ
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/6/17 13:15
  • 上次更新2025/6/18 13:16:30
查看原帖
求调
1727930
gyf_QAQ楼主2025/6/17 13:15

求调QAQ

#include <bits/stdc++.h>
using namespace std;
const int max_=5e5+5;
int n,a[max_],f[max_],rp[max_],sum;
signed main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr); 
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        sum+=a[i];
    }
    for(int i=1;i<=sum;i++){
        rp[i]=INT_MIN;   
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<=sum;j++){
            f[j]=max(max(rp[abs(j-a[i])],rp[j+a[i]])+a[i],rp[j]);
        }
        for(int j=0;j<=sum;j++){
            rp[j]=f[j];
        }
    }
    if(!(sum-rp[0]/2)){
        cout<<-1<<'\n';
        exit(0);
    }
    cout<<sum-rp[0]/2<<'\n';
    return 0;
}
2025/6/17 13:15
加载中...