#include <bits/stdc++.h>
#define IO_OP ios::sync_with_stdio(0), cin.tie(0)
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7;
bool check(int a, int b, int c, int d) { // a / b >= c / d ?
assert(b > 0 && d > 0);
return 1LL * a * d >= 1LL * b * c;
}
vi go(vi a) {
vi h;
int n = SZ(a);
for(int i = 0; i < n; i++) {
while(SZ(h) >= 2 && check(a[h[SZ(h) - 1]] - a[h[SZ(h) - 2]], h[SZ(h) - 1] - h[SZ(h) - 2], a[i] - a[h[SZ(h) - 1]], i - h[SZ(h) - 1]))
h.pop_back();
h.PB(i);
}
return h;
}
signed main()
{
IO_OP;
int n, m;
cin >> n >> m;
assert(n >= 2 && m >= 2);
vi a(n), b(m);
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < m; i++)
cin >> b[i];
vi ha = go(a), hb = go(b);
ll ans = 0;
while(SZ(ha) >= 2 || SZ(hb) >= 2) {
if(SZ(hb) == 1 || (SZ(ha) >= 2 && check(a[ha[SZ(ha) - 1]] - a[ha[SZ(ha) - 2]], ha[SZ(ha) - 1] - ha[SZ(ha) - 2], b[hb[SZ(hb) - 1]] - b[hb[SZ(hb) - 2]], hb[SZ(hb) - 1] - hb[SZ(hb) - 2]))) {
ans += 1LL * (ha[SZ(ha) - 1] - ha[SZ(ha) - 2]) * b[hb.back()];
ha.pop_back();
} else {
ans += 1LL * (hb[SZ(hb) - 1] - hb[SZ(hb) - 2]) * a[ha.back()];
hb.pop_back();
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
1236 KB |
Output is correct |
5 |
Correct |
9 ms |
1108 KB |
Output is correct |
6 |
Correct |
3 ms |
720 KB |
Output is correct |
7 |
Correct |
17 ms |
2128 KB |
Output is correct |
8 |
Correct |
16 ms |
2224 KB |
Output is correct |
9 |
Correct |
17 ms |
2232 KB |
Output is correct |
10 |
Correct |
18 ms |
2144 KB |
Output is correct |
11 |
Correct |
17 ms |
2132 KB |
Output is correct |
12 |
Correct |
15 ms |
2236 KB |
Output is correct |
13 |
Correct |
16 ms |
2228 KB |
Output is correct |
14 |
Correct |
15 ms |
2216 KB |
Output is correct |
15 |
Correct |
15 ms |
2332 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
7 ms |
1236 KB |
Output is correct |
33 |
Correct |
9 ms |
1108 KB |
Output is correct |
34 |
Correct |
3 ms |
720 KB |
Output is correct |
35 |
Correct |
17 ms |
2128 KB |
Output is correct |
36 |
Correct |
16 ms |
2224 KB |
Output is correct |
37 |
Correct |
17 ms |
2232 KB |
Output is correct |
38 |
Correct |
18 ms |
2144 KB |
Output is correct |
39 |
Correct |
17 ms |
2132 KB |
Output is correct |
40 |
Correct |
15 ms |
2236 KB |
Output is correct |
41 |
Correct |
16 ms |
2228 KB |
Output is correct |
42 |
Correct |
15 ms |
2216 KB |
Output is correct |
43 |
Correct |
15 ms |
2332 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
316 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
11 ms |
1748 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
468 KB |
Output is correct |
58 |
Correct |
3 ms |
724 KB |
Output is correct |
59 |
Correct |
23 ms |
3352 KB |
Output is correct |
60 |
Correct |
23 ms |
3240 KB |
Output is correct |
61 |
Correct |
23 ms |
2956 KB |
Output is correct |
62 |
Correct |
29 ms |
3180 KB |
Output is correct |
63 |
Correct |
23 ms |
3628 KB |
Output is correct |
64 |
Correct |
27 ms |
3184 KB |
Output is correct |
65 |
Correct |
24 ms |
3336 KB |
Output is correct |
66 |
Correct |
21 ms |
3372 KB |
Output is correct |
67 |
Correct |
26 ms |
2660 KB |
Output is correct |