# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
908555 | Ronak1808 | Knapsack (NOI18_knapsack) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <chrono>
#define int long long
using namespace __gnu_pbds;
using namespace std;
using namespace std::chrono;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MOD = 998244353;
int add(int x, int y){
return ((x + y) % MOD + MOD) % MOD;
}
int mul(int x, int y){
return x * 1ll * y % MOD;
}
int binpow(int x, int y){
int z = 1;
while(y){
if(y % 2 == 1) z = mul(z, x);
x = mul(x, x);
y /= 2;
}
return z;
}
int inv(int x){
return binpow(x, MOD - 2);
}
int divide(int x, int y){
return mul(x, inv(y));
}
signed main(){
int s, n;
cin>>s>>n;
map<int, vector<pair<int, int>>>mp;
for(int i = 1;i<=n;i++){
int v, w, k;
cin>>v>>w>>k;
mp[w].push_back({v, k});
}
for(auto &x : mp){
sort((x.second).begin(), (x.second).end(), greater<pair<int, int>>());
}
vector<vector<int>>dp(int(mp.size()), vector<int>(s+1, 0));
auto it = mp.begin();
int res= 0;
for(int i = 0;i<int(mp.size());i++){
for(int use = 0; use <= s; use++){
int sum = 0;
int val = 0;
dp[i][use] = (i-1 >= 0?dp[i-1][use]:0);
for(auto &x : it->second){
val += x.first;
sum += it->first;
if(sum > use)break;
dp[i][use] = max(dp[i][use], val + (i-1 >= 0?dp[i-1][use - sum]:0));
}
res = max(res, dp[i][use]);
}
it++;
}
cout<<res<<"\n";
}