Submission #1002653

#TimeUsernameProblemLanguageResultExecution timeMemory
1002653kh0iKnapsack (NOI18_knapsack)C++17
100 / 100
41 ms3932 KiB
#include "bits/stdc++.h"
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif

using ll = long long;
using pii = pair<int, int>;

#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
    assert(l <= r);
    return uniform_int_distribution<ll> (l, r)(rng);
}

const int N = 2003;

int s, n;
vector<pair<int, int>> p[N], a;
int f[N];

void solve(){
    cin >> s >> n;
    for(int i = 1; i <= n; ++i){
        int v, w, k;
        cin >> v >> w >> k;
        p[w].push_back({v, k});
    }
    for(int i = 1; i <= s; ++i){
        sort(all(p[i]));
        int cnt = s / i;    
        while(!p[i].empty() and cnt){
            a.push_back({i, p[i].back().F});
            --p[i].back().S;
            if(p[i].back().S == 0)
                p[i].pop_back();
            --cnt;
        }
    }

    for(int i = 1; i < N; ++i)
        f[i] = -2e9;
    f[0] = 0;

    for(auto [w, v] : a){
        for(int i = s; i >= w; --i)
            f[i] = max(f[i], f[i - w] + v);
    }

    cout << *max_element(f, f + s + 1);
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    #define task "troll"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
//    cin >> test;
    for(int i = 1; i <= test; ++i){
//        cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef LOCAL
        cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...