# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
464295 | vbee | Knapsack (NOI18_knapsack) | C++14 | 71 ms | 4820 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>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ii pair<int,int>
#define vii vector<ii>
#define vi vector<int>
#define fi first
#define se second
#define TASK ""
#define ll long long
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define pb push_back
#define MASK(i) (1 << (i))
#define BIT(x, i) ((x >> (i)) & 1)
using namespace std;
const int oo = 1e9 + 7;
const ll loo = (ll)1e18 + 7;
const int N = 1e5 + 1;
const int S = 2007;
int dp[S], w[N], v[N], k[N], n, s;
void maximize(int &a, int b){
if (a < b) a = b;
}
vii obj[S];
int main() {
scanf("%d%d", &s, &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d%d", &v[i], &w[i], &k[i]);
obj[w[i]].pb({v[i], k[i]});
}
for (int i = 0; i <= s; i++){
if (obj[i].size() == 0) continue;
sort(all(obj[i]), greater<ii>());
int id = 0;
for (int j = 0; j * i < s; j++){
if (id >= obj[i].size()) break;
for (int k = s; k >= i; k--){
maximize(dp[k], dp[k - i] + obj[i][id].fi);
}
if (--obj[i][id].se == 0) id++;
}
}
cout << dp[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... |