Submission #1266934

#TimeUsernameProblemLanguageResultExecution timeMemory
1266934samarthkulkarniKnapsack (NOI18_knapsack)C++20
73 / 100
308 ms327680 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("fast-math")

#define ll long long
#define ld long double
#define vi vector<ll>
#define endl "\n"
#define pr pair<ll, ll>
#define ff first 
#define ss second
#define all(x) x.begin(), x.end()
const int mod = 1e9+7;
void _p(ll a){cout<<a<<endl;}
void _p(string a){cout<<a<<endl;}
void _p(ld a) {cout<<a<<endl;}
template <class T>
void _p(vector<T> a){for(T val:a)cout<<val<<" ";cout<<endl;}
#define debug(x) cout<<#x<<" -> ";_p(x)

typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > ordered_set;

vector<pr> move8 = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
vector<pr> move4 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<pr> move2 = {{0, 1}, {1, 0}};

void solution();
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solution();
    return 0;
}


struct DT {ll v, w, k;};


/*
constraints:
n <= 1e5, s <= 2000, k <= 2000, w <= 2000
obs 1: k <= 2000
obs 2: we can't select more then 2000 values.

*/

void solution() {
    ll s, n; cin >> s >> n;
    vector<DT> A(n);
    for (int i = 0; i < n; i++) {
    	DT Z; cin >> Z.v >> Z.w >> Z.k;
    	A[i] = Z;
    }
    for (int i = 0; i < n; i++) {
    	A[i].k = min(A[i].k, s/A[i].w);
    }


    vector<vi> dp(n+1, vi(s+1));

    for (int i = 1; i <= n; i++) {
    	for (int j = 1; j <= s; j++) {

    		for (int cnt = 0; cnt <= A[i-1].k; cnt++) {
    			if (j >= cnt*A[i-1].w)
    			dp[i][j] = max(dp[i][j], dp[i-1][j - cnt*A[i-1].w] + cnt*A[i-1].v);
    		}
    	}
    }


    cout << dp[n][s] << endl;



}
#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...