#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5;
const long long inf = 1e18;
int k, m, d, a, n;
int t[N], b[N];
long long ft[2][N], dp[N];
void upd(long long *s, int i, long long x) {
for (; i < N; i += i & -i) {
s[i] = max(s[i], x);
}
}
long long qry(long long *s, int i) {
long long res = -inf;
for (; i; i -= i & -i) {
res = max(res, s[i]);
}
return res;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> k >> m >> d >> a >> n;
t[0] = k, t[n + 1] = m;
vector<int> comp = {k % d, m % d};
for (int i = 1; i <= n; ++i) {
cin >> t[i] >> b[i];
comp.push_back(t[i] % d);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (auto it : {0, 1}) {
fill(ft[it] + 1, ft[it] + N, -inf);
}
for (int i = 0; i <= n + 1; ++i) {
int p = lower_bound(comp.begin(), comp.end(), t[i] % d) - comp.begin() + 1;
if (i) {
dp[i] = max(qry(ft[0], p), qry(ft[1], N - p)) - (long long) (t[i] / d) * a + b[i];
}
upd(ft[0], p + 1, dp[i] + (long long) (t[i] / d - 1) * a);
upd(ft[1], N - p, dp[i] + (long long) (t[i] / d) * a);
}
cout << dp[n + 1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2520 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
3952 KB |
Output is correct |
2 |
Correct |
28 ms |
4060 KB |
Output is correct |
3 |
Correct |
10 ms |
3548 KB |
Output is correct |
4 |
Correct |
19 ms |
4028 KB |
Output is correct |
5 |
Correct |
12 ms |
3552 KB |
Output is correct |
6 |
Correct |
11 ms |
3296 KB |
Output is correct |
7 |
Correct |
21 ms |
3944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
4076 KB |
Output is correct |
2 |
Correct |
43 ms |
4060 KB |
Output is correct |
3 |
Correct |
27 ms |
4056 KB |
Output is correct |
4 |
Correct |
23 ms |
4060 KB |
Output is correct |
5 |
Correct |
21 ms |
3548 KB |
Output is correct |
6 |
Correct |
24 ms |
3296 KB |
Output is correct |