This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 4;
const int oo = 1e18;
const int mod = 1e9 + 7;
int t, n, q, m, k, a[N], ans, w, cnt, cur, dem, coun, profit;
map<int, vector<pair<int, int>>>mp;
vector<pair<int, int>>v;
vector<vector<int>>dp;
bool cmp(pair<int, int>a, pair<int, int>b){
return a.first > b.first;
}
void solve(){
cnt = 1;
cin >> k >> n;
for (int i = 1; i <= n; i++){
int x, y, z; cin >> x >> y >> z;
if (y <= k)mp[y].push_back({x, z});
}
dp.resize(mp.size() + 5, vector<int>(k + 5, -1));
dp[0][0] = 0;
for (auto p : mp){
w = p.first, v = p.second;
sort(v.begin(), v.end(), cmp);
for (int i = 0; i <= k; i++){
dem = coun = 0, cur = 1;
dp[cnt][i] = dp[cnt - 1][i];
profit = 0;
while (cur * w <= i && dem < v.size()){
profit += v[dem].first;
if (dp[cnt - 1][i - cur * w] != -1)dp[cnt][i] = max(dp[cnt][i], dp[cnt - 1][i - cur * w] + profit);
cur++;
coun++;
if (coun == v[dem].second){
coun = 0;
dem++;
}
}
}
cnt++;
}
for (int i = 0; i <= mp.size(); i++){
for (int j = 0; j <= k; j++)ans = max(ans, dp[i][j]);
}
cout << ans;
}
main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("clocktree.in", "r", stdin); freopen("clocktree.out", "w", stdout);
t = 1;
//cin >> t;
while (t--)solve();
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:31:40: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | while (cur * w <= i && dem < v.size()){
| ~~~~^~~~~~~~~~
knapsack.cpp:44:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::map<long long int, std::vector<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i <= mp.size(); i++){
| ~~^~~~~~~~~~~~
knapsack.cpp: At global scope:
knapsack.cpp:49:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
49 | main() {
| ^~~~
# | 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... |