#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T>
using ve = vector<T>;
using ll = long long;
using pll = pair<ll, ll>;
const char el = '\n';
const int maxw = 2007;
int n, W;
map<int, ve<pll>> orz;
ll dp[maxw][maxw];
template<typename T>
void print(T a[], int n) { for (int i = 0; i < n; i++) cout << a[i] << ' '; cout << el; }
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
// freopen("test.inp", "r", stdin);
// freopen(".inp", "r", stdin);
// freopen(".out", "w", stdout);
cin >> W >> n;
for (int i = 0; i < n; i++) {
int w; ll v, k;
cin >> v >> w >> k;
if (w <= W) orz[w].pb(pll(v, k));
}
int at = 1;
for (auto& i : orz) {
int w = i.fi;
ve<pll>& items = i.se;
sort(all(items), greater<pll>());
// for (auto i : items) cout << '(' << i.fi << ' ' << i.se << ')' << ' '; cout << el;
for (int j = 0; j <= W; j++) {
dp[at][j] = dp[at-1][j];
int cnt = 1;
int itm_cnt = 1;
int itm_at = 0;
ll acc_val = 0;
while (w*cnt <= j && itm_at < items.size()) {
acc_val += items[itm_at].fi;
ll p = acc_val + dp[at-1][j - w*cnt];
dp[at][j] = max(dp[at][j], p);
cnt++;
itm_cnt++;
if (itm_cnt > items[itm_at].se) {
itm_at++;
itm_cnt = 1;
}
}
}
at++;
}
// for (int i = 0; i < at; i++) print(dp[i], W+1);
cout << dp[at-1][W];
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... |