| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1306637 | maximusaurelius | Knapsack (NOI18_knapsack) | C++20 | 117 ms | 128352 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;
using str = string;
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define f first
#define s second
#define endl "\n"
constexpr ll MOD = 1000000007;
void setIO(string name = "") {
ios::sync_with_stdio(0); cin.tie(0);
if (sz(name)) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
int main() {
setIO();
int S; cin >> S;
int n; cin >> n;
vector<vi> a_or(n);
vector<vi> a1;
for (int i = 0; i < n; i++)
{
int v, w, k; cin >> v >> w >> k;
a_or[i] = {w,-v,k};
}
sort(all(a_or));
a_or.pb({100000000,0,0});
int cur_w = a_or[0][0];
int cur_added = 0;
// evtl doppelte? schlechter?
for (int i = 0; i < n; i++)
{
if (a_or[i][0] > cur_w) {
cur_w=a_or[i][0];
cur_added=0;
}
if (cur_w == 100000000) break;
if (cur_added+a_or[i][2] <= S / cur_w) {
a1.pb({a_or[i][0],a_or[i][1],a_or[i][2]});
cur_added+=a_or[i][2];
} else if (S / cur_w - cur_added > 0){
a1.pb({a_or[i][0],a_or[i][1],S / cur_w - cur_added});
cur_added+=S / cur_w - cur_added;
}
}
vector<ii> test1;
for (const auto& aa: a1) {
for (int i = 0; i < aa[2]; i++)
{
test1.pb(mp(aa[0],-aa[1]));
}
}
//dp
vector<vi> dp(sz(test1)+1, vi(S+1,0));
for (int i = 0; i < sz(test1); i++)
{
for (int j = S; j >= 0; j--)
{
dp[i+1][j] = dp[i][j];
if (j - test1[i].f >= 0)dp[i+1][j] = max(dp[i+1][j],dp[i][j-test1[i].f]+test1[i].s);
}
}
cout << dp[sz(test1)][S] << endl;
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... | ||||
