# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
688586 | koolaider | Knapsack (NOI18_knapsack) | C++17 | 3 ms | 596 KiB |
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 <cstdlib>
using namespace std;
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl
#define pb push_back
#define ll long long
const int MN = 1e5+2;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
/*
a b y x b
a 1 1 1 1 1
x 1 1 1 2 2
y 1 1 2 2 2
b 1 2 2 2 3
*/
// #pragma GCC target ("avx,avx2")
// #pragma GCC optimize ("Ofast")
//DSUFlowHyRuthTh!
//const long long ML = 1e18;
//const int MN = 1e9;
const int MM = 1e9+7;
int main(int argc, char **argv){
ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL);
//setIO("time");
int s, n; cin >> s >> n;
int v, w, k;
vector<int> values; values.reserve(1e14+100);
vector<int> weights; weights.reserve(1e14+100);
for(int i = 0; i<n; i++){
cin >> v >> w >> k;
for(int j = 0; j<k; j++){
values.emplace_back(v); weights.emplace_back(w);
}
}
int amount = values.size();
ll dp[2+1][s+1]; memset(dp, 0, sizeof(dp));
bool f = true;
for(int y = 1; y<=amount; y++){
for(int x = 1; x<=s; x++){
if(f==true){
if(x-weights[y-1]>=0) dp[1][x]=dp[2][x-weights[y-1]]+values[y-1];
dp[1][x]=max(dp[1][x], dp[2][x]);
}else{
if(x-weights[y-1]>=0) dp[2][x]=dp[1][x-weights[y-1]]+values[y-1];
dp[2][x]=max(dp[2][x], dp[1][x]);
}
}
/*
for(int x = 1; x<=s; x++){
if(f==true){
dp[2][x]=dp[1][x];
}else{
dp[0][x]=dp[2][x];
}
}
*/
f=!f;
}
cout << max(dp[2][s], dp[1][s]);
return 0;
}
Compilation message (stderr)
# | 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... |