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>
using namespace std;
// type
using ll = long long;
using ld = long double;
using db = double;
using str = string;
// pairs
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
// vector
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define sor(x) sort(all(x))
#define rsz resize
#define rtn return
// binary search
#define lb lower_bound
#define ub upper_bound
template<class T> ll lwb(vector<T>& a, const T& b) { rtn lb(all(a), b) - a.begin(); }
template<class T> ll upb(vector<T>& a, const T& b) { rtn ub(all(a), b) - a.begin(); }
// pairs
#define mp make_pair
#define f first
#define s second
// loops
#define FOR(i,a,b) for (ll i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (ll i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define each(a,x) for (auto& a: x)
#define cont continue
#define bre break
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s) {
setIn(s+".inp"); setOut(s+".out");
}
const int MOD = 1e9 + 7; //998244353;
const int MX = 2e5+5;
const ll INF = 1e15;
const db PI = acos((db)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
// modulo, exponentiation, inverse
ll pow_mod(ll a, ll b, ll m) { ll res = 1; while(b){ res = res * (b & 1 ? a : 1) % m; a = a * a % m; b >>= 1; } rtn res; }
ll inv(ll a, ll m) { rtn pow_mod(a, m - 2, m); }
ll nxt(void){ // fast way for cin operation
ll x; cin >> x;
rtn x;
}
#define MXN 100001
ll W, N, res = -INF, v[MXN], w[MXN], k[MXN], dp[2][2001];
void init(void){
cin >> W >> N;
F0R(i, N) cin >> v[i] >> w[i] >> k[i];
F0R(i, W + 1) dp[0][i] = -INF;
dp[0][0] = 0;
}
void solve(void){
F0R(i, N){
FOR(copies, 1, k[i] + 1)
F0R(j, W + 1){
if(copies == 1) dp[1][j] = dp[0][j];
if(j >= copies * w[i] && dp[0][j - copies * w[i]] != -INF)
dp[1][j] = max(dp[1][j], dp[0][j - copies * w[i]] + copies * v[i]);
}
F0R(j, W + 1)
swap(dp[0][j], dp[1][j]);
}
F0R(i, W + 1)
res = max(res, dp[0][i]);
cout << res;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
// setIO("CodeforcesEasy");
init();
solve();
rtn 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void setIn(str)':
knapsack.cpp:54:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
knapsack.cpp: In function 'void setOut(str)':
knapsack.cpp:55:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |