# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
680799 | MilosMilutinovic | Knapsack (NOI18_knapsack) | C++14 | 55 ms | 4364 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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;
typedef long long ll;
const int N = 2020;
const int M = N * N;
int s, n;
int v[M];
int w[M];
int k[M];
ll dp[N];
pair<int, int> b[M];
vector<int> f[N];
int ord[M];
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%d%d", &s, &n);
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &v[i], &w[i], &k[i]);
f[w[i]].push_back(i);
}
int m = 0;
for (int i = 1; i <= s; i++) {
int sz = (int)f[i].size();
for (int j = 0; j < sz; j++)
ord[j] = f[i][j];
sort(ord, ord + sz, [&](int i, int j) { return v[i] > v[j]; });
int curS = 0;
for (int j = 0; j < sz; j++) {
while(k[ord[j]] > 0 && curS + i <= s) {
b[m++] = make_pair(v[ord[j]], i);
k[ord[j]]--;
curS += i;
}
}
}
for (int i = 0; i < m; i++) {
int V = b[i].first;
int W = b[i].second;
for (int j = s - W; j >= 0; j--)
dp[j + W] = max(dp[j + W], dp[j] + V);
}
printf("%lld\n", 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... |