#include <bits/stdc++.h>
// Author: Kazuki_Will_Win_VOI_8703
#define fi first
#define se second
#define pii pair<int, int>
#define int long long
#define all(a) a.begin(), a.end()
using namespace std;
const int mn = 5e5 + 5, bm = (1 << 11) + 1, mod = 1e9 + 7, offset = 5e4, B = 320 + 5;
const int inf = 1e9, base = 311;
int n, m, w[mn], v[mn], a[mn];
vector <pii> adj[mn];
vector <int> val[mn];
int dp[mn], tmp[mn];
void solve(){
cin >> m >> n;
int max_val = 0;
for(int i = 1; i <= n; i++){
cin >> v[i] >> w[i] >> a[i];
adj[w[i]].push_back({v[i], a[i]});
max_val = max(w[i], max_val);
}
for(int i = 1; i <= max_val; i++){
if(adj[i].empty()) continue;
sort(adj[i].begin(), adj[i].end());
int num = 0, cur = 0;
bool ok = true;
val[i].push_back(0);
while(!adj[i].empty() && ok){
int sl = adj[i].back().se;
int gia = adj[i].back().fi;
adj[i].pop_back();
for(int x = 1; x <= sl; x++){
num ++;
cur += gia;
val[i].push_back(cur);
if(num == m / i){
ok = false;
break;
}
}
}
}
for(int i = 1; i <= max_val; i++){
if(val[i].size() <= 1) continue;
for(int j = 1; j <= m; j++){
tmp[j] = dp[j];
}
for(int j = val[i].size() - 1; j >= 1; j--){
int kl = i * j;
int gia = val[i][j];
for(int ii = kl; ii <= m; ii++){
tmp[ii] = max(tmp[ii], dp[ii - kl] + gia);
}
}
for(int j = 1; j <= m; j++){
dp[j] = tmp[j];
}
}
int res = 0;
for(int i = 1; i <= m; i++){
res = max(res, dp[i]);
}
cout << res;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
while(t --){
solve();
}
}
# | 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... |