#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()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
// #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 k = 1;
while(k <= q && k * weight <= s){
w[cnt] = weight * k;
v[cnt] = value * k;
q -= k;
k *= 2;
cnt++;
}
if(q > 0 && q * weight <= s){
w[cnt] = weight * q;
v[cnt] = weight * q;
cnt++;
}
}
n = cnt;
vector<vector<int>> dp(n + 5, vector<int>(s + 5));
dp[0].assign(s + 5, 0);
for(int i = 1; i <= n; i++){
for(int ww = 0; ww <= s; ww++){
dp[i][ww] = dp[i - 1][ww];
if(ww >= w[i])
dp[i][ww] = max(dp[i][ww], dp[i - 1][ww - w[i]] + v[i]);
}
}
cout << dp[n][s] << nl;
}
signed main(void)
{
fastio();
int tc = 1;
//cin >> tc;
int i = 1;
while (tc--)
{
// cout<<"Case #"<<i<<": ";
solve(i++);
}
return 0;
}
# | 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... |