This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,tune=native")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
//template <typename T>
//using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef pair<int,int> pii;
const int arr = 2e3+1;
const ll mod = 1e9+7;
const ll maxv = 1e18+1;
#define int long long
#define fi first
#define se second
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define no "NO\n"
#define yes "YES\n"
#define ld long double
#define mat vector<vector<ll>>
int s, n, dp[30001];
vector<pii> a[arr];
bool cmp(pii c, pii d){
return (c.fi > d.fi);
}
void read(){
cin >> s >> n;
for(int i = 1; i <= n; ++i){
int v, w, k; cin >> v >> w >> k;
a[w].pb({v, k});
}
for(int i = 1; i <= s; ++i)
sort(all(a[i]), cmp);
}
void solve(){
int ans = 0;
for(int w = 1; w <= s; ++w){
int cur = 0;
if(!a[w].size()) continue;
for(int i = 1; i <= s/w; ++i){
if(cur >= a[w].size()) break;
int v = a[w][cur].fi;
for(int j = s; j >= w; --j)
dp[j] = max(dp[j], dp[j-w]+v);
a[w][cur].se--;
if(a[w][cur].se == 0) ++cur;
}
}
for(int i = 1; i <= s; ++i) ans = max(ans, dp[i]);
cout << ans;
}
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
read();
solve();
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:50:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | if(cur >= a[w].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... |