Submission #467707

#TimeUsernameProblemLanguageResultExecution timeMemory
467707ShinKnapsack (NOI18_knapsack)C++14
100 / 100
95 ms3776 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define TASK "file"
#define all(x) x.begin(), x.end()

using namespace std;
const int N = 2e3 + 7;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const long long INFLL = 1e18 + 7;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <class X, class Y> bool minimize(X &a, Y b) {
    if (a > b) return a = b, true;
    return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
    if (a < b) return a = b, true;
    return false;
}

struct Items {
    int v, k;
    Items(int _v = 0, int _k = 0) : v(_v), k(_k) {}
    bool operator <(const Items &a) const {
        return v > a.v;
    }
};

int n, max_weight;
ll dp[N];
vector<Items> w[N];

void solve(void) {
    cin >> max_weight >> n;
    for (int i = 1; i <= n; i ++) {
        int x, y, z; cin >> y >> x >> z;
        w[x].push_back(Items(y, z));
    }

    for (int i = 1; i <= max_weight; i ++) {
        sort(all(w[i]));
        
        try {
            ll sum = 0;
            for (Items x: w[i]) while (x.k --) {
                for (int k = max_weight; k >= i; k --)
                    maximize(dp[k], dp[k - i] + x.v);
                if (sum >= max_weight) throw 1;
                sum += i;
            }
        } catch(...) {}
    }

    cout << dp[max_weight];
}

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

    int test = 1;
    // cin >> test;
    while (test --) {
        solve();
    }
    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...