#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<int,pii> ipii;
typedef pair<pii,pii> ipiii;
const int MAXN = 5e5+30;
const int SQRT = 610;
const int MAXA = 5e3+10;
const int MOD = 1e6+7;
const int INF = 1e9+10;
const int LOG = 21;
const ld EPS = 1e-12;
void chmx(int &a, int b){ a = max(a,b); }
int n, u, d, s;
vector <ipii> vec;
int dp[MAXN], dp2[MAXN];
signed main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> u >> d >> s;
for(int i=1; i<=s-1; i++) dp[i] = -(s-i)*u;
for(int i=s+1; i<=MAXA; i++) dp[i] = -(i-s)*d;
for(int i=1; i<=n; i++){
int d,id,w; cin>>d>>id>>w;
vec.pb({d, {id,w}});
}
sort(vec.begin(), vec.end());
for(int p=0; p<vec.size(); ){
int j = p;
vector <pii> te, te2;
while(p<vec.size() && vec[p].fi == vec[j].fi){
te.pb({vec[p].se.fi, vec[p].se.se});
te2.pb({vec[p].se.fi, vec[p].se.se});
p++;
}
for(int i=1; i<=MAXA; i++) dp2[i] = dp[i];
sort(te.begin(), te.end());
for(auto [idx, add] : te){
int nw = dp[idx]+add;
for(int i=1; i<=idx; i++) chmx(dp[i], nw-(idx-i)*u);
for(int i=idx+1; i<=MAXA; i++) chmx(dp[i], nw-(i-idx)*d);
}
sort(te2.rbegin(), te2.rend());
for(auto [idx, add] : te){
int nw = dp2[idx]+add;
for(int i=1; i<=idx; i++) chmx(dp2[i], nw-(idx-i)*u);
for(int i=idx+1; i<=MAXA; i++) chmx(dp2[i], nw-(i-idx)*d);
}
for(int i=1; i<=MAXA; i++) dp[i] = dp2[i];
}
cout << dp[s] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |