結果

問題 No.1606 Stuffed Animals Keeper
ユーザー ktr216ktr216
提出日時 2021-07-16 22:53:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 3,000 ms
コード長 1,127 bytes
コンパイル時間 1,569 ms
コンパイル使用メモリ 170,540 KB
実行使用メモリ 35,888 KB
最終ジャッジ日時 2023-09-20 15:19:03
合計ジャッジ時間 3,815 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 18 ms
16,796 KB
testcase_04 AC 21 ms
20,048 KB
testcase_05 AC 5 ms
5,984 KB
testcase_06 AC 9 ms
8,644 KB
testcase_07 AC 15 ms
14,396 KB
testcase_08 AC 14 ms
13,340 KB
testcase_09 AC 7 ms
7,300 KB
testcase_10 AC 4 ms
5,456 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 8 ms
8,696 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 27 ms
24,804 KB
testcase_17 AC 27 ms
25,048 KB
testcase_18 AC 27 ms
25,060 KB
testcase_19 AC 26 ms
24,928 KB
testcase_20 AC 26 ms
24,720 KB
testcase_21 AC 6 ms
6,396 KB
testcase_22 AC 6 ms
5,952 KB
testcase_23 AC 6 ms
6,000 KB
testcase_24 AC 6 ms
6,720 KB
testcase_25 AC 6 ms
6,128 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 4 ms
4,376 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 4 ms
4,376 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 2 ms
4,384 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 37 ms
35,812 KB
testcase_44 AC 36 ms
35,812 KB
testcase_45 AC 36 ms
35,776 KB
testcase_46 AC 36 ms
35,876 KB
testcase_47 AC 36 ms
35,888 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l); i < (r); i++)
using namespace std;

typedef long long ll;

int main() {
    int N;
    cin >> N;
    vector<int> A(N, 0), B(1, 0), C(3, 0), D(1, 0);
    rep(i, 0, N) {
        cin >> A[i];
        C[A[i]]++;
        if (A[i] == 2) {
            if (B.back() > 0) {
                B.push_back(0);
                D.push_back(0);
            }
        } else {
            B.back()++;
            D.back() += A[i];
        }
    }
    if (B.back() == 0) {
        B.pop_back();
        D.pop_back();
    }
    int M = B.size();
    //rep(i, 0, M) cout << B[i] << " "; cout << endl;
    //rep(i, 0, M) cout << D[i] << " "; cout << endl;
    vector<vector<int>> dp(M + 1, vector<int>(N + 1, 1e9));
    dp[0][0] = 0;
    rep(i, 0, M) {
        rep(j, 0, N) {
            if (dp[i][j] == 1e9) continue;
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + D[i]);
            dp[i + 1][j + B[i]] = min(dp[i + 1][j + B[i]], dp[i][j] + B[i] - D[i]);
        }
    }
    int ans = dp[M][C[1]];
    if (ans == 1e9) ans = -1;
    else ans /= 2;
    cout << ans << endl;
}
0