# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
869895 | Karnis_052 | Knapsack (NOI18_knapsack) | C++17 | 45 ms | 4948 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.
// Bismillahir Rahmanir Rahim
/// It is a oj.uz oj's problem
// problem link https://oj.uz/problem/view/NOI18_knapsack
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int>PI;
typedef pair<ll, ll > PL;
typedef vector<int>VI;
typedef vector<ll>VL;
#define FF first
#define SS second
#define sz size()
const int mod = 1e9 + 7;
const int INF = 1e9;
const int N = 2e3 + 5;
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll bag, n, value, weight, k;
cin >> bag >> n;
vector<PL>items[N];
for (int i = 0; i < n; i++)
{
cin >> value >> weight >> k;
items[weight].push_back({value, k});
}
vector<ll>dp(N);
dp[0] = 0;
for (ll i = 1; i < N; i++)
{
if (items[i].size() == 0)continue;
sort(items[i].rbegin(), items[i].rend());
ll coun = 0, it = 0;
while (coun * i <= bag and it < items[i].size())
{
int picked = 0;
while (coun * i <= bag and picked < items[i][it].SS)
{
for (ll j = bag; j >= i; j--)
dp[j] = max(dp[j], dp[j - i] + items[i][it].FF);
coun++;
picked++;
}
it++;
}
}
ll ans = 0;
for (int i = 0; i < N; i++)
ans = max(ans, dp[i]);
cout << ans << '\n';
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << 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... |