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>
#include <iostream>
using namespace std;
typedef long long ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
const int mxN = 2005;
const int mod = 1e9+7;
const int mxlogN = 20;
const int mxK = 10;
vector<pair<int, int>> a[mxN];
ll sum[mxN];
ll dp[mxN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int S, n; cin >> S >> n;
for(int i = 0; i < n; i++)
{
int V, W, K; cin >> V >> W >> K;
a[W].push_back({V, K});
}
vector<pair<int, int>> items; ///W V
for(int i = 1; i <= S; i++)
{
sort(a[i].begin(), a[i].end());
while(sum[i] < S && a[i].size())
{
while(sum[i] < S && a[i].back().second)
{
sum[i] += i;
a[i].back().second--;
items.push_back({i, a[i].back().first});
}
a[i].pop_back();
}
}
for(int i = 0; i < items.size(); i++)
{
for(int j = S; j >= items[i].first; j--)
{
if(items[i].second+dp[j-items[i].first] > dp[j])
{
dp[j] = items[i].second+dp[j-items[i].first];
}
}
}
cout << dp[S];
}
/**
1
8
1 0 1 1 0 1 1 1
*/
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:46:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i = 0; i < items.size(); i++)
| ~~^~~~~~~~~~~~~~
# | 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... |