Submission #708704

#TimeUsernameProblemLanguageResultExecution timeMemory
708704hafoKnapsack (NOI18_knapsack)C++17
100 / 100
380 ms35148 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 7;
const ll oo = 1e17 + 69;
const int N = 2e3 + 7;

int n, s, v, w, k;
vector<pa> g[N];
ll dp[N][N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>s>>n;
    for(int i = 1; i <= n; i++) {
        cin>>v>>w>>k;
        if(w <= s && k > 0) g[w].pb({v, k});
    }
    for(int i = 1; i <= s; i++) sort(all(g[i]), greater<pa>());

    for(int i = 1; i <= s; i++){
        for(int j = 1; j <= s; j++) {
            dp[i][j] = dp[i - 1][j];
            int cnt = 0;
            ll cost = 0;
            for(auto h:g[i]) {
                for(int d = 1; d <= h.se; d++) {
                    if(1LL * i * (cnt + 1) > j) break;
                    cnt++;
                    cost += h.fi;
                    maxi(dp[i][j], dp[i - 1][j - cnt * i] + cost);

                }
                if(1LL * i * cnt > j) break;
            }
        }
    }
//    for(int i = 1; i <= s; i++) {
//        for(int j = 1; j <= s; j++) cout<<dp[i][j]<<" ";
//        cout<<"\n";
//    }
    cout<<dp[s][s];
    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...