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 ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
const int nmax = 3e5 + 1;
int main(){
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int s, n; cin >> s >> n;
pair<ll, pii> a[n + 1];
multiset <ll> st[s + 1];
for(int i = 1; i <= n; i++){
cin >> a[i].f >> a[i].s.f >> a[i].s.s;
}
sort(a + 1, a + 1 + n);
reverse(a + 1, a + 1 + n);
vector <vector <ll> > vec(s + 1);
for(int i = 0; i <= s; i++)
vec[i].pb(0);
for(int i = 1; i <= n; i++){
int k = a[i].s.s, w = a[i].s.f;
for(int j = 1; j <= k; j++){
if(vec[w].size() > 2000) break;
vec[w].pb(vec[w].back() + a[i].f);
}
}
ll dp[s + 1];
fill(dp, dp + s + 1, 0);
dp[0] = 0;
for(int i = 1; i <= s; i++){
for(int j = s; j >= 0; j--){
ll cnt = -1;
for(int l = j; l >= 0; l -= i){
cnt++;
if(cnt == vec[i].size()) break;
dp[j] = max(dp[j], dp[l] + vec[i][cnt]);
}
}
}
ll mx = 0;
for(int i = 1; i <= s; i++)
mx = max(mx, dp[i]);
cout << mx;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:45:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | if(cnt == vec[i].size()) break;
| ~~~~^~~~~~~~~~~~~~~~
# | 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... |