제출 #1043884

#제출 시각아이디문제언어결과실행 시간메모리
1043884sanatp977Knapsack (NOI18_knapsack)C++17
73 / 100
1096 ms2036 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int s; cin >> s;
    int n; cin >> n;

    vector<tuple<int, int, ll>> stor(n); 
    for (int i = 0; i < n; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        stor[i] = make_tuple(b, a, min((ll)s / b, c));
    }

    sort(stor.begin(), stor.end()); 

    vector<ll> dp(s + 1, 0);
    ll ans = 0;

    for (int i = 0; i < n; i++) {
        int weight = get<0>(stor[i]);
        int value = get<1>(stor[i]);
        ll maxItems = get<2>(stor[i]);

        for (int j = 0; j < maxItems; j++) {
            for (int x = s; x >= weight; x--) {
                dp[x] = max(dp[x], dp[x - weight] + value);
                ans = max(ans, dp[x]);
            }
        }
    }

    cout << ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...