This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1000000007;
typedef long long ll;
typedef long double ld;
#define sz(s) ((int)s.size())
#define all(v) begin(v), end(v)
#define ff first
#define ss second
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
// *-> KISS*
int solve() {
ll s, n; cin >> s >> n;
map<int, vector<pair<ll, ll>>> mp;
for(int i = 0; i < n; ++i) {
ll v, w, k; cin >> v >> w >> k;
mp[w].push_back(pair(v, k));
}
for(auto& [w, v]: mp) sort(v.begin(), v.end(), greater<pair<ll, ll>>());
map<int, vector<ll>> order;
for(auto& [w, v]: mp) {
vector<ll> temp;
int elem = s / w;
int si = sz(v), i = 0, in = 0;
while(i + 1 <= elem && in != si) {
temp.push_back(v[in].ff);
++i;
v[in].ss--;
if(v[in].ss == 0) ++in;
}
order[w] = temp;
}
vector<vector<ll>> dp(sz(order) + 1, vector<ll>(s + 1, 0));
dp[0][0] = 0;
int in = 1;
for(auto& [w, v]: order) {
// v -> vector
// w -> weight
for(int j = 1; j <= s; ++j) {
dp[in][j] = dp[in - 1][j];
ll ex = 0;
for(int i = 0; i < sz(v); ++i) {
ex += v[i];
if(j - (i + 1) * w < 0) break;
dp[in][j] = max(dp[in][j], ex + dp[in - 1][j - (i + 1) * w]);
}
}
++in;
}
cout << dp[sz(order)][s];
return 0;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
bool test = false;
int TET = 1;
if(test) cin >> TET;
cout << fixed << setprecision(6);
for (int i = 1; i <= TET; i++) {
#ifdef LOCAL
cout << "##################" << '\n';
#endif
if (solve()) {
break;
}
cout << '\n';
}
#ifdef LOCAL
cout << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
return 0;
}
// -> Keep It Simple Stupid!
# | 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... |