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>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <class T>
using indexed_set=tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update>;
const long long M = 1000000007;
#define multiply(a,b) ((a % M ) * (b % M)) % M
#define all(a) (a).begin(), (a).end()
#define ll long long
#define ld long double
#define max(a,b)( a > b ? a : b )
#define loop(i,a,b) for(long long i=a;i<b;i++)
#define rloop(i,a,b) for(int i=a;i>=b;i--)
#define logarr(arr,a,b) for(int i=a;i<b;i++) cout<<arr[i]<<" "; cout<<endl
#define vi vector<int>
#define pi pair<int,int>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define p push
#define po pop
#define gcd(a,b) __gcd(a,b)
#define yes "YES\n"
#define no "NO\n"
// find_by_order -> iterator to value
// order_of_key -> index where value is/will be
long long binary_expo(long long x, long long n) {
if (n == 0) return 1;
if (n == 1) return x % M;
long long temp = binary_expo(x, n / 2);
temp = multiply(temp, temp);
if (n % 2) temp = multiply(temp, x);
return temp;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n,s;
cin>>s>>n;
vector<priority_queue<pair<ll,ll>>>trk(s+1);
ll x,y,z;
loop(i,0,n){
cin>>x>>y>>z;
trk[y].push(make_pair(x,z));
}
const int up = s+1;
vector<vector<ll>> dp(s+1,vector<ll>(s+1,LLONG_MIN));
ll p=-1;
vector<pair<ll,ll>> vec;
for(ll ind = 1; ind<=s;ind++){
dp[ind][0]=0;
if( trk[ind].empty() ) continue;
if( p == -1 ){
//base case .....
ll sum=0,prof=0;
while( !trk[ind].empty() ){
ll val =trk[ind].top().first;
ll time= trk[ind].top().second;
trk[ind].pop();
prof+=val;
sum+=ind;
while( time-- && sum <=s ){
dp[ind][sum]=prof;
prof+=(val);
sum+=(ind);
}
prof-=val;
sum-=ind;
}
}
else{
int sz=trk[ind].size();
vec.resize(sz);
int x=0;
while( !trk[ind].empty() ){
vec[x]=(trk[ind].top());
x++;
trk[ind].pop();
}
for(ll sum=0;sum<=s;sum++){
ll notake = dp[p][sum];
ll take = LLONG_MIN;
ll sm = 0,prof=0,i=0;
while( i < sz && sum - sm >=0 ){
ll val =vec[i].first;
ll time=vec[i].second;
prof+=val;
sm+=ind;
while( time-- && sum - sm >= 0 ){
if( dp[p][ sum - sm ] != LLONG_MIN ){
take=max(take,prof+dp[p][sum-sm]);
}
prof+=val;
sm+=ind;
}
i++;
}
dp[ind][sum]=max(notake,take);
}
}
p=ind;
}
ll maxprof=0;
for(ll i=0;i<=s;i++){
if( dp[p][i] != LLONG_MIN)maxprof=max(maxprof,dp[p][i]);
}
std::cout<<maxprof<<endl;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:54:15: warning: unused variable 'up' [-Wunused-variable]
54 | const int up = s+1;
| ^~
knapsack.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:44:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen("output.txt", "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... |