結果

問題 No.2840 RGB Plates
ユーザー ShirotsumeShirotsume
提出日時 2024-08-09 23:19:09
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,613 ms / 2,000 ms
コード長 1,395 bytes
コンパイル時間 1,150 ms
コンパイル使用メモリ 103,564 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-08-18 01:01:49
合計ジャッジ時間 18,274 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,816 KB
testcase_01 AC 4 ms
5,376 KB
testcase_02 AC 8 ms
5,376 KB
testcase_03 AC 260 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 1,613 ms
5,376 KB
testcase_06 AC 8 ms
5,376 KB
testcase_07 AC 9 ms
5,376 KB
testcase_08 AC 9 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 1,307 ms
5,376 KB
testcase_11 AC 8 ms
5,376 KB
testcase_12 AC 1,305 ms
5,376 KB
testcase_13 AC 809 ms
5,376 KB
testcase_14 AC 1,174 ms
5,376 KB
testcase_15 AC 1,069 ms
5,376 KB
testcase_16 AC 953 ms
5,376 KB
testcase_17 AC 1,148 ms
5,376 KB
testcase_18 AC 1,610 ms
5,376 KB
testcase_19 AC 1,598 ms
5,376 KB
testcase_20 AC 1,593 ms
6,940 KB
testcase_21 AC 1,603 ms
6,944 KB
testcase_22 AC 1,589 ms
6,944 KB
testcase_23 AC 209 ms
6,944 KB
testcase_24 AC 174 ms
6,944 KB
testcase_25 AC 250 ms
6,944 KB
testcase_26 AC 983 ms
6,944 KB
testcase_27 AC 717 ms
6,944 KB
testcase_28 AC 741 ms
6,940 KB
testcase_29 AC 958 ms
6,944 KB
testcase_30 AC 1,520 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;

const long long inf = LLONG_MAX / 2;

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a.begin(), a.end());

    vector<long long> dp0(40001, -inf);
    vector<long long> dp1(40001, -inf);

    dp0[0] = 0;

    for (int i = 0; i < n; i++) {
        vector<long long> new_dp0 = dp0;
        vector<long long> new_dp1 = dp1;

        for (int j = 0; j <= 40000; j++) {
            if (dp0[j] != -inf) {
                new_dp0[j] = max(new_dp0[j], dp0[j] + a[i]);
            }

            if (dp1[j] != -inf) {
                new_dp1[j] = max(new_dp1[j], dp1[j] + a[i]);
                new_dp1[abs(j - a[i])] = max(new_dp1[abs(j - a[i])], dp1[j]);
            }

            if (dp0[j] != -inf) {
                new_dp1[abs(j - a[i])] = max(new_dp1[abs(j - a[i])], dp0[j]);
            }

            if (j + a[i] <= 40000) {
                if (dp1[j] != -inf) {
                    new_dp1[j + a[i]] = max(new_dp1[j + a[i]], dp1[j]);
                }
                if (dp0[j] != -inf) {
                    new_dp1[j + a[i]] = max(new_dp1[j + a[i]], dp0[j]);
                }
            }
        }

        dp0 = new_dp0;
        dp1 = new_dp1;
    }

    cout << (dp1[0] <= 0 ? -1 : dp1[0]) << endl;

    return 0;
}
0