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;
const int N = (int) (6e6);
const long long inf = (long long) -1.0001e18;
int root, ch[N][2], tsz;
long long mx[N];
void Modify(int& x, int l, int r, int i, long long v) {
if (!x) {
x = ++tsz;
mx[x] = inf;
}
if (l == r) {
mx[x] = max(mx[x], v);
return;
}
int mid = (l + r) >> 1;
if (i <= mid) {
Modify(ch[x][0], l, mid, i, v);
} else {
Modify(ch[x][1], mid + 1, r, i, v);
}
mx[x] = inf;
for (int c = 0; c < 2; c++) {
if (ch[x][c] > 0) {
mx[x] = max(mx[x], mx[ch[x][c]]);
}
}
}
long long Query(int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return x == 0 ? inf : mx[x];
}
int mid = (l + r) >> 1;
if (rr <= mid) {
return Query(ch[x][0], l, mid, ll, rr);
} else if (ll > mid) {
return Query(ch[x][1], mid + 1, r, ll, rr);
} else {
return max(Query(ch[x][0], l, mid, ll, rr), Query(ch[x][1], mid + 1, r, ll, rr));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int k, m, d, a, n;
cin >> k >> m >> d >> a >> n;
vector<int> t(n), b(n);
for (int i = 0; i < n; i++) {
cin >> t[i] >> b[i];
}
vector<long long> dp(n, inf);
for (int i = 0; i < n; i++){
dp[i] = -((t[i] - k + d - 1) / d) * 1LL * a + b[i];
int md = (t[i] % d);
dp[i] = max(dp[i], Query(root, 0, d, md, d) - (t[i] / d) * 1LL * a + b[i]);
if (md > 0) {
dp[i] = max(dp[i], Query(root, 0, d, 0, md - 1) - (t[i] / d + 1) * 1LL * a + b[i]);
}
Modify(root, 0, d, md, dp[i] + (t[i] / d) * 1LL * a);
}
long long res = inf;
res = -((m - k + d - 1) / d) * 1LL * a;
for (int i = 0; i < n; i++) {
res = max(res, dp[i] - ((m - t[i] + d - 1) / d) * 1LL * a);
}
cout << res << '\n';
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... |