結果

問題 No.2840 RGB Plates
ユーザー HIcoder
提出日時 2024-08-31 16:34:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 181 ms / 2,000 ms
コード長 1,276 bytes
コンパイル時間 1,236 ms
コンパイル使用メモリ 118,132 KB
最終ジャッジ日時 2025-02-24 03:32:26
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
N=1000のとき
異なる2つを選ぶ方法は499*500通り

*/
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cassert>
#include<random>
#include<set>
#include<map>
#include<cassert>
#include<unordered_map>
#include<bitset>
#include<numeric>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=1<<30;
const ll INF=1LL<<62;
typedef pair<int,ll> P;
typedef pair<int,P> PP; 
const ll MOD=998244353;

int main(){
    int N;
    cin>>N;
    ll totA=0;
    vector<int> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
        totA+=A[i];
    }
    int K=5000;
    if(N<=20){
        K=5000*N;
    }else if(N<=200){
        K=5000*10;
    }else{
        //N>200
        K=5000*2;
    }
    vector<vector<int>> dp(N+1,vector<int>(K+1,0));

    dp[0][0]=1;//[0,i),和が0
    for(int i=0;i<N;i++){
        for(int j=0;j<=K;j++){
            if(dp[i][j]>0){
                dp[i+1][j]+=dp[i][j];
                if(j+A[i]<=K){
                    dp[i+1][j+A[i]]+=dp[i][j];
                }
            }
        }
    }

    int t=-1;
    for(int j=1;j<=min(1LL*K,totA/2-1);j++){
        if(dp[N][j]>=2){

            cout<<totA-2*j<<endl;
            return 0;
        }
    }

    cout<<-1<<endl;
    return 0;

    

}
0