#include <cstdio>
#include <stack>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef __int128 hl;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
struct point {
ll x, y;
point() {}
point(ll x, ll y) : x(x), y(y) {}
hl operator * (const point &p) { return (hl) x * (hl) p.x + (hl) y * (hl) p.y; }
};
bool ccw(point a, point b, point c) { // return 1 ako je ccw ili kolinearno
return ((hl) b.x - (hl) a.x) * ((hl) c.y - (hl) a.y) - ((hl) b.y - (hl) a.y) * ((hl) c.x - (hl) a.x) >= 0;
}
const int N = 2e5 + 10;
int n, m, ind[N];
ll w, t, prf[N], d[N], c[N];
vector<ll> s;
ll jmp[N];
vector<point> ch;
stack<int> stck;
hl dp[N];
int main() {
memset(jmp, -1, sizeof(jmp));
ll X; scanf("%lld%d%d%lld%lld", &X, &n, &m, &w, &t);
s.push_back(0ll);
s.push_back(X);
for(int i = 0; i < n; ++i) {
ll x; scanf("%lld", &x);
s.push_back(x);
}
sort(s.begin(), s.end());
n += 2;
for(int i = 1; i <= m; ++i) {
scanf("%lld%lld", d + i, c + i);
ind[i] = i;
}
sort(ind + 1, ind + m + 1, [](int a, int b) { return d[a] < d[b]; });
for(int i = 1; i <= m; ++i) {
prf[i] = prf[i - 1] + c[ind[i]];
// printf("%lld\n", prf[i]);
}
for(int i = 1; i < n; ++i) {
ll k = s[i] / t, r = s[i] % t;
int lo = 0, hi = m + 1;
for(int mi = (lo + hi) / 2; hi - lo > 1; mi = (lo + hi) / 2) {
if(d[ind[mi]] < r) { lo = mi; }
else { hi = mi; }
}
if(lo && jmp[lo] == -1 && s[i] / t * t + d[ind[lo]] > s[i - 1]) {
jmp[lo] = k;
// printf("%d %lld\n", lo, k);
}
}
for(int i = 0; i <= m; ++i) {
dp[i] = ((X - d[ind[i]]) / t + 1ll) * w + (i ? dp[i - 1] : 0);
if(i) {
point p = point(-w * i, dp[i - 1] - prf[i - 1]);
for(; ch.size() >= 2 && !ccw(p, ch.back(), ch.end()[-2]); ch.pop_back()) { /*printf("- %d\n", stck.top()); */ }
ch.push_back(p);
stck.push(i);
// printf("+ %d (%lld %lld)\n", i, ch.back().x, ch.back().y);
}
if(jmp[i] != -1) {
point q = point(jmp[i], 1);
hl plus = (hl) prf[i] + (hl) jmp[i] * (hl) w * (hl) (i + 1);
// printf("%lld = %lld + %lld * %lld * %lld\n", (ll) plus, (ll) prf[i], (ll) jmp[i], (ll) w, (ll) i + 1);
int lo = -1, hi = (int) ch.size() - 1;
for(int mi = (lo + hi) / 2; hi - lo > 1; mi = (lo + hi) / 2) {
if(q * ch[mi] > q * ch[mi + 1]) { lo = mi; }
else { hi = mi; }
}
if(hi >= 0) {
dp[i] = min(dp[i], q * ch[hi] + plus);
// printf("j %d %d | %lld %lld %lld %lld %lld\n", i, hi, q.x, q.y, (ll) plus, ch[hi].x, ch[hi].y);
}
}
// printf("%d: %lld %lld\n\n", i, (ll) dp[i], jmp[i]);
}
printf("%lld\n", (ll) dp[m]);
return 0;
}
Compilation message
coach.cpp: In function 'int main()':
coach.cpp:40:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | ll X; scanf("%lld%d%d%lld%lld", &X, &n, &m, &w, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
coach.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | ll x; scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
coach.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%lld%lld", d + i, c + i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
0 ms |
2396 KB |
Output is correct |
32 |
Correct |
0 ms |
2484 KB |
Output is correct |
33 |
Correct |
0 ms |
2472 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
0 ms |
2396 KB |
Output is correct |
32 |
Correct |
0 ms |
2484 KB |
Output is correct |
33 |
Correct |
0 ms |
2472 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
1 ms |
2652 KB |
Output is correct |
42 |
Correct |
1 ms |
2592 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2652 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2652 KB |
Output is correct |
47 |
Correct |
1 ms |
2652 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2652 KB |
Output is correct |
50 |
Correct |
1 ms |
2640 KB |
Output is correct |
51 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
0 ms |
2396 KB |
Output is correct |
32 |
Correct |
0 ms |
2484 KB |
Output is correct |
33 |
Correct |
0 ms |
2472 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
1 ms |
2652 KB |
Output is correct |
42 |
Correct |
1 ms |
2592 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2652 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2652 KB |
Output is correct |
47 |
Correct |
1 ms |
2652 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2652 KB |
Output is correct |
50 |
Correct |
1 ms |
2640 KB |
Output is correct |
51 |
Correct |
2 ms |
2396 KB |
Output is correct |
52 |
Correct |
127 ms |
18628 KB |
Output is correct |
53 |
Correct |
114 ms |
18724 KB |
Output is correct |
54 |
Correct |
107 ms |
18884 KB |
Output is correct |
55 |
Correct |
125 ms |
18344 KB |
Output is correct |
56 |
Correct |
103 ms |
18368 KB |
Output is correct |
57 |
Correct |
102 ms |
18364 KB |
Output is correct |
58 |
Correct |
103 ms |
18600 KB |
Output is correct |
59 |
Correct |
104 ms |
18340 KB |
Output is correct |
60 |
Correct |
100 ms |
17928 KB |
Output is correct |
61 |
Correct |
99 ms |
18400 KB |
Output is correct |
62 |
Correct |
102 ms |
18112 KB |
Output is correct |
63 |
Correct |
92 ms |
21216 KB |
Output is correct |
64 |
Correct |
85 ms |
18880 KB |
Output is correct |
65 |
Correct |
115 ms |
18568 KB |
Output is correct |
66 |
Correct |
115 ms |
18644 KB |
Output is correct |
67 |
Correct |
112 ms |
18624 KB |
Output is correct |
68 |
Correct |
117 ms |
18624 KB |
Output is correct |
69 |
Correct |
133 ms |
18368 KB |
Output is correct |
70 |
Correct |
146 ms |
18368 KB |
Output is correct |
71 |
Correct |
127 ms |
18368 KB |
Output is correct |
72 |
Correct |
138 ms |
18428 KB |
Output is correct |
73 |
Correct |
134 ms |
18368 KB |
Output is correct |
74 |
Correct |
130 ms |
18344 KB |
Output is correct |
75 |
Correct |
133 ms |
18628 KB |
Output is correct |
76 |
Correct |
142 ms |
18624 KB |
Output is correct |
77 |
Correct |
110 ms |
21072 KB |
Output is correct |
78 |
Correct |
126 ms |
18624 KB |
Output is correct |
79 |
Correct |
147 ms |
18052 KB |
Output is correct |
80 |
Correct |
137 ms |
17852 KB |
Output is correct |