# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
651692 | marche | Knapsack (NOI18_knapsack) | C++17 | 296 ms | 145284 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>
#pragma GCC optimize("O2")
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
using pi = pair<int,int>;
#define fr first
#define sc second
#define mp make_pair
void setIO(string name = "") {
cin.tie(0)->sync_with_stdio(0); // see /general/fast-io
if (sz(name)) {
freopen((name + ".in").c_str(), "r", stdin); // see /general/input-output
freopen((name + ".out").c_str(), "w", stdout);
}
}
int inf = 1000000;
int MOD = 1e9 + 7;
int main() {
setIO();
int s,n;
cin >> s >> n;
vector<vector<int>>a(n, vector<int>(3));
for (int i =0 ; i<n; i++){
for (int j = 0; j<3; j++)cin >> a[i][j];
}
unordered_map<int,int>weight;
function<bool(vector<int>a, vector<int>b)>sr = [&](vector<int>a,vector<int>b){
if (a[1] < b[1])return true;
else if (a[1] > b[1])return false;
else{
return a[0] > b[0];
}
};
sort(a.begin(), a.end(), sr);
vector<int>weights;
vector<int>vals;
for (int i = 0; i<n; i++){
int S = s/a[i][1] + 1;
int togo = S - weight[a[i][1]];
if (togo <= 0)continue;
int pushed = 0;
for (int j = 0; j<min(togo,a[i][2]); j++){
weights.pb(a[i][1]);
vals.pb(a[i][0]);
pushed++;
}
weight[a[i][1]]+=pushed;
}
int dp[weights.size()+1][2001];
memset(dp, 0, sizeof(dp));
for (int i = 1; i<=weights.size(); i++){
int w = weights[i - 1];
int v = vals[i - 1];
for (int j = 1; j<=s; j++){
dp[i][j] = dp[i-1][j];
if (j - w >= 0){
dp[i][j] = max(dp[i][j], dp[i-1][j - w] + v);
}
}
}
cout<<dp[weights.size()][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... |