//
// All truth are easy to understand once they are discovered.
// The point is to discover them
//
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 123;
void solve(){
int n, S; cin >> S >> n;
int v[n], w[n], k[n];
vector<pair<long long, int>> weights[S+1];
for (int i = 0; i < n; i++) {
cin >> v[i] >> w[i] >> k[i];
weights[w[i]].push_back({v[i], k[i]});
}
for (int i = 1; i < S+1; i++) {
sort(weights[i].begin(), weights[i].end(), greater());
weights[i].resize(min((int)weights[i].size(), S / i));
}
long long dp[S+1];
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for (int i = 1; i <= S; i++) {
int cnt = 0;
for (auto [val, times] : weights[i]) {
for (int j = 0; j < times; j++) {
if (cnt == S / i) break;
for (int x = S; x >= i; x--) {
if (dp[x - i] != -1) dp[x] = max(dp[x - i] + val, dp[x]);
}
cnt++;
}
if (cnt == S / i) break;
}
}
long long ans = 0;
for (int i = 0; i <= S; i++) {
ans = max(ans, dp[i]);
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
cout << '\n';
}
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... |