#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
const long long inf = 1e18;
struct line {
long long a, b;
long long eval(long long x) const {
return a * x + b;
}
long long div(long long a, long long b) const {
return a / b - ((a ^ b) < 0 && a % b);
}
long long insec(const line &o) const {
return div(o.b - b, a - o.a);
}
};
int n, m, t, top;
long long x, w;
line hull[N];
long long pf[N], dp[N];
array<long long, 2> s[N], a[N];
void add(line k) {
if (top > 0 && hull[top].a == k.a) {
if (hull[top].b <= k.b) {
return;
}
--top;
}
while (top > 1 && hull[top - 1].insec(hull[top]) >= hull[top].insec(k)) {
--top;
}
hull[++top] = k;
}
long long qry(long long x) {
int l = 1, r = top - 1, p = top;
while (l <= r) {
int md = (l + r) / 2;
if (hull[md].insec(hull[md + 1]) >= x) {
p = md;
r = md - 1;
} else {
l = md + 1;
}
}
return hull[p].eval(x);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> x >> n >> m >> w >> t;
for (int i = 1; i <= n; ++i) {
cin >> s[i][1];
s[i][0] = s[i][1] % t;
}
s[++n] = {x % t, x};
for (int i = 1; i <= m; ++i) {
cin >> a[i][0] >> a[i][1];
}
sort(s + 1, s + n + 1);
sort(a + 1, a + m + 1);
for (int i = 1; i <= m; ++i) {
pf[i] = pf[i - 1] + a[i][1];
}
a[m + 1][0] = inf;
add({0, 0});
for (int i = 1, j = 0; i <= m; ++i) {
dp[i] = dp[i - 1] + (x / t + ((x % t) >= a[i][0])) * w;
long long mn = inf;
while (j <= n && s[j][0] < a[i][0]) {
++j;
}
while (j <= n && s[j][0] < a[i + 1][0]) {
mn = min(mn, s[j][1] / t);
++j;
}
if (mn != inf) {
dp[i] = min(dp[i], qry(mn) + pf[i] + i * w * mn);
}
add({-i * w, dp[i] - pf[i]});
}
cout << dp[m] + (long long) (x / t + 1) * w;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
0 ms |
4556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
0 ms |
4556 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4816 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
0 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
0 ms |
4556 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4816 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
0 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4572 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
4624 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
1 ms |
4596 KB |
Output is correct |
44 |
Correct |
2 ms |
4620 KB |
Output is correct |
45 |
Correct |
2 ms |
4700 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
0 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
0 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
0 ms |
4556 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4816 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
0 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4572 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
4624 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
1 ms |
4596 KB |
Output is correct |
44 |
Correct |
2 ms |
4620 KB |
Output is correct |
45 |
Correct |
2 ms |
4700 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
1 ms |
4444 KB |
Output is correct |
52 |
Correct |
75 ms |
16468 KB |
Output is correct |
53 |
Correct |
58 ms |
16468 KB |
Output is correct |
54 |
Correct |
68 ms |
15856 KB |
Output is correct |
55 |
Correct |
75 ms |
15956 KB |
Output is correct |
56 |
Correct |
70 ms |
16212 KB |
Output is correct |
57 |
Correct |
75 ms |
16032 KB |
Output is correct |
58 |
Correct |
68 ms |
16724 KB |
Output is correct |
59 |
Correct |
66 ms |
15956 KB |
Output is correct |
60 |
Correct |
68 ms |
15704 KB |
Output is correct |
61 |
Correct |
67 ms |
15732 KB |
Output is correct |
62 |
Correct |
66 ms |
15956 KB |
Output is correct |
63 |
Correct |
51 ms |
16980 KB |
Output is correct |
64 |
Correct |
52 ms |
16724 KB |
Output is correct |
65 |
Correct |
69 ms |
16568 KB |
Output is correct |
66 |
Correct |
62 ms |
16472 KB |
Output is correct |
67 |
Correct |
62 ms |
16468 KB |
Output is correct |
68 |
Correct |
63 ms |
16492 KB |
Output is correct |
69 |
Correct |
68 ms |
16212 KB |
Output is correct |
70 |
Correct |
69 ms |
16212 KB |
Output is correct |
71 |
Correct |
72 ms |
16364 KB |
Output is correct |
72 |
Correct |
70 ms |
16212 KB |
Output is correct |
73 |
Correct |
77 ms |
16192 KB |
Output is correct |
74 |
Correct |
70 ms |
16212 KB |
Output is correct |
75 |
Correct |
74 ms |
16500 KB |
Output is correct |
76 |
Correct |
72 ms |
16380 KB |
Output is correct |
77 |
Correct |
65 ms |
16212 KB |
Output is correct |
78 |
Correct |
68 ms |
16308 KB |
Output is correct |
79 |
Correct |
83 ms |
15952 KB |
Output is correct |
80 |
Correct |
68 ms |
15956 KB |
Output is correct |