結果

問題 No.2840 RGB Plates
ユーザー ococonomy1ococonomy1
提出日時 2024-08-09 22:35:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 2,774 bytes
コンパイル時間 2,556 ms
コンパイル使用メモリ 216,772 KB
実行使用メモリ 61,952 KB
最終ジャッジ日時 2024-08-18 00:56:46
合計ジャッジ時間 4,631 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 31 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 83 ms
50,304 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 4 ms
6,944 KB
testcase_08 AC 6 ms
6,940 KB
testcase_09 AC 14 ms
6,944 KB
testcase_10 AC 90 ms
61,952 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 85 ms
52,224 KB
testcase_13 AC 77 ms
44,928 KB
testcase_14 AC 85 ms
51,712 KB
testcase_15 AC 84 ms
50,432 KB
testcase_16 AC 81 ms
48,384 KB
testcase_17 AC 84 ms
51,456 KB
testcase_18 AC 47 ms
18,048 KB
testcase_19 AC 54 ms
25,216 KB
testcase_20 AC 44 ms
15,488 KB
testcase_21 AC 45 ms
16,896 KB
testcase_22 AC 36 ms
9,216 KB
testcase_23 AC 43 ms
13,184 KB
testcase_24 AC 41 ms
12,160 KB
testcase_25 AC 43 ms
14,464 KB
testcase_26 AC 33 ms
6,940 KB
testcase_27 AC 38 ms
9,088 KB
testcase_28 AC 40 ms
8,832 KB
testcase_29 AC 33 ms
6,940 KB
testcase_30 AC 49 ms
20,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pli = pair<ll,int>;
#define TEST cerr << "TEST" << endl
#define AMARI 998244353
//#define AMARI 1000000007
#define el '\n'
#define El '\n'

#define MULTI_TEST_CASE false
void solve(void){
    //問題を見たらまず「この問題設定から言えること」をいっぱい言う
    //一個回答に繋がりそうな解法が見えても、実装や細かい詰めに時間がかかりそうなら別の方針を考えてみる
    //添え字回りで面倒になりそうなときは楽になる言い換えを実装の前にじっくり考える
    //ある程度考察しても全然取っ掛かりが見えないときは実験をしてみる
    //よりシンプルな問題に言い換えられたら、言い換えた先の問題を自然言語ではっきりと書く
    int n;
    cin >> n;
    vector<int> a(n);
    int sum = 0;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        sum += a[i];
    }
    sort(a.begin(),a.end());
    

    if(n < 20){
        map<int,int> mp;
        for(int S = 0; S < (1 << n); S++){
            int temp = 0;
            for(int i = 0; i < n; i++){
                if((S & (1 << i)))temp += a[i];
            }
            mp[temp]++;
        }

        for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++){
            if(it->second >= 2 && sum - it->first * 2 != 0){
                cout << sum - it->first * 2 << el;
                return;
            }
        }
        cout << -1 << el;
        return;
    }
    
    map<int,int> mp;
    for(int S = 0; S < (1 << 18); S++){
        int temp = 0;
        for(int i = 0; i < 18; i++){
            if((S & (1 << i)))temp += a[i];
        }
        mp[temp]++;
    }

    int m = -1;

    for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it++){
        if(it->second >= 2){
            m = it->first;
            break;
        }
    }

    assert(m != -1);

    vector<vector<int>> dp(n + 1,vector<int>(m + 1,0));
    dp[0][0] = 1;
    for(int i = 0; i < n; i++){
        for(int j = 0; j <= m; j++){
            dp[i + 1][j] += dp[i][j];
            if(j + a[i] <= m)dp[i + 1][j + a[i]] += dp[i][j];
        }
    }

    for(int i = 0; i <= m; i++){
        if(dp[n][i] >= 2){
            cout << sum - 2 * i << el;
            return;
        }
    }
    //assert(0);
    return;
}

void calc(void){
    return;
}

signed main(void){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    calc();
    int t = 1;
    if(MULTI_TEST_CASE)cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
0