結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 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,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 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,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 7 ms
4,528 KB
testcase_29 AC 8 ms
4,824 KB
testcase_30 AC 8 ms
4,836 KB
testcase_31 AC 8 ms
4,892 KB
testcase_32 AC 7 ms
4,376 KB
testcase_33 AC 9 ms
4,952 KB
testcase_34 AC 8 ms
4,824 KB
testcase_35 AC 5 ms
4,380 KB
testcase_36 AC 7 ms
4,600 KB
testcase_37 AC 7 ms
4,688 KB
testcase_38 AC 315 ms
52,192 KB
testcase_39 AC 283 ms
52,044 KB
testcase_40 AC 294 ms
52,076 KB
testcase_41 AC 309 ms
52,168 KB
testcase_42 AC 314 ms
52,308 KB
testcase_43 AC 306 ms
52,192 KB
testcase_44 AC 309 ms
52,084 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 w = 1; w <= n; ++w) {
        std::cin >> c[w] >> v[w];
        c[w] = std::min(c[w], n / w);
    }

    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