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;
int32_t main() {
int S, N, W, v, n, ans = -1e18;
cin >> S >> N;
vector<vector<pair<int, int>>> w(S + 1);
for (int i = 0; i < N; i++) {
cin >> v >> W >> n;
w[W].push_back({-v, n});
}
for (auto& i: w) {
if (!i.size()) continue;
sort(i.begin(), i.end());
}
vector<vector<int>> dp(S + 1, vector<int>(S + 1, -1e18));
dp[0][0] = 0;
for (int i = 1; i <= S; i++) {
if (!w[i].size()) {
dp[i] = dp[i - 1];
continue;
}
for (int j = 0; j <= S; j++) {
dp[i][j] = dp[i - 1][j];
int amnt = 0;
int ind = 0;
int used = 0;
int val = 0;
while ((amnt + 1) * i <= j && ind < (int)w[i].size()) {
amnt++;
val += -w[i][ind].first;
if (dp[i - 1][j - amnt * i] != -1e18) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - amnt * i] + val);
}
ans = max(ans, dp[i][j]);
used++;
if (used == w[i][ind].second) {
used = 0;
ind++;
}
}
}
}
if (ans < 0) ans = 0;
cout << ans << '\n';
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... |