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 <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
#define v vector
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define F first
#define S second
#define ll long long
#define pll pair<long long, long long>
#define sz(p) p.size()
#define all(x) x.begin(), x.end()
int n, s;
v<ll> va(100005);
v<v<ll>> dp(104, v<ll>(2001));
v<int> w(100005), k(100005);
int main()
{
cin >> s >> n;
ll ans = 0;
for (int i = 0; i < n; i++)
cin >> va[i] >> w[i] >> k[i];
for (int i = 1; i <= k[0] && i * w[0] <= s; i++)
dp[0][i * w[0]] = i * va[0];
// cout << dp[0][w[0]] << endl;
// cout << "max_index: 0" << endl;
// for (int k = 0; k <= s; k++)
// cout << dp[0][k] << ' ';
// cout << endl;
for (int j = 1; j < n; j++)
{
// cout << "max_index: " << j << endl;
for (int i = s; i >= 0; i--)
{
// dp[i] = max(dp[i - (q * w[j])] + q * va[j], dp[i]);
for (int q = 0; q <= k[j]; q++)
{
if (q * w[j] > i)
break;
if (dp[j - 1][i - (q * w[j])] || i == q * w[j])
dp[j][i] = max(max(dp[j][i], dp[j - 1][i - (q * w[j])] + (ll)q * va[j]), dp[j - 1][i]);
// cout << i << ' ' << q << endl;
// cout << i << ' ' << dp[j][i] << ' ' << dp[j][i - 1] << endl;
}
// cout << "weight: " << i << endl;
// for (int k = 0; k <= s; k++)
// cout << dp[j][k] << ' ';
// cout << endl;
}
}
for (int i = 0; i <= s; i++)
ans = max(ans, dp[n - 1][i]);
cout << ans << endl;
}
# | 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... |