# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
696312 | Alihan_8 | Knapsack (NOI18_knapsack) | C++17 | 90 ms | 36300 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>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
#define int long long
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int S, n; cin >> S >> n;
vector <pair<int,int>> g[S+1];
for ( int i = 1; i <= n; i++ ){
int v, w, k; cin >> v >> w >> k;
assert(w > 0 and w <= S);
g[w].pb({v, k});
}
vector <vector<int>> dp(S+1, vector <int> (S+1));
int _it = 0;
for ( auto &p: g ){
if ( !_it ){
_it = 1; continue;
}
sort(all(p), greater <pair<int,int>> ());
for ( int i = 1; i <= S; i++ ){
dp[_it][i] = dp[_it-1][i];
int it = 0, cnt = 0, value = 0, num = 0;
while ( (cnt+1)*_it <= i and it < (int)p.size() ){
value += p[it].first;
cnt++;
dp[_it][i] = max(dp[_it][i], dp[_it-1][i-cnt*_it]+value);
if ( ++num == p[it].second ){
num = 0; it++;
}
}
} _it++;
}
cout << *max_element(all(dp.back()));
cout << '\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |