提出 #61749709


ソースコード 拡げる

#include <bits/stdc++.h>

using namespace std;

#define in(a)                                                                  \
    for (auto &x : a)                                                          \
        cin >> x;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define out(a)                                                                 \
    for (auto x : a)                                                           \
        cout << x << " ";
#define pb push_back
#define int long long
typedef vector<int> vi;
typedef pair<int, int> pii;

void solve()
{
    int n, c;
    cin >> n >> c;
    vector<pair<int, int>> knapsack;
    vector<int> dp(c + 1, 0);
    for (int i = 0; i < n; i++) {
        int weight, value;
        cin >> weight >> value;
        knapsack.push_back({weight, value});
    }

    // so need to find a way to implement take or no take
    // take: means weight reduces while cost increases
    // no take: everything remains the same
    // dp[w] -> store max value possible at weight w
    for (auto [w, v] : knapsack) {
        for (int j = c; j >= 1; j--) {
            if (j - w >= 0) {
                dp[j] = max(dp[j - w] + v, dp[j]);
            }
        }
    }
    cout << dp[c] << '\n';
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

提出情報

提出日時
問題 D - Knapsack 1
ユーザ rakim0
言語 C++ 20 (gcc 12.2)
得点 100
コード長 1481 Byte
結果 AC
実行時間 8 ms
メモリ 4020 KiB

ジャッジ結果

セット名 All
得点 / 配点 100 / 100
結果
AC × 13
セット名 テストケース
All 0_00, 0_01, 0_02, 1_00, 1_01, 1_02, 1_03, 1_04, 1_05, 1_06, 1_07, 1_08, 1_09
ケース名 結果 実行時間 メモリ
0_00 AC 1 ms 3456 KiB
0_01 AC 1 ms 3524 KiB
0_02 AC 1 ms 3608 KiB
1_00 AC 1 ms 3932 KiB
1_01 AC 7 ms 3896 KiB
1_02 AC 8 ms 4020 KiB
1_03 AC 8 ms 3844 KiB
1_04 AC 8 ms 3840 KiB
1_05 AC 7 ms 3840 KiB
1_06 AC 7 ms 3768 KiB
1_07 AC 7 ms 3812 KiB
1_08 AC 8 ms 3804 KiB
1_09 AC 7 ms 3940 KiB