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>
using namespace std;
#ifdef DEBUG
#define LOG(...) cerr << "[" << #__VA_ARGS__ << "]: " << repr(__VA_ARGS__) << endl;
#define MSG(args) cerr << args << "\n";
#define debug(x) x
#else
#define LOG(...)
#define MSG(args)
#define debug(x)
#endif
#define mp make_pair
#define pb push_back
#define sz(x) (int((x).size()))
#define ms(x, v) memset((x), v, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vvi = vector<vi>;
int S, n;
map<int, vii> mpp;
ll memo[2001][2001];
ll solve(int cur, int rem) {
if (cur > S || rem <= 0) return 0LL;
else if (memo[cur][rem] != -1) return memo[cur][rem];
ll now = 0, value = 0;
ll res = solve(cur + 1, rem);
for (auto [v, k] : mpp[cur]) {
while (k--) {
now += cur;
value += v;
if (now > rem)
break;
res = max(res, solve(cur + 1, rem - now) + value);
}
if (now > rem)
break;
}
return memo[cur][rem] = res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ms(memo, 0xff);
cin >> S >> n;
for (int i = 0; i < n; ++i) {
int v, w, k; cin >> v >> w >> k;
if (w <= S) {
mpp[w].pb({v, k});
}
}
for (int i = 1; i <= S; ++i)
sort(all(mpp[i]), greater<pii>());
cout << solve(1, S) << endl;
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... |