#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define str string
#define ch char
#define task ""
#define pb push_back
#define ii pair<ll,ll>
#define iii pair <ll, ii>
#define fi first
#define se second
using namespace std;
template<class X, class Y>
bool maximize(X &x, const Y &y) { return (x < y) ? (x = y, true) : false; }
template<class X, class Y>
bool minimize(X &x, const Y &y) { return (x > y) ? (x = y, true) : false; }
const ll mod = 1e9 + 7;
const ll N = 1e6;
const ll inf = LLONG_MAX;
const ll SHIFT = 1e6;
ll a[N], w[N], v[N], dp[N];
ll n, s;
void sub1();
void sub2();
void sub3();
vector <ll> item[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
if (fopen(task ".inp", "r")) {
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
cin >> s >> n;
/*for (ll i = 1; i <= n; i++) {
cin >> v[i] >> w[i] >> a[i];
}
for (ll i = 1; i <= n; i++) {
for (ll j = s; j >= w[i]; j--) {
for (ll k = 1; k <= a[i]; k++) {
if (j - k * w[i] >= 0) {
dp[j] = max(dp[j], dp[j - k * w[i]] + k * v[i]);
} else {
break;
}
}
}
}*/
for (ll i = 1; i <= n; i++) {
ll v, w, k;
cin >> v >> w >> k;
ll take = min(k, (ll) s / w);
for (ll t = 0; t < take; t++) {
item[w].push_back(v);
}
}
for (ll w = 0; w <= s; w++) {
if (item[w].empty()) continue;
sort (item[w].begin(), item[w].end(), greater<ll>());
if (item[w].size() > s / w) {
item[w].resize(s / w);
}
for (ll v : item[w]) {
for (ll j = s; j >= w; j--) {
dp[j] = max(dp[j], dp[j - w] + v);
}
}
}
cout << dp[s];
return 0;
}
/*Set the pace, set the goal, maintain your streak by solving problem everyday*/
/*
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⣠⡀⠀⠀⠀⠀⠀⣀⣴⠞⠀⠀⠀⠀⠀⠀ ⢀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⡼⣿⠀⠀⠀⣀⠴⠋⢡⠎⠀⠀⣀⡠⠴⠒⢉⠟⠁⠀⠀⠀ ⣀⣠⣤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢠⡄⠀⠀⠀⢠⠎⠀⡇⢀⡠⠚⠁⠀⢀⡯⠤⠚⠋⠁⠀⢀⡴⢃⣀⠤⠖⠛⠉⣠⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⡜⢧⠀⠀⣰⠋⠀⢠⡧⠋⠀⠀⠀⠀⠈⠀⠀⠀⠀⠀⠰⠞⠋⠉⠀⠀⠀⢀⡜⠁⣀⣀⣀⣀⣀⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⣰⠁⢸⡀⡰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠋⠉⠉⠉⠉⠉⠉⢉⡭⠛⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡏⠀⠀⠿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⠖⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢸⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠶⣋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⣀⠀⠀⠀⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢦⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠙⣟⠒⠤⡧⠀⠀⠀⠀⠀⠀⠀⠐⢦⠀⠀⠀⠀⣄⠀⠀⠀⠀⢶⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠳⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠈⢦⠀⠀⠀⠀⠀⢀⣤⣴⣶⠿⢿⣷⣦⡀⠀⠘⣷⡄⠀⠀⠀⢳⣄⠀⠀⠀⠀⠰⡄⠀⠀⠀⠀⠀⠐⢶⠲⠤⣤⣌⡑⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠈⢳⡀⣠⣴⡾⠟⠉⠁⠀⠀⠈⢸⣿⣿⣦⡀⠈⢿⣶⣄⠀⠈⢻⣿⣶⣄⠀⠀⢹⣄⠀⠀⠀⠀⠀⠈⠳⡀⠀⠀⠈⠉⠃⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⡿⢟⢁⡀⠀⢀⣀⡤⠶⠒⠛⣿⣿⣿⣿⣦⣌⢿⣿⣿⣦⡀⢿⣿⣿⣿⣦⣄⣿⣷⣄⠀⠀⢷⠤⣀⠹⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⣼⡁⢰⣸⣷⠞⠋⠀⠀⠀⠀⠤⢿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣷⣿⣿⣿⠿⣿⣿⣿⣿⣷⣄⠸⡆⠈⠙⠾⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⢿⣽⡿⡏⠛⠀⠀⠀⣀⣤⣶⣶⣾⣿⡿⠿⠛⠛⠉⣏⠉⠁⠀⢰⠟⣯⣾⡟⠻⣿⡍⠉⠛⣟⠳⣽⡀⠀⠀⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⢸⠟⠃⠀⢀⣤⣶⣿⣿⣿⡿⣋⡭⣄⠀⠀⠀⠀⠀⠘⣆⠀⠀⡏⠀⣿⣿⣧⡀⢿⡷⠀⠀⠘⣄⠈⠛⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠈⡇⣀⣶⣾⣿⠿⣫⢿⢟⣵⣾⣟⣻⠀⠀⠀⠀⠀⠀⠹⣶⡀⡇⠀⠈⣿⣇⠇⣾⡷⠀⠀⠀⠘⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠻⣿⣿⡋⠀⠸⠁⠎⣿⣿⣟⡿⠇⠀⠀⠀⠀⠀⠀⠀⠯⢳⡇⠀⠀⠛⠋⣸⣿⠆⠀⠀⠀⢤⣜⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠈⢻⣳⡆⢠⣶⡆⠛⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⠀⠀⣀⣀⣽⡋⠀⠀⠀⠀⠘⣏⠛⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠘⢿⡇⢰⣿⡗⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⣿⡟⡏⢷⠀⠀⠀⠀⣤⡘⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⠁⠰⣿⠿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣇⠃⠈⡆⠀⠀⠀⢹⡙⢿⡀⠀⠀⣀⣀⣠⣤⣤⡄⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢰⠃⠀⠀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⡏⠀⠀⢹⣄⣀⣀⣼⣷⣶⣾⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠈⠳⢴⣯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⣿⣀⣤⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡀⢀⣀⡤⠤⠄⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣏⡠⢤⣴⠄⠀⠀⠀⢀⣠⣤⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣄⠀⠛⢀⣠⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣸⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⣠⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣟⣿⣽⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿⣶⣄⠀⠀⠀
⠀⠀⠀⠀⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡄⠀
⠀⠀⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷
*/
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen(task ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | 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... |