#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
void add(int &x, int y) {x += y; if (x >= mod) x -= mod;}
void sub(int &x, int y) {x -= y; if (x < 0) x += mod;}
int mul(int x, int y) {return 1LL * x * y % mod;}
// -----------------------------------------//
int n, S;
int weight[N], value[N], cop[N];
void read() {
cin >> S >> n;
for(int i = 1; i <= n; i++) cin >> value[i] >> weight[i] >> cop[i];
}
void sub1() {
int cnt = S / weight[1];
cout << 1LL * min(cnt, cop[1]) * value[1];
}
void sub2() {
vector<long long> dp(S + 2, 0);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= min(cop[i], S / weight[i]); j++) {
for(int k = S; k >= weight[i]; k--) {
maximize(dp[k], dp[k - weight[i]] + value[i]);
}
}
}
cout << dp[S];
}
void lastSol() {
vector<vector<long long>> dp(S + 5, vector<long long>(S + 5, 0));
vector<pair<int, int>> f[2005];
for(int i = 1; i <= n; i++) f[weight[i]].push_back({value[i], cop[i]});
for(int i = 1; i <= S; i++) sort(ALL(f[i]));
for(int i = 1; i <= S; i++) {
for(int j = 1; j <= S; j++) {
dp[i][j] = dp[i - 1][j];
int p = f[i].size() - 1;
int cnt = 0;
if (f[i].size()) cnt = f[i].back().se;
long long sum = 0;
for(int k = 1; k <= j / i; k++) {
if (cnt == 0) {
--p;
if (p < 0) break;
cnt = f[i][p].se;
}
if (p < 0) break;
sum += f[i][p].fi;
maximize(dp[i][j], dp[i - 1][j - k * i] + sum);
--cnt;
}
}
}
long long ans = 0;
for(int i = 0; i <= S; i++) ans = max(ans, dp[S][i]);
cout << ans;
}
void solve() {
lastSol();
// if (n == 1) sub1();
// else if (n <= 100) sub2();
// else lastSol();
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |