Submission #1358688

#TimeUsernameProblemLanguageResultExecution timeMemory
1358688Desh03Knapsack (NOI18_knapsack)C++20
100 / 100
146 ms16872 KiB
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int s, n;
    cin >> s >> n;
    vector<pair<int, long long>> cc;
    for (int i = 0; i < n; i++) {
        int r, v, k;
        cin >> r >> v >> k;
        for (int i = 0; i < 30; i++) {
            k -= (1 << i);
            if (k < 0) {
                k += (1 << i);
                break;
            }
            if ((1LL << i) * v <= s) {
                cc.push_back({(1 << i) * v, (1LL << i) * r});
            }
        }
        if (k && (long long) k * v <= s) {
            cc.push_back({k * v, (long long) k * r});
        }
    }
    sort(cc.begin(), cc.end(), [&](const auto &x, const auto &y) {
        return (__int128) x.second * y.first > (__int128) y.second * x.first;
    });
    vector<long long> dp(s + 1);
    for (int i = 0; i < min(120000, (int) cc.size()); i++) {
        auto [x, y] = cc[i];
        for (int j = s; j >= x; j--) {
            dp[j] = max(dp[j], dp[j - x] + y);
        }
    }
    cout << dp[s] << '\n';
    return 0;
}
#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...