# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1102281 | tuannm | Knapsack (NOI18_knapsack) | C++17 | 1090 ms | 2900 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 N = 1e5 + 1;
const string NAME = "";
int n, s;
array<int, 3> a[N];
long long dp[N];
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)
cin >> a[i][0] >> a[i][1] >> a[i][2];
}
void solve(){
for(int i = 1; i <= s; ++i)
dp[i] = -1e16;
for(int i = 1; i <= n; ++i){
for(int w = s; w > 0; --w){
for(int j = 1; j <= min(a[i][2], w / a[i][1]); ++j)
maximize(dp[w], dp[w - j * a[i][1]] + 1LL * a[i][0] * j);
}
}
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... |