# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
942485 | AtabayRajabli | Knapsack (NOI18_knapsack) | C++17 | 0 ms | 0 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 <bits/stdc++.h>
// author : a1abay
#define all(v) v.begin(), v.end()
#define GCD(a, b) __gcd(a, b)
#define int ll
#define LCM(a, b) (a*b / (__gcd(a, b)))
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
const int inf = 1e9 + 7;
const int inff = (int)1e18 + 7;
const int sz = 1e5 + 5;
using namespace std;
int s, n;
int v[sz], w[sz], f[sz], dp[sz];
vector<array<int, 2>> g[sz];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("in.in", "r", stdin); freopen("out.out", "w", stdout);
cin >> s >> n;
for(int i = 1; i <= n; i++)
{
cin >> v[i] >> w[i] >> f[i];
g[w[i]].push_back({v[i], f[i]});
}
for(int i = 1; i <= s; i++)
{
sort(all(g[i]));
int mx = s / i;
while(!g[i].empty())
{
int x = g[i].back()[1], v = g[i].back()[0];
if(mx - x < 0)break;
g[i].pop_back();
for(int j = 1; j <= x; j++)
{
for(int k = s; k >= i; k--)dp[k] = max(dp[k - i] + v, dp[k]);
}
}
for(int j = 1; j <= mx; j++)
{
for(int k = s; k >= i; k--)dp[k] = max(dp[k - i] + v, dp[k]);
}
}
int mx = -1;
for(int i = 0; i <= m; i++)mx = max(mx, dp[i]);
cout << mx;
}