# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
543716 | zammy | Knapsack (NOI18_knapsack) | C++17 | 107 ms | 262144 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 int long long
using namespace std;
const int inf = INT_MAX, mod = 1e9 + 7, MAX_N = (1e5), MAX_S = 2000;
int dp[MAX_N + 1][MAX_S + 1];
void solve()
{
int S, N;
cin >> S >> N;
vector<int> V(N + 1), W(N + 1), K(N + 1);
for (int i = 1; i <= N; i++)
cin >> V[i] >> W[i] >> K[i];
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= S; j++)
{
dp[i][j] = dp[i - 1][j];
for (int k = 1; k <= K[i]; k++)
{
int wt = W[i] * k;
if (j - wt < 0)
break;
dp[i][j] = max(dp[i][j], (V[i] * k) + dp[i - 1][j - wt]);
}
}
}
cout << dp[N][S] << "\n";
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed;
cout << setprecision(9);
int tt = 1;
// cin >> tt;
for (int i = 1; i <= tt; i++)
{
// cout << "Case #" << i << ": ";
solve();
}
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}
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... |