# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
771330 | Ahmed57 | Salesman (IOI09_salesman) | C++17 | 299 ms | 35520 KiB |
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>
using namespace std;
#define int long long
//BIT Fenwick tree
struct fenwick{
long long bit[500011]={0} , n = 500010;
void build(){
for(int i = 0;i<=500010;i++)bit[i] = -1e13;
}
void add(int e,long long v){
while(e<=n){
bit[e]=max(bit[e],v);
e+=e&-e;
}
}
long long sum(int e){
long long res = -1e13;
while(e>=1){
res=max(res,bit[e]);
e -= e&-e;
}
return res;
}
};
fenwick s1,s2;
long long n,u,d,s;
void update(long long i,long long val){
s1.add(i,val+d*i);
s2.add(500002-i,val-u*i);
}long long get(long long i){
return max(s2.sum(500002-i)+u*i,s1.sum(i)-d*i);
}
//end BIT
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
s1.build();s2.build();
cin>>n>>u>>d>>s;
vector<pair<long long,long long>> v[500005];
for(int i = 0;i<n;i++){
long long a,b,c;
cin>>a>>b>>c;
v[a].push_back({b,c});
}
update(s,0);
for(long long i = 0;i<=500004;i++){
if(v[i].empty())continue;
sort(v[i].begin(),v[i].end());
long long cur[v[i].size()] ;
long long maxD[v[i].size()] ;
long long maxU[v[i].size()];
for(int j = 0;j<v[i].size();j++){
cur[j] = -1e13 , maxD[j] =-1e13 , maxU[j] = -1e13;
cur[j] = get(v[i][j].first)+v[i][j].second;
}
long long tempD = -1e13,tempU = -1e13;
for(int j = 0;j<v[i].size();j++){
maxD[j] = max(cur[j],tempD-d*v[i][j].first+v[i][j].second);
tempD = max(tempD,maxD[j]+d*v[i][j].first);
}
for(int j = v[i].size()-1;j>=0;j--){
maxU[j] = max(cur[j],tempU+u*v[i][j].first+v[i][j].second);
tempU = max(tempU,maxU[j]-u*v[i][j].first);
}
for(int j = 0;j<v[i].size();j++){
cur[j] = max({cur[j],maxD[j],maxU[j]});
update(v[i][j].first,cur[j]);
}
}
cout<<get(s)<<endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |