#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
int k, m, d, a, n;
pair<int, int> p[100000];
struct node{
ll x;
node *left, *right;
node() :x(-2e18), left(NULL), right(NULL){}
};
void update(node *h, int l, int r, int g, ll x){
if (r < g || g < l) return;
h->x = max(h->x, x);
if (l != r){
if (!h->left) h->left = new node;
if (!h->right) h->right = new node;
update(h->left, l, (l + r) / 2, g, x);
update(h->right, (l + r) / 2 + 1, r, g, x);
}
}
ll query(node *h, int l, int r, int gl, int gr){
if (r < gl || gr < l) return -2e18;
if (gl <= l && r <= gr) return h->x;
else{
if (!h->left) h->left = new node;
if (!h->right) h->right = new node;
return max(query(h->left, l, (l + r) / 2, gl, gr),
query(h->right, (l + r) / 2 + 1, r, gl, gr));
}
}
int main(){
scanf("%d %d %d %d %d", &k, &m, &d, &a, &n);
for (int i = 0; i < n; i++)
scanf("%d %d", &p[i].first, &p[i].second);
sort(p, p + n);
node *rt = new node;
update(rt, 0, d - 1, k%d, (ll)k / d*a);
for (int i = 0; i < n; i++){
ll maxi = max((ll)-(p[i].first / d + 1)*a + query(rt, 0, d - 1, 0, p[i].first%d - 1),
(ll)-p[i].first / d*a + query(rt, 0, d - 1, p[i].first%d, d - 1)) + p[i].second;
update(rt, 0, d - 1, p[i].first%d, maxi + (ll)p[i].first / d*a);
}
printf("%lld", max((ll)-(m / d + 1)*a + query(rt, 0, d - 1, 0, m%d - 1),
(ll)-m / d*a + query(rt, 0, d - 1, m%d, d - 1)));
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2784 KB |
Output is correct |
2 |
Correct |
4 ms |
3048 KB |
Output is correct |
3 |
Correct |
2 ms |
2784 KB |
Output is correct |
4 |
Correct |
0 ms |
2124 KB |
Output is correct |
5 |
Correct |
0 ms |
1992 KB |
Output is correct |
6 |
Correct |
0 ms |
1992 KB |
Output is correct |
7 |
Correct |
0 ms |
1992 KB |
Output is correct |
8 |
Correct |
0 ms |
1992 KB |
Output is correct |
9 |
Correct |
3 ms |
3180 KB |
Output is correct |
10 |
Correct |
0 ms |
2784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
1992 KB |
Output is correct |
2 |
Correct |
61 ms |
1992 KB |
Output is correct |
3 |
Correct |
31 ms |
1992 KB |
Output is correct |
4 |
Correct |
61 ms |
1992 KB |
Output is correct |
5 |
Correct |
35 ms |
1992 KB |
Output is correct |
6 |
Correct |
17 ms |
1992 KB |
Output is correct |
7 |
Correct |
53 ms |
1992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
7800 KB |
Output is correct |
2 |
Correct |
175 ms |
24036 KB |
Output is correct |
3 |
Correct |
223 ms |
44628 KB |
Output is correct |
4 |
Correct |
253 ms |
65484 KB |
Output is correct |
5 |
Correct |
116 ms |
14664 KB |
Output is correct |
6 |
Correct |
66 ms |
8328 KB |
Output is correct |