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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define getar(ar,n) for(ll i=0;i<n;++i) cin>>ar[i]
#define show(n) cout<<n<<'\n'
#define all(v) v.begin(), v.end()
#define br cout<<"\n"
#define pb push_back
#define nl '\n'
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ret return
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
const int mxN = 1e5+10;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
vector<vector<int>> items;
int S,N;
ll dp[500][2001];
int f(int i=0,ll tot=0){
if(tot>S) return -INF;
if(i==N) return 0;
if(dp[i][tot]!=-1){
return dp[i][tot];
}
ll ans=-INF;
for(ll cpy=0;cpy<=items[i][2];++cpy){
// check for exceeding the total S
if(tot+cpy*items[i][1]<=S){
ans=max(ans,f(i+1,tot+cpy*items[i][1])+items[i][0]*cpy);
}
}
return dp[i][tot]=ans;
}
void solve(ll tc) {
int p,w,c;cin>>S>>N;
// price,weight,copy
for(ll i=0;i<N;++i){
cin>>p>>w>>c;
items.pb({p,w,c});
}
memset(dp,0,sizeof(dp));
show(f());
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve(0);
return 0;
}
Compilation message (stderr)
knapsack.cpp:28:39: warning: bad option '-fwhole-program' to pragma 'optimize' [-Wpragmas]
28 | #pragma GCC optimize("-fwhole-program")
| ^
knapsack.cpp:35:41: warning: bad option '-fstrict-overflow' to pragma 'optimize' [-Wpragmas]
35 | #pragma GCC optimize("-fstrict-overflow")
| ^
knapsack.cpp:37:41: warning: bad option '-fcse-skip-blocks' to pragma 'optimize' [-Wpragmas]
37 | #pragma GCC optimize("-fcse-skip-blocks")
| ^
knapsack.cpp:51:51: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
51 | #pragma GCC optimize("-funsafe-loop-optimizations")
| ^
knapsack.cpp:81:23: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
81 | int f(int i=0,ll tot=0){
| ^
knapsack.cpp:81:23: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
knapsack.cpp:81:23: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
knapsack.cpp:81:23: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
knapsack.cpp:98:17: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
98 | void solve(ll tc) {
| ^
knapsack.cpp:98:17: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
knapsack.cpp:98:17: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
knapsack.cpp:98:17: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
knapsack.cpp:109:10: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
109 | int main() {
| ^
knapsack.cpp:109:10: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
knapsack.cpp:109:10: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
knapsack.cpp:109:10: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
# | 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... |