結果

問題 No.2840 RGB Plates
ユーザー SnowBeenDiding
提出日時 2024-08-10 00:07:52
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 1,049 bytes
コンパイル時間 5,458 ms
コンパイル使用メモリ 308,040 KB
実行使用メモリ 120,968 KB
最終ジャッジ日時 2024-08-18 01:02:21
合計ジャッジ時間 6,688 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
using namespace atcoder;
using namespace std;

typedef long long ll;

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, 0, n) cin >> a[i];
    int max_t, max_a = *max_element(a.begin(), a.end());
    int sum_a = accumulate(a.begin(), a.end(), 0);
    if (n <= 20)
        max_t = max_a * n + 10;
    else if (n <= 200)
        max_t = max_a * 10 + 10;
    else
        max_t = max_a * 2 + 10;
    int dp[n + 1][max_t + 1];
    rep(i, 0, n + 1) rep(j, 0, max_t + 1) dp[i][j] = 0;
    dp[0][0] = 1;
    rep(i, 0, n) {
        for (int j = max_t; j >= 0; j--) {
            dp[i + 1][j] += dp[i][j];
            if (j + a[i] <= max_t)
                dp[i + 1][j + a[i]] += dp[i][j];
        }
    }
    int mn = 1e9;
    rep(i, 0, max_t + 1) {
        if (dp[n][i] >= 2) {
            mn = i;
            break;
        }
    }
    int ans = sum_a - mn - mn;
    if (ans <= 0)
        ans = -1;
    cout << ans << endl;
}
0