#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll INF= 1000000000000005LL, cons= 500001;
ll n, upcost, downcost, sta;
vector<ii> a[500005];
ll g[500005][2], f[500005][2], s[500005];
ll d[2][2000505];
void update(ll type, ll k, ll p, ll q, ll x, ll z){
if(p>q || p>x || q<x) return;
if(p==x && x==q){
d[type][k]= z;
return;
}
update(type,2*k,p,(p+q)/2,x,z);
update(type,2*k+1,(p+q)/2+1,q,x,z);
d[type][k]= max(d[type][2*k], d[type][2*k+1]);
}
ll get(ll type, ll k, ll p, ll q, ll x, ll y){
if(p>q || p>y || q<x) return -INF;
if(x<=p && q<=y) return d[type][k];
return max(get(type,2*k,p,(p+q)/2,x,y),get(type,2*k+1,(p+q)/2+1,q,x,y));
}
int main(){
ios_base::sync_with_stdio(false);
cin >> n >> upcost >> downcost >> sta;
a[0].push_back(ii(sta,0)); a[cons].push_back(ii(sta,0));
for(ll i=1;i<=n;i++){
ll x, y, z; cin >> x >> y >> z;
a[x].push_back(ii(y,z));
}
for(ll i=1;i<=4*cons;i++){d[0][i]= -INF; d[1][i]= -INF;}
update(0,1,1,cons,sta,upcost*sta);
update(1,1,1,cons,sta,-downcost*sta);
for(ll i=cons-1;i>=0;i--){
if(a[i].empty()) continue;
sort(a[i].begin(),a[i].end());
ll maxi= -INF;
for(ll j=0;j<(ll)a[i].size();j++){
s[j+1]= s[j]+a[i][j].y;
maxi= max(maxi, -s[j]+(upcost+downcost)*a[i][j].x);
g[j+1][0]= maxi+(s[j+1]-(upcost+downcost)*a[i][j].x);
}
maxi= -INF;
for(ll j=(ll)a[i].size()-1;j>=0;j--){
maxi= max(maxi, s[j+1]-(upcost+downcost)*a[i][j].x);
g[j+1][1]= maxi+(-s[j]+(upcost+downcost)*a[i][j].x);
}
for(ll j=0;j<(ll)a[i].size();j++){
f[j+1][1]= get(0,1,1,cons,1,a[i][j].x)-upcost*a[i][j].x;
f[j+1][1]= max(f[j+1][1], get(1,1,1,cons,a[i][j].x,cons)+downcost*a[i][j].x);
}
maxi= -INF;
for(ll j=0;j<(ll)a[i].size();j++){
maxi= max(maxi, f[j+1][1]+upcost*a[i][j].x-s[j]);
f[j+1][0]= maxi+(-upcost*a[i][j].x+s[j]+g[j+1][1]);
}
maxi= -INF;
for(ll j=(ll)a[i].size()-1;j>=0;j--){
maxi= max(maxi, f[j+1][1]-downcost*a[i][j].x+s[j+1]);
f[j+1][0]= max(f[j+1][0], maxi+(downcost*a[i][j].x-s[j+1]+g[j+1][0]));
}
for(ll j=0;j<(ll)a[i].size();j++){
update(0,1,1,cons,a[i][j].x,f[j+1][0]+upcost*a[i][j].x);
update(1,1,1,cons,a[i][j].x,f[j+1][0]-downcost*a[i][j].x);
}
}
cout << f[1][0] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50524 KB |
Output is correct |
2 |
Correct |
8 ms |
50760 KB |
Output is correct |
3 |
Correct |
9 ms |
50776 KB |
Output is correct |
4 |
Correct |
10 ms |
50780 KB |
Output is correct |
5 |
Correct |
13 ms |
50780 KB |
Output is correct |
6 |
Correct |
32 ms |
51380 KB |
Output is correct |
7 |
Correct |
73 ms |
52332 KB |
Output is correct |
8 |
Correct |
119 ms |
53880 KB |
Output is correct |
9 |
Correct |
168 ms |
55448 KB |
Output is correct |
10 |
Correct |
301 ms |
59988 KB |
Output is correct |
11 |
Correct |
336 ms |
60236 KB |
Output is correct |
12 |
Correct |
456 ms |
63256 KB |
Output is correct |
13 |
Correct |
446 ms |
63056 KB |
Output is correct |
14 |
Correct |
557 ms |
64084 KB |
Output is correct |
15 |
Correct |
504 ms |
65396 KB |
Output is correct |
16 |
Correct |
544 ms |
63764 KB |
Output is correct |
17 |
Correct |
8 ms |
50524 KB |
Output is correct |
18 |
Correct |
8 ms |
50792 KB |
Output is correct |
19 |
Correct |
9 ms |
50780 KB |
Output is correct |
20 |
Correct |
10 ms |
50844 KB |
Output is correct |
21 |
Correct |
10 ms |
50816 KB |
Output is correct |
22 |
Correct |
11 ms |
50780 KB |
Output is correct |
23 |
Correct |
12 ms |
50780 KB |
Output is correct |
24 |
Correct |
12 ms |
50856 KB |
Output is correct |
25 |
Correct |
103 ms |
52632 KB |
Output is correct |
26 |
Correct |
193 ms |
56900 KB |
Output is correct |
27 |
Correct |
292 ms |
59444 KB |
Output is correct |
28 |
Correct |
319 ms |
60100 KB |
Output is correct |
29 |
Correct |
416 ms |
59728 KB |
Output is correct |
30 |
Correct |
406 ms |
62916 KB |
Output is correct |