Submission #881675

#TimeUsernameProblemLanguageResultExecution timeMemory
881675seriouslyFlawedKnapsack (NOI18_knapsack)C++17
0 / 100
1 ms348 KiB

#include <bits/stdc++.h>

using namespace std;

// Shortcuts for common operations
#define pb push_back
#define ppb pop_back
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define endl "\n"

// Type definitions for convenience
typedef vector<int> vi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef unordered_set<int> usi;
typedef unordered_map<int, int> umii;

// Debugging macros
#define debug(x) cerr << #x << " = " << (x) << '\n'
#define debug_vector(v) _debug_vector(#v, v)
template<typename T>
void _debug_vector(const string& name, const vector<T>& a) {
    cerr << name << " = [ ";
    for(const auto &x : a) cerr << x << ' ';
    cerr << "]\n";
}

// I/O redirection for local testing
#define iofile(io) \
        freopen((io + ".in").c_str(), "r", stdin); \
        freopen((io + ".out").c_str(), "w", stdout);

// delta for floodfill
vi dx = {0, 1, 0, -1};
vi dy = {1, 0, -1, 0};

// extended deltas for floodfill
vi edx = {0, 1, 0, -1, 1, 1, -1, -1};
vi edy = {1, 0, -1, 0, 1, -1, 1, -1};

// Common outputs
void yes() { cout << "YES" << '\n'; }
void no() { cout << "NO" << '\n'; }

// cin.tie(0)->sync_with_stdio(0);

void fx() {
    // Functionality for fx
    int s, n;
    cin >> s >> n;
    
    map<int, vii>tab;
    vector<ll>total(2001, 0);

    for(int i = 0; i < n; i++){
        int v, w, k;
        cin >> v >> w >> k;

        tab[w].pb({v, k});
        total[w] += k;
    }

    for(auto &i: tab) sort(all((i).s)), reverse(all((i).s));

    int us = tab.size();
    vector<vector<ll>>dp(us+5, vector<ll>(s+10, 0));

    int curr = 1;
    for(auto &i: tab){

        for(int take = 0; take <= min(total[i.f], 2000LL); take++){
            if(take * i.f > s) break;

            ll value = 0;
            int rem = take;
            int temp = 0;

            while(rem){
                int localTake = min((i.s)[temp].s, rem);
                value += localTake * (i.s)[temp].f;
                temp++;
                rem -= localTake;
            }

            for(int j = s; j >= take * i.f; j--) dp[curr][j] = max(dp[curr][j], dp[curr-1][j - take * i.f] + value);

        }
        curr++;
    }

    cout << *max_element(all(dp[n-1])) << endl;


}

int main() {
    // Uncomment the following lines for file I/O
    // iofile(string("hello"));
    
    // Uncomment the following lines for multiple test cases
    // int t; cin >> t;
    // while(t--) fx();
    
    // Single test case
    fx();
    
    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...