結果

問題 No.2026 Yet Another Knapsack Problem
ユーザー suisensuisen
提出日時 2022-07-30 00:29:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 315 ms / 10,000 ms
コード長 1,082 bytes
コンパイル時間 742 ms
コンパイル使用メモリ 80,876 KB
実行使用メモリ 52,376 KB
最終ジャッジ日時 2023-09-27 00:52:40
合計ジャッジ時間 4,727 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 7 ms
4,680 KB
testcase_29 AC 7 ms
4,828 KB
testcase_30 AC 8 ms
4,956 KB
testcase_31 AC 8 ms
4,936 KB
testcase_32 AC 6 ms
4,376 KB
testcase_33 AC 9 ms
4,940 KB
testcase_34 AC 7 ms
4,940 KB
testcase_35 AC 6 ms
4,424 KB
testcase_36 AC 7 ms
4,560 KB
testcase_37 AC 7 ms
4,624 KB
testcase_38 AC 300 ms
52,164 KB
testcase_39 AC 289 ms
52,252 KB
testcase_40 AC 296 ms
52,128 KB
testcase_41 AC 313 ms
52,200 KB
testcase_42 AC 314 ms
52,204 KB
testcase_43 AC 315 ms
52,376 KB
testcase_44 AC 313 ms
52,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <limits>
#include <vector>

constexpr long long inf = std::numeric_limits<long long>::max() / 2;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;

    std::vector<int> c(n + 1);
    std::vector<long long> v(n + 1);

    for (int i = 1; i <= n; ++i) {
        std::cin >> c[i] >> v[i];
    }

    std::vector dp(n + 1, std::vector<long long>(n + 1, -inf));
    std::fill(std::begin(dp[0]), std::end(dp[0]), 0);

    for (int w = n; w >= 1; --w) {
        for (int pw = 1; c[w] > 0; pw <<= 1) {
            const int k = std::min(pw, c[w]);
            c[w] -= k;

            const int dw = k * w;
            const long long dv = k * v[w];

            for (int num = n / w; num >= k; --num) {
                for (int wsum = n; wsum >= dw; --wsum) {
                    dp[num][wsum] = std::max(dp[num][wsum], dp[num - k][wsum - dw] + dv);
                }
            }
        }
    }
    for (int num = 1; num <= n; ++num) {
        std::cout << dp[num][n] << '\n';
    }
    return 0;
}
0