#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define REP(i,a,b) for(int i=(a); i>=(b); --i)
#define umax(x, y) x = max(x, (y))
#define umin(x, y) x = min(x, (y))
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define sz(v) (int)(v).size()
#define ALL(v) (v).begin(),(v).end()
#define el cout << "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int base = 311;
const ll INF = 1e18;
/// REXDINO FROM LTV HSGS ///
int S, n;
int v[N], w[N], k[N];
vector<int> order;
int used[N];
ll dp[N];
bool cmp(int x, int y){
return v[x] > v[y];
}
void solve(){
cin >> S >> n;
FOR(i, 1, n) cin >> v[i] >> w[i] >> k[i], order.push_back(i);
sort(ALL(order), cmp);
for (int i : order){
FOR(j, 1, k[i]) {
if (used[w[i]] * w[i] > S) break;
REP(t, S, w[i]){
umax(dp[t], dp[t - w[i]] + v[i]);
}
used[w[i]]++;
}
}
cout << dp[S], el;
}
signed main(){
#define NAME "main"
if (fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen(NAME".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... |