# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1102288 | tuannm | Knapsack (NOI18_knapsack) | C++17 | 53 ms | 3832 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>
#define ii pair<int, int>
#define ll pair<long long, long long>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod[2] = {1000000007, 998244353};
const int W = 2e3 + 1;
const string NAME = "";
int n, s;
vector<ii> item[W];
long long dp[W];
template<class X, class Y> bool maximize(X &x, const Y &y) { if (x < y) { x = y; return true; } else return false; }
void inp(){
cin >> s >> n;
for(int i = 1; i <= n; ++i){
int v, w, k;
cin >> v >> w >> k;
item[w].pb({v, k});
}
}
void solve(){
for(int i = 1; i <= s; ++i)
dp[i] = -1e16, sort(item[i].begin(), item[i].end(), greater<ii>());
for(int w = 1; w < W; ++w){
for(int j = s; j >= 0; --j){
int sumW = 0;
long long sumVal = 0;
for(auto [v, k] : item[w]){
for(int i = 1; i <= k; ++i){
sumW += w, sumVal += v;
if(sumW > j)
break;
maximize(dp[j], dp[j - sumW] + sumVal);
}
if(sumW > j)
break;
}
}
}
long long ans = 0;
for(int i = 0; i <= s; ++i)
maximize(ans, dp[i]);
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen((NAME + ".inp").c_str(), "r")){
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
inp();
solve();
}
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... |