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;
int s , n;
const int N = 1e5 + 7;
int w[N] , v[N] , k[N];
vector<pair<int ,int>> adj[2007];
long long dp[2][2007];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> s >> n;
for(int i = 1 ; i <= n ; i++){
cin >> v[i] >> w[i] >> k[i];
adj[w[i]].emplace_back(v[i] , k[i]);
}
for(int i = 1; i <= 2000 ; i ++) sort(adj[i].rbegin() , adj[i].rend());
n = 0;
for(int i = 1 ; i <= 2000 ; i++){
int lim = s / i;
for(int j = 0, h = 0; j < lim; j++) {
if (h == (int)adj[i].size()) break;
w[++n] = i;
v[n] = adj[i][h].first;
adj[i][h].second--;
if (!adj[i][h].second) h++;
}
}
memset(dp , 0xef , sizeof dp);
dp[0][0] = 0;
for(int i = 1 ; i <= n ; i++){
for(int val = s ; val >= 0 ; val--) {
dp[i & 1][val] = dp[~i & 1][val];
if (val >= w[i])
dp[i & 1][val] = max(dp[i & 1][val], dp[~i & 1][val - w[i]] + v[i]);
}
}
long long ans = numeric_limits<long long>::min();
for (int i = 0; i <= s; i++)
ans = max(ans, dp[n & 1][i]);
cout << ans << '\n';
}
# | 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... |