This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Author:Matkap(prefix_sum)
*/
#include <bits/stdc++.h>
#include <array>
using namespace std;
#define int long long
#define endl "\n"
#define ar array
#define all(x) x.begin(),x.end()
const int INF = 1e17 , MOD = 1e9 + 7;
void setIO(string name = "")
{
if (name.size())
{
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
int mul(int a,int b,int mod = MOD)
{
a %= mod;
b %= mod;
return a * 1LL * b % mod;
}
int sum(int a,int b,int mod = MOD)
{
a %= mod;
b %= mod;
return (a + b + mod) % mod;
}
int binpow(int base,int power,int mod = MOD)
{
if(power == 1) return base;
if(power == 0) return 1;
if(power%2==1)
{
int a;
a = binpow(base,(power - 1)/2);
return mul(base, mul(a, a, mod), mod);
}
else
{
int a;
a = binpow(base,power/2);
return mul(a, a, mod);
}
}
int inv(int a,int mod = MOD)
{
a %= mod;
return binpow(a, mod - 2) % mod;
}
void solve()
{
int n,s;
cin >> s >> n;
vector<int> dp(s + 1, -INF), ndp(s + 1);
vector<ar<int,2>> weights[s + 1];
dp[0] = 0;
for(int i = 0;n > i;i++)
{
ndp = dp;
int value, weight, count;
cin >> value >> weight >> count;
weights[weight].push_back({value,count});
}
for(auto& it : weights) sort(all(it));
for(int i = 1;s >= i;i++)
{
ndp = dp;
int used_weight = 0, total_value = 0;
while(weights[i].size())
{
auto& [value, count] = weights[i].back();
used_weight += i;
total_value += value;
for(int j = used_weight;s >= j;j++)
{
ndp[j] = max(ndp[j], dp[j - used_weight] + total_value);
}
count--;
if(count == 0) weights[i].pop_back();
}
dp = ndp;
}
cout << *max_element(all(dp)) << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt;
tt=1;
//cin >> tt;
while(tt--) solve();
}
Compilation message (stderr)
knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |