# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
574413 | IWannaHaveAGirlfriend | Knapsack (NOI18_knapsack) | C++17 | 69 ms | 4140 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.
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 1e5, M = 2e3;
vector<int> keep[M + 1];
int v[N + 1], w[N + 1], k[N + 1], id[N + 1], s, n;
long long dp[N + 1];
void read()
{
cin >> s >> n;
for (int i = 1; i <= n; ++ i)
{
cin >> v[i] >> w[i] >> k[i];
id[i] = i;
}
sort(id + 1, id + n + 1, [&](const int &x, const int &y)
{
return v[x] > v[y];
});
}
void solve()
{
for (int j = 1; j <= n; ++ j)
{
int i = id[j];
while(k[i]--)
{
if (keep[w[i]].size() == s/w[i])
{
break;
}
keep[w[i]].push_back(v[i]);
}
}
for (int i = 1; i <= s; ++ i)
{
if (keep[i].empty())
{
continue;
}
for (int j = s; j >= 0; -- j)
{
int x = j;
long long sum = 0;
for (int l : keep[i])
{
x += i;
sum += l;
if (x > s)
{
break;
}
dp[x] = max(dp[x], dp[j] + sum);
}
}
for (int j = 1; j <= s; ++ j)
{
dp[j] = max(dp[j], dp[j - 1]);
}
}
cout << dp[s];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
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... |