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);
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));
}
vector<ll> prev(s+1,LLONG_MIN),nxt(s+1,LLONG_MIN);
ll p=-1;
vector<pair<ll,ll>> vec;
for(ll ind = 1; ind<=s;ind++){
if( trk[ind].empty() ) continue;
prev[0]=0;
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 ){
nxt[sum]=prof;
prof+=(val);
sum+=(ind);
}
prof-=val;
sum-=ind;
}
}
else{
ll sz=trk[ind].size();
vec.resize(sz);
ll x=0;
while( !trk[ind].empty() ){
vec[x]=(trk[ind].top());
x++;
trk[ind].pop();
}
for(ll sum=0;sum<=s;sum++){
ll notake = prev[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( prev[ sum - sm ] != LLONG_MIN ){
take=max(take,prof+prev[sum-sm]);
}
prof+=val;
sm+=ind;
}
prof -= val;
sm -= ind;
i++;
}
nxt[sum]=max(notake,take);
}
}
p=ind;
prev=nxt;
}
ll maxprof=0;
for(ll i=0;i<=s;i++){
if( prev[i] != LLONG_MIN)maxprof=max(maxprof,prev[i]);
}
std::cout<<maxprof<<endl;
return 0;
}
# | 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... |