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 fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file "name"
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
const int inf = 1e9;
const ll linf = 1e17;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int n, cnt;
ll res, tmp, k, m, d, mana;
pii a[N];
ll st[4 * N], cost;
int x[N];
void inp()
{
cin >> k >> m >> d >> mana >> n;
a[1] = {k, 0};
for(int i = 2; i <= n + 1; ++i) cin >> a[i].fi >> a[i].se;
n++;
a[++n] = {m, 0};
}
void build(int id, int l, int r) {
if(l == r) {
st[id] = -linf;
return ;
}
int m = (l + r) >> 1;
build(id << 1, l, m);
build(id << 1 | 1, m + 1, r);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
void update(int id, int l, int r, int i, ll v) {
if(i < l || r < i) return ;
if(l == r) {
maximize(st[id], v);
return ;
}
int m = (l + r) >> 1;
update(id << 1, l, m, i, v);
update(id << 1 | 1, m + 1, r, i, v);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
ll get(int id, int l, int r, int u, int v) {
if(v < l || r < u) return -linf;
if(l >= u && r <= v) return st[id];
int m = (l + r) >> 1;
return max(get(id << 1, l, m, u, v), get(id << 1 | 1, m + 1, r, u, v));
}
void solve()
{
for(int i = 1; i <= n; ++i) {
x[i] = a[i].fi % d;
if(a[i].fi % d == 0) x[i] = d;
}
sort(x + 1, x + n + 1);
cnt = unique(x + 1, x + n + 1) - x - 1;
build(1, 1, cnt);
int p = a[1].fi % d;
if(a[1].fi % d == 0) p = d;
int id = lower_bound(x + 1, x + cnt + 1, p) - x;
update(1, 1, cnt, id, (a[1].fi + d - 1) / d * mana);
for(int i = 2; i <= n; ++i) {
int p = a[i].fi % d;
if(a[i].fi % d == 0) p = d;
int id = lower_bound(x + 1, x + cnt + 1, p) - x;
tmp = get(1, 1, cnt, 1, id - 1) - mana;
maximize(tmp, get(1, 1, cnt, id, cnt));
cost = tmp - (a[i].fi + d - 1) / d * mana + a[i].se;
update(1, 1, cnt, id, cost + (a[i].fi + d - 1) / d * mana);
}
cout << cost;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp" , "r" , stdin);
// freopen(file".out" , "w" , stdout);
inp();
solve();
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... |