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;
typedef long long ll;
const int N = 1e5 + 1;
const ll INF = 1e18;
int k, m, d, a, n;
pair<int, int> t[N];
struct segTree{
int n, a;
vector <int> vals;
vector <ll> maxV;
segTree(const vector <int> &_vals, int _a){
n = _vals.size();
a = _a;
vals = _vals;
maxV.assign(n * 4 + 1, -INF);
}
void update(int id, int l, int r, int u, int v, ll val){
if (v < l || r < u)
return;
if (u <= l && r <= v){
maxV[id] = max(maxV[id], val);
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
}
ll get(int id, int l, int r, int pos){
if (pos < l || r < pos)
return -INF;
ll res = maxV[id];
if (l == r)
return res;
int mid = (l + r) >> 1;
res = max(res, get(id << 1, l, mid, pos));
res = max(res, get(id << 1 | 1, mid + 1, r, pos));
return res;
}
void update(int rem, ll val){
int pos = lower_bound(vals.begin(), vals.end(), rem) - vals.begin() + 1;
update(1, 1, n, 1, pos, val);
update(1, 1, n, pos, n, val - a);
}
ll get(int rem){
int pos = lower_bound(vals.begin(), vals.end(), rem) - vals.begin() + 1;
return get(1, 1, n, pos);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k >> m >> d >> a >> n;
for(int i = 1; i <= n; i++)
cin >> t[i].first >> t[i].second;
m -= k;
for(int i = 1; i <= n; i++)
t[i].first -= k;
k = 0;
sort(t + 1, t + 1 + n);
vector <int> vals;
for(int i = 1; i <= n; i++)
vals.push_back(t[i].first % d);
vals.push_back(k % d);
vals.push_back(m % d);
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
segTree ST(vals, a);
ST.update(k % d, 0);
for(int i = 1; i <= n; i++){
int rem = t[i].first % d;
ll val = ST.get(rem) + t[i].second;
ST.update(rem, val);
}
ll ans = ST.get(m % d) - m / d * a;
cout << ans;
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... |