# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1126505 | mazen_ghanayem | Knapsack (NOI18_knapsack) | C++20 | 1095 ms | 19012 KiB |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned long long
#define ld long double
#define int long long
#define nl "\n"
#define oo 1e9 + 1
#define OO 1e18 + 1
#define sp ' '
#define sz(x) (int)(x.size())
#define MOD 1000000007
#define fixed(n) fixed << setprecision(n)
#define sub_mod(a, b, m) ((((a) % m) - ((b) % m) + m) % m)
#define add_mod(a, b, m) ((((a) % m) + ((b) % m)) % m)
#define mult_mod(a, b, m) ((((a) % m) * ((b) % m)) % m)
#define EPS 1e-9
#define PI acos(-1)
using namespace __gnu_pbds;
using namespace std;
void fastio()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}
void solve(int tc)
{
int s, n;
cin >> s >> n;
vector<int> w(n * 12 + 5);
vector<int> v(n * 12 + 5);
int cnt = 1;
while (n--)
{
int weight, value, q;
cin >> value >> weight >> q;
int x = s / weight;
q = min(q, x + 1);
int k = 1;
while (k <= q)
{
w[cnt] = weight * k;
v[cnt] = value * k;
q -= k;
k *= 2;
cnt++;
}
if (q > 0)
{
w[cnt] = weight * q;
v[cnt] = value * q;
cnt++;
}
}
n = cnt;
vector<int> dp(s + 5);
for (int i = 1; i <= n; i++)
{
vector<int> ndp = dp;
for (int ww = w[i]; ww <= s; ww++)
{
ndp[ww] = max(ndp[ww], dp[ww - w[i]] + v[i]);
}
dp = ndp;
}
cout << dp[s] << nl;
}
signed main(void)
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
//fastio();
int tc = 1;
// cin >> tc;
int i = 1;
while (tc--)
{
// cout<<"Case #"<<i<<": ";
solve(i++);
}
return 0;
}
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... |