Submission #1218816

#TimeUsernameProblemLanguageResultExecution timeMemory
1218816ladnooooKnapsack (NOI18_knapsack)C++20
100 / 100
60 ms34376 KiB
#include <bits/stdc++.h>
using namespace std;

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")

#define int long long
#define pb push_back

const int maxN = 2e3 + 7;

int dp[maxN][maxN];

void solveStupid() {
    int s, n;
    cin >> s >> n;
    map<int, vector<pair<int, int>>> w;
    for(int i = 0; i < n; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        w[b].pb({a, c});
    }
    for(int i = 0; i < maxN; i++) {
        for(int j = 0; j < maxN; j++) {
            dp[i][j] = -1e18;
        }
    }
    dp[0][0] = 0;
    int k = 0;
    for(auto &[key, value] : w) {
        k++;
        sort(value.begin(), value.end(), greater<pair<int, int>>());
        for(int i = 0; i <= s; i++) {
            dp[k][i] = dp[k - 1][i];
            int plus = 0, cur = 0, type = 0, cnt = 0;
            while((cnt + 1) * key <= i && type < value.size()) {
                cnt++;
                plus += value[type].first;
                dp[k][i] = max(dp[k][i], plus + dp[k - 1][i - cnt * key]);
                cur++;
                if(cur == value[type].second) {
                    type++;
                    cur = 0;
                }
            }
        }
    }

    int mx = 0;
    for(int i = 0; i <= s; i++) {
        mx = max(dp[k][i], mx);
    }

    cout << mx << '\n';
}

signed main() {
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        solveStupid();
    }
}
#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...