# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
593502 | wyhong3103 | Knapsack (NOI18_knapsack) | C++14 | 2 ms | 1876 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 <bits/stdc++.h>
#define all(x) begin(x),end(x)
#define fir first
#define sec second
#define sz(x) x.size()
#define pb push_back
using namespace std;
using vi = vector<int>;
using pi = pair<int,int>;
using pdb = pair<double,double>;
using ll = long long;
using pll = pair<ll,ll>;
const double EPS = (1e-7);
void setio(string s){
freopen((s + ".in").c_str(),"r",stdin);
freopen((s + ".out").c_str(),"w",stdout);
}
const int MXN = 1e5 + 5;
const int MXS = 2001;
//{val, k used}
pair<int,int> dp[MXN][MXS];
struct Item{
int v, w, k;
};
void solve(){
int n, s;
cin >> s >> n;
vector<Item> items(n);
for(auto& i : items) cin >> i.v >> i.w >> i.k;
for(int i{1}; i <= n; i++){
for(int j{1}; j <= s; j++){
//based on last
dp[i][j].fir = max(dp[i-1][j].fir, dp[i][j].fir);
if (j-items[i-1].w >= 0 && dp[i-1][j-items[i-1].w].fir + items[i-1].v > dp[i][j].fir){
dp[i][j].fir = dp[i-1][j-items[i-1].w].fir + items[i-1].v;
dp[i][j].sec = 1;
}
//based on cur
if (j-items[i-1].w >= 0 && dp[i][j-items[i-1].w].fir + items[i-1].v > dp[i][j].fir && dp[i][j-items[i-1].w].sec + 1 <= items[i-1].k){
dp[i][j].fir = dp[i][j-items[i-1].w].fir + items[i-1].v;
dp[i][j].sec = dp[i][j-items[i-1].w].sec + 1;
}
}
}
cout << dp[n][s].fir;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
while(t--){
solve();
}
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... |