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>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define vi vector<int>
#define vl vector<long long>
#define vs vector<string>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(ll i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(ll i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define ub upper_bound
#define lb lower_bound
const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 1;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
// HERE IS THE SOLUTION
int main()
{
cin.tie(0);
cout.tie(0);
boost;
int s , n;
cin>>s>>n;
vl val(n) , weight(n) , k(n);
map<ll ,vll> weights;
forr(i, 1 , n)
{
ll a , b, c;
cin>>a>>b>>c;
weights[b].pb({a , c});
}
vector<vl> dp(s+1 , vl(sz(weights)+1 , INT32_MIN));
int i = 0;
dp[0][0] = 0;
for(auto p : weights)
{
i++;
vll val = p.se;
sort(val.rbegin() , val.rend());
ll y = p.fi;
forr(w , 0 , s)
{
dp[w][i] = dp[w][i-1];
ll prev = 0;
ll j = 0;
ll x = 1;
ll used = 1;
while(x*y<=w&&j<sz(val))
{
int k = val[j].se , v = val[j].fi;
if(used<=k)
{
prev+=v;
if(dp[w - x*y][i-1]!=INT32_MIN)
dp[w][i] = max(dp[w][i] , dp[w - x*y][i-1]+prev);
}
if(used == k)
{
used = 0;
j++;
}
used++;
x++;
}
}
}
ll ans = INT32_MIN;
fore(i , s+1)
{
ans = max(ans , dp[i][sz(weights)]);
}
cout<<ans<<nl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:66:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | while(x*y<=w&&j<sz(val))
| ^
# | 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... |