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 <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <tuple>
#include <numeric>
#include <climits>
#include <bitset>
#include <iomanip>
#include <random>
#include <ctime>
using namespace std;
//change the long long to int if you need to save memory/time really badly
typedef long long ll;
//Comment this define when working on interactive problems
#define endl "\n"
#define sqrt(n) sqrt((long double) n)
const ll MAXN = 2e5 + 5;
const ll ZER = 0;
const ll ONE = 1;
const ll INF = LLONG_MAX;
const ll MOD = 1e9 + 7;
ll min(ll a, ll b) {
if (a < b) {
return a;
} else {
return b;
}
}
ll max(ll a, ll b) {
if (a > b) {
return a;
} else {
return b;
}
}
ll mod(ll num) {
return (num >= MOD ? num % MOD : num);
}
ll __gcd(ll a, ll b) {
if (b == 0) {
return a;
}
if (a == 0) {
return b;
}
if (a < b) {
swap(a, b);
}
return __gcd(b, a % b);
}
ll __lcm(ll a, ll b) {
ll prod = a*b;
return prod/__gcd(a, b);
}
ll powmod(ll a, ll b){
a %= MOD;
if (a == 0) return 0;
ll product = 1;
while(b > 0){
if (b&1){ // you can also use b % 2 == 1
product *= a;
product %= MOD;
--b;
}
a *= a;
a %= MOD;
b /= 2; // you can also use b >> 1
}
return product;
}
//calculates the inverse of a number mod MOD
ll inv(ll a) {
return powmod(a, MOD - 2);
}
void solve(ll ca)
{
ll s, n;
cin >> s >> n;
vector<pair<ll, ll>> items[s+1]; //list of items with the same weight, sorted by the value
for (ll i = 0; i < n; i++) {
ll v, w, k;
cin >> v >> w >> k;
items[w].push_back({v, k});
}
vector<ll> weights[s+1];
for (ll i = 0; i <= s; i++) {
if (items[i].size() == 0) {
continue;
}
sort(items[i].begin(), items[i].end(), greater<>());
ll cur = 0;
for (ll j = 0; j < items[i].size(); j++) {
ll k = 0;
while (cur+i <= s && k < items[i][j].second) {
weights[i].push_back(items[i][j].first);
k++;
cur += i;
}
if (cur+i > s) {
break;
}
}
}
ll dp[s+1]; //dp[i] is the maximum value you can get with a weight of i
for (ll i = 0; i <= s; i++) {
dp[i] = -1;
}
dp[0] = 0;
for (ll we = 0; we <= s; we++) {
if (weights[we].size() == 0) {
continue;
}
for (ll i = s; i >= 0; i--) {
if (dp[i] != -1) {
ll cur = i; ll csum = 0; ll ind = 0;
while (cur+we <= s && ind < weights[we].size()) {
dp[cur+we] = max(dp[cur+we], dp[i]+csum+weights[we][ind]);
csum += weights[we][ind];
cur += we;
ind++;
}
}
}
}
ll ans = 0;
for (ll i = 0; i <= s; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
return;
}
int main()
{
//mt19937 rng(0);
//Fast IO
ios::sync_with_stdio(false);
cin.tie(nullptr);
/*
freopen("feast.in", "r", stdin);
freopen("feast.out", "w", stdout);
*/
ll t = 1;
//cin >> t;
ll co = 1;
while (t--) {
solve(co);
++co;
}
}
//Always write your algorithm down on a piece of paper and question EACH and EVERY step if you aren't able to find an implementation mistake in your code.
Compilation message (stderr)
knapsack.cpp: In function 'void solve(ll)':
knapsack.cpp:123:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (ll j = 0; j < items[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~
knapsack.cpp:150:43: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | while (cur+we <= s && ind < weights[we].size()) {
| ~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |