#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 101010
#define MAXS 41
#define INF 1000000000000000001
#define MOD 1000000007
#define bb ' '
#define ln '\n'
#define Ln '\n'
int A[MAX];
int B[MAX];
int pra[MAX], nea[MAX];
int prb[MAX], neb[MAX];
struct frac {
int a, b, ind;
frac(int a = 0, int b = 1, int ind = 0) :a(a), b(b), ind(ind) {}
};
bool operator<(frac f1, frac f2) { return (1ll * f1.a * f2.b < 1ll * f1.b * f2.a) || ((1ll * f1.a * f2.b == 1ll * f1.b * f2.a) && f1.ind < f2.ind); }
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int H, W;
cin >> H >> W;
int i;
for (i = 1; i <= H; i++) cin >> A[i];
for (i = 1; i <= W; i++) cin >> B[i];
for (i = H; i > 1; i--) pra[i] = i - 1;
for (i = W; i > 1; i--) prb[i] = i - 1;
for (i = 1; i < H; i++) nea[i] = i + 1;
for (i = 1; i < W; i++) neb[i] = i + 1;
ll ans = 0;
set<frac> sta, stb;
for (i = 1; i < H; i++) sta.insert(frac(A[i + 1] - A[i], 1, i));
for (i = 1; i < W; i++) stb.insert(frac(B[i + 1] - B[i], 1, i));
int h, w;
h = H;
w = W;
while (1) {
if (sta.empty() || stb.empty()) {
if (sta.empty()) ans += 1ll * (W - 1) * A[1];
if (stb.empty()) ans += 1ll * (H - 1) * B[1];
break;
}
if (*stb.rbegin() < *sta.rbegin()) {
int k = (*sta.rbegin()).ind;
int nk = nea[k];
if (nk == H) {
ans += 1ll * B[W] * (*sta.rbegin()).b;
H = k;
sta.erase(*sta.rbegin());
}
else {
sta.erase(frac(A[nea[nk]] - A[nk], nea[nk] - nk, nk));
sta.erase(*sta.rbegin());
sta.emplace(frac(A[nea[nk]] - A[k], nea[nk] - k, k));
pra[nea[nk]] = k;
nea[k] = nea[nk];
}
}
else {
int k = (*stb.rbegin()).ind;
int nk = neb[k];
if (nk == W) {
ans += 1ll * A[H] * (*stb.rbegin()).b;
W = k;
stb.erase(*stb.rbegin());
}
else {
stb.erase(frac(B[neb[nk]] - B[nk], neb[nk] - nk, nk));
stb.erase(*stb.rbegin());
stb.emplace(frac(B[neb[nk]] - B[k], neb[nk] - k, k));
prb[neb[nk]] = k;
neb[k] = neb[nk];
}
}
}
cout << ans << ln;
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:41:6: warning: variable 'h' set but not used [-Wunused-but-set-variable]
41 | int h, w;
| ^
kyoto.cpp:41:9: warning: variable 'w' set but not used [-Wunused-but-set-variable]
41 | int h, w;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
141 ms |
6532 KB |
Output is correct |
5 |
Correct |
57 ms |
6256 KB |
Output is correct |
6 |
Correct |
33 ms |
2900 KB |
Output is correct |
7 |
Correct |
444 ms |
15952 KB |
Output is correct |
8 |
Correct |
390 ms |
16076 KB |
Output is correct |
9 |
Correct |
433 ms |
15976 KB |
Output is correct |
10 |
Correct |
424 ms |
15928 KB |
Output is correct |
11 |
Correct |
135 ms |
15908 KB |
Output is correct |
12 |
Correct |
380 ms |
15988 KB |
Output is correct |
13 |
Correct |
423 ms |
15936 KB |
Output is correct |
14 |
Correct |
220 ms |
16052 KB |
Output is correct |
15 |
Correct |
103 ms |
16148 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
328 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
141 ms |
6532 KB |
Output is correct |
33 |
Correct |
57 ms |
6256 KB |
Output is correct |
34 |
Correct |
33 ms |
2900 KB |
Output is correct |
35 |
Correct |
444 ms |
15952 KB |
Output is correct |
36 |
Correct |
390 ms |
16076 KB |
Output is correct |
37 |
Correct |
433 ms |
15976 KB |
Output is correct |
38 |
Correct |
424 ms |
15928 KB |
Output is correct |
39 |
Correct |
135 ms |
15908 KB |
Output is correct |
40 |
Correct |
380 ms |
15988 KB |
Output is correct |
41 |
Correct |
423 ms |
15936 KB |
Output is correct |
42 |
Correct |
220 ms |
16052 KB |
Output is correct |
43 |
Correct |
103 ms |
16148 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
328 KB |
Output is correct |
46 |
Correct |
1 ms |
324 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
328 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
136 ms |
7756 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
9 ms |
1108 KB |
Output is correct |
58 |
Correct |
26 ms |
2516 KB |
Output is correct |
59 |
Correct |
391 ms |
17140 KB |
Output is correct |
60 |
Correct |
390 ms |
17144 KB |
Output is correct |
61 |
Correct |
390 ms |
17152 KB |
Output is correct |
62 |
Correct |
408 ms |
17164 KB |
Output is correct |
63 |
Correct |
97 ms |
16976 KB |
Output is correct |
64 |
Correct |
417 ms |
17156 KB |
Output is correct |
65 |
Correct |
384 ms |
17156 KB |
Output is correct |
66 |
Correct |
139 ms |
17140 KB |
Output is correct |
67 |
Correct |
119 ms |
17360 KB |
Output is correct |