//one of the coolest problems i've ever seen
#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (ll i = s; i <= e; ++i)
#define pb(u) push_back(u)
#define len(a) (ll)a.size()
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll h, w, x = 0; cin >> h >> w;
ll a[h+1], b[w+1];
rep (i,1,h) cin >> a[i];
rep (i,1,w) cin >> b[i];
vector<ll> ha, hb;
ha.pb(1), hb.pb(1);
rep (i,2,h) {
while (true) {
ll n = len(ha);
if (n==1 || ((a[ha[n-1]]-a[ha[n-2]])*(i-ha[n-1])<(a[i]-a[ha[n-1]])*(ha[n-1]-ha[n-2]))) break;
ha.pop_back();
}
ha.pb(i);
}
rep (i,2,w) {
while (true) {
ll n = len(hb);
if (n==1 || ((b[hb[n-1]]-b[hb[n-2]])*(i-hb[n-1])<(b[i]-b[hb[n-1]])*(hb[n-1]-hb[n-2]))) break;
hb.pop_back();
}
hb.pb(i);
}
while (true) {
ll n = len(ha), m = len(hb);
if (max(n,m)<2) break;
if (n>1 && (m==1 || ((a[ha[n-1]]-a[ha[n-2]])*(hb[m-1]-hb[m-2])>(b[hb[m-1]]-b[hb[m-2]])*(ha[n-1]-ha[n-2])))) {
x += b[hb[m-1]] * (ha[n-1]-ha[n-2]);
ha.pop_back();
}
else {
x += a[ha[n-1]] * (hb[m-1]-hb[m-2]);
hb.pop_back();
}
}
cout << x << "\n";
return 0;
}
# |
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 |
1 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
320 KB |
Output is correct |
26 |
Correct |
0 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 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 |
340 KB |
Output is correct |
4 |
Correct |
6 ms |
1004 KB |
Output is correct |
5 |
Correct |
5 ms |
980 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
15 ms |
1968 KB |
Output is correct |
8 |
Correct |
15 ms |
2004 KB |
Output is correct |
9 |
Correct |
14 ms |
1996 KB |
Output is correct |
10 |
Correct |
14 ms |
1876 KB |
Output is correct |
11 |
Correct |
12 ms |
1908 KB |
Output is correct |
12 |
Correct |
15 ms |
2004 KB |
Output is correct |
13 |
Correct |
15 ms |
2004 KB |
Output is correct |
14 |
Correct |
15 ms |
2008 KB |
Output is correct |
15 |
Correct |
18 ms |
2104 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
320 KB |
Output is correct |
26 |
Correct |
0 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 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 |
340 KB |
Output is correct |
32 |
Correct |
6 ms |
1004 KB |
Output is correct |
33 |
Correct |
5 ms |
980 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
15 ms |
1968 KB |
Output is correct |
36 |
Correct |
15 ms |
2004 KB |
Output is correct |
37 |
Correct |
14 ms |
1996 KB |
Output is correct |
38 |
Correct |
14 ms |
1876 KB |
Output is correct |
39 |
Correct |
12 ms |
1908 KB |
Output is correct |
40 |
Correct |
15 ms |
2004 KB |
Output is correct |
41 |
Correct |
15 ms |
2004 KB |
Output is correct |
42 |
Correct |
15 ms |
2008 KB |
Output is correct |
43 |
Correct |
18 ms |
2104 KB |
Output is correct |
44 |
Correct |
1 ms |
320 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 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 |
328 KB |
Output is correct |
57 |
Correct |
1 ms |
460 KB |
Output is correct |
58 |
Correct |
4 ms |
764 KB |
Output is correct |
59 |
Correct |
22 ms |
3776 KB |
Output is correct |
60 |
Correct |
26 ms |
3772 KB |
Output is correct |
61 |
Correct |
30 ms |
3776 KB |
Output is correct |
62 |
Correct |
21 ms |
3780 KB |
Output is correct |
63 |
Correct |
22 ms |
4672 KB |
Output is correct |
64 |
Correct |
24 ms |
3760 KB |
Output is correct |
65 |
Correct |
24 ms |
3668 KB |
Output is correct |
66 |
Correct |
20 ms |
3760 KB |
Output is correct |
67 |
Correct |
27 ms |
3268 KB |
Output is correct |