Submission #927641

#TimeUsernameProblemLanguageResultExecution timeMemory
927641Ojas_2003Knapsack (NOI18_knapsack)C++17
73 / 100
169 ms262144 KiB
#include <bits/stdc++.h>// #include <climits>// #include<iostream> using namespace std;// #define ll long long // using ll = __int128; #define pii pair<int, int> #define pll pair<long long, long long> #define vi vector<int> #define vll vector<long long> #define mii map<int, int> #define si set<int> #define ff first #define ss second // #define sc set<char> /* FUNCTION */ #define f(i, s, e) for (long long i = s; i < e; i++) #define fe(i, s, e) for (long long i = s; i <= e; i++) #define rf(i, e, s) for (long long i = e - 1; i >= s; i--) #define pb push_back #define eb emplace_back /* PRINTS */ template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "\b"; } /* UTILS */ #define MOD 1000000007 #define debug(x) cout << #x << " " << x << endl; #define pi 3.14159 ll min(ll a, int b) { if (a < b) return a; return b; } ll min(int a, ll b) { if (a < b) return a; return b; } ll max(ll a, int b) { if (a > b) return a; return b; } ll max(int a, ll b) { if (a > b) return a; return b; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } string to_upper(string a) { for (int i = 0; i < (int)a.size(); ++i) if (a[i] >= 'a' && a[i] <= 'z') a[i] -= 'a' - 'A'; return a; } string to_lower(string a) { for (int i = 0; i < (int)a.size(); ++i) if (a[i] >= 'A' && a[i] <= 'Z') a[i] += 'a' - 'A'; return a; } bool prime(ll a) { if (a == 1) return 0; for (int i = 2; i <= round(sqrt(a)); ++i) if (a % i == 0) return 0; return 1; } ll pow(ll a,ll b){ long long res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } void yes() { cout << "YES\n"; } void no() { cout << "NO\n"; } typedef long int int32; typedef unsigned long int uint32; typedef long long int int64; typedef unsigned long long int uint64; typedef vector<ll> vl; typedef vector<char> vc; typedef vector<string> vs; template <typename T> void add(T &a, T b, T M) { a = ((a % M) + (b % M)) % M; } template <typename T> void mul(T &a, T b, T M) { a = ((a % M) * (b % M)) % M; } template <typename T> void sub(T &a, T b, T M) { a = (a - b + M) % M; } typedef vector<pair<ll,ll>>vpl; #define all(v) (v).begin(),(v).end() class dsu{ ll n; vector<ll> par, sz; public: int maxi = 0; dsu(ll s){ n=s; for(ll i=0; i<n; i++){ par.push_back(i); sz.push_back(1); } maxi = 1; } ll findPar(ll a){ if(par[a]==a) return a; return par[a]=findPar(par[a]); } bool Union(ll a, ll b){ int par_a = findPar(a); int par_b = findPar(b); if(par_a == par_b) return 0; if(sz[par_a]<sz[par_b]){ par[par_a]=par[par_b]; sz[par_b]+=sz[par_a]; maxi = max(maxi, findsize(par_b)); } else{ par[par_b]=par[par_a]; sz[par_a]+=sz[par_b]; maxi = max(maxi, findsize(par_a)); } return 1; } ll findsize(ll i){ ll pa = findPar(i); return sz[pa]; } }; ll derangements(ll a){ if(a == 1) return 0; if(a == 2) return 1 ; ll nm2 = 0; ll nm1 = 1; for(int i = 3 ; i <= a ; i ++ ){ ll curr = (i-1)*(nm1+nm2); nm2 = nm1; nm1 = curr ; } return nm1 ; } /* *************************************************************************************** *************************************************************************************** *************************************************************************************** ----------------- IF YOU ARE GOOD AT SOMETHING NEVER DO IT FOR FREE ------------------ ------------------------------ KEEP IT SIMPLE STUPID --------------------------------- *************************************************************************************** *************************************************************************************** *************************************************************************************** I been searching for the courage To face my enemies When they turn down the lights..... I hear my battle symphony All the world in front of me If my armor breaks I’ll fuse it back together **************************************************************************************** **************************************************************************************** **************************************************************************************** */ void solve(){ int K, n ; cin >> K >> n ; vector<ll> values(n+1), weights(n+1), copies(n+1); for(int i = 1 ; i <= n ; i ++ ){ cin >> values[i] >> weights[i] >> copies[i]; } vector<vector<ll>> dp(n+1, vector<ll>(K+1)); for(int i = 1 ; i <= n ; i ++ ){ for(int k = 0 ; k <= K ; k++){ dp[i][k] = dp[i-1][k]; for(int j = 1 ; j <= copies[i] ; j++){ if(k-j*weights[i] >= 0){ dp[i][k] = max(dp[i][k], dp[i-1][k-j*weights[i]] + values[i]*j); } else break; } } } cout << dp[n][K] << '\n'; } int main(){ // #ifndef ONLINE_JUDGE // // freopen("talent.in", "r", stdin); // file input.txt is opened in reading mode i.e "r" // // freopen("talent.out", "w", stdout); // file output.txt is opened in writing mode i.e "w" // freopen("input.txt", "r", stdin); // file input.txt is opened in reading mode i.e "r" // freopen("output.txt", "w", stdout); // #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); const int M = 998244353; int t ; t = 1 ; // cin >> t; while(t--) { solve(); } return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:313:15: warning: unused variable 'M' [-Wunused-variable]
  313 |     const int M = 998244353;
      |               ^
#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...