Submission #542634

#TimeUsernameProblemLanguageResultExecution timeMemory
542634Vladth11Knapsack (NOI18_knapsack)C++14
100 / 100
141 ms3728 KiB
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "

using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <long double, pii> muchie;

const ll NMAX = 2001;
const ll VMAX = 1000001;
const ll INF = (1LL << 60);
const ll MOD = 1000000007;
const ll BLOCK = 1000000;
const ll nr_of_bits = 16;

vector <int> v[NMAX];
vector <pii> p[NMAX];
int dp[NMAX];

int main() {
    int s, n, i;
    cin >> s >> n;
    for(i = 1; i <= n; i++){
        int a, b, c;
        cin >> a >> b >> c;
        p[b].push_back({a, c});
    }
    for(i = 1; i <= s; i++){
        sort(p[i].begin(), p[i].end());
        reverse(p[i].begin(), p[i].end());
        int sum = 0;
        for(auto &x : p[i]){
            while(sum < s && x.second > 0){
                x.second--;
                v[i].push_back(x.first);
                sum += i;
            }
        }
    }
    for(i = 1; i <= s; i++){
        for(auto x : v[i]){
            for(int j = s - i; j >= 0; j--){
                dp[j + i] = max(dp[j + i], dp[j] + x);
            }
        }
    }
    for(int j = 1; j <= s; j++){
        dp[j] = max(dp[j], dp[j - 1]);
    }
    cout << dp[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...