#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ar array
const int N = 4e5 + 5;
struct trie {
int it[N << 2];
int lz[N << 2];
trie() {
memset(it, 0x3f, sizeof it);
memset(lz, 0, sizeof lz);
}
void dolazy(int i) {
if(lz[i]) {
int val = lz[i];
lz[i << 1] += val;
lz[i << 1 | 1] += val;
it[i << 1] += val;
it[i << 1 | 1] += val;
lz[i] = 0;
}
}
void update(int i, int l, int r, int u, int v, int val) {
if(v < l || u > r) return;
if(u <= l && r <= v) {
it[i] += val;
lz[i] += val;
return;
}
int mid = l + (r - l) / 2;
dolazy(i);
update(i << 1, l, mid, u, v, val);
update(i << 1 | 1, mid + 1, r, u, v, val);
it[i] = min(it[i << 1], it[i << 1 | 1]);
}
void update(int i, int l, int r, int pos, int val) {
if(pos < l || pos > r) return;
if(l == r) {
it[i] = val;
return;
}
int mid = l + (r - l) / 2;
dolazy(i);
update(i << 1, l, mid, pos, val);
update(i << 1 | 1, mid + 1, r, pos, val);
it[i] = min(it[i << 1], it[i << 1 | 1]);
}
int query(int i, int l, int r, int u, int v) {
if(v < l || u > r) return 1e15;
if(u <= l && r <= v) return it[i];
int mid = l + (r - l) / 2;
dolazy(i);
return min(query(i << 1, l, mid, u, v), query(i << 1 | 1, mid + 1, r, u, v));
}
} p1, p2;
int n, m, a[N];
ii b[N];
int s[N], pos[N], le[N], ri[N], p[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n + m - 1; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n + m - 1; ++i) {
cin >> b[i].se;
b[i].fi = i;
}
int nn = n + m - 1;
sort(b + 1, b + nn + 1, [](ii x, ii y) {
int u = x.fi % 2;
int v = y.fi % 2;
if(u != v) return u == 1;
else return x.fi < y.fi;
});
s[0] = 0;
for (int i = 1; i <= nn; ++i) {
pos[b[i].fi] = i;
s[i] = s[i - 1] + b[i].se;
}
int x = m, y = m, dx = -1, dy = 1;
for (int i = 1; i <= nn; ++i) {
le[i] = pos[x];
ri[i] = pos[y];
if(x == 1) dx = 1;
if(y == m + n - 1) dy = -1;
x += dx;
y += dy;
p[i] = i;
}
sort(p + 1, p + nn + 1, [](int x, int y) {
return le[x] < le[y];
});
p1.update(1, 0, nn, 0, 0);
p2.update(1, 0, nn, 0, 0);
n = nn;
for (int j = 1; j <= nn; ++j) {
int i = p[j];
int fir;
fir = p1.query(1, 0, nn, 0, le[i] - 1) + s[ri[i]] - s[le[i] - 1];
fir = min(fir, p2.query(1, 0, nn, le[i], ri[i]) + s[ri[i]]);
p1.update(1, 0, nn, ri[i], ri[i], a[i]);
fir = min(p1.query(1, 0, nn, ri[i], ri[i]), fir);
p1.update(1, 0, nn, ri[i], fir);
p2.update(1, 0, nn, ri[i], fir - s[ri[i]]);
p1.update(1, 0, nn, 0, ri[i] - 1, a[i]);
p2.update(1, 0, nn, 0, ri[i] - 1, a[i]);
}
cout << p1.it[1] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
50540 KB |
Output is correct |
2 |
Correct |
32 ms |
50512 KB |
Output is correct |
3 |
Correct |
31 ms |
50540 KB |
Output is correct |
4 |
Correct |
27 ms |
50540 KB |
Output is correct |
5 |
Correct |
26 ms |
50540 KB |
Output is correct |
6 |
Correct |
26 ms |
50540 KB |
Output is correct |
7 |
Correct |
26 ms |
50540 KB |
Output is correct |
8 |
Correct |
27 ms |
50540 KB |
Output is correct |
9 |
Correct |
28 ms |
50540 KB |
Output is correct |
10 |
Correct |
28 ms |
50540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
50540 KB |
Output is correct |
2 |
Correct |
32 ms |
50512 KB |
Output is correct |
3 |
Correct |
31 ms |
50540 KB |
Output is correct |
4 |
Correct |
27 ms |
50540 KB |
Output is correct |
5 |
Correct |
26 ms |
50540 KB |
Output is correct |
6 |
Correct |
26 ms |
50540 KB |
Output is correct |
7 |
Correct |
26 ms |
50540 KB |
Output is correct |
8 |
Correct |
27 ms |
50540 KB |
Output is correct |
9 |
Correct |
28 ms |
50540 KB |
Output is correct |
10 |
Correct |
28 ms |
50540 KB |
Output is correct |
11 |
Correct |
27 ms |
50688 KB |
Output is correct |
12 |
Correct |
26 ms |
50540 KB |
Output is correct |
13 |
Correct |
26 ms |
50540 KB |
Output is correct |
14 |
Correct |
26 ms |
50540 KB |
Output is correct |
15 |
Correct |
27 ms |
50540 KB |
Output is correct |
16 |
Correct |
27 ms |
50560 KB |
Output is correct |
17 |
Correct |
28 ms |
50540 KB |
Output is correct |
18 |
Correct |
27 ms |
50540 KB |
Output is correct |
19 |
Correct |
29 ms |
50540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
50540 KB |
Output is correct |
2 |
Correct |
32 ms |
50512 KB |
Output is correct |
3 |
Correct |
31 ms |
50540 KB |
Output is correct |
4 |
Correct |
27 ms |
50540 KB |
Output is correct |
5 |
Correct |
26 ms |
50540 KB |
Output is correct |
6 |
Correct |
26 ms |
50540 KB |
Output is correct |
7 |
Correct |
26 ms |
50540 KB |
Output is correct |
8 |
Correct |
27 ms |
50540 KB |
Output is correct |
9 |
Correct |
28 ms |
50540 KB |
Output is correct |
10 |
Correct |
28 ms |
50540 KB |
Output is correct |
11 |
Correct |
27 ms |
50688 KB |
Output is correct |
12 |
Correct |
26 ms |
50540 KB |
Output is correct |
13 |
Correct |
26 ms |
50540 KB |
Output is correct |
14 |
Correct |
26 ms |
50540 KB |
Output is correct |
15 |
Correct |
27 ms |
50540 KB |
Output is correct |
16 |
Correct |
27 ms |
50560 KB |
Output is correct |
17 |
Correct |
28 ms |
50540 KB |
Output is correct |
18 |
Correct |
27 ms |
50540 KB |
Output is correct |
19 |
Correct |
29 ms |
50540 KB |
Output is correct |
20 |
Correct |
26 ms |
50540 KB |
Output is correct |
21 |
Correct |
26 ms |
50540 KB |
Output is correct |
22 |
Correct |
27 ms |
50540 KB |
Output is correct |
23 |
Correct |
27 ms |
50540 KB |
Output is correct |
24 |
Correct |
26 ms |
50540 KB |
Output is correct |
25 |
Correct |
25 ms |
50476 KB |
Output is correct |
26 |
Correct |
26 ms |
50540 KB |
Output is correct |
27 |
Correct |
31 ms |
50540 KB |
Output is correct |
28 |
Correct |
30 ms |
50540 KB |
Output is correct |
29 |
Correct |
26 ms |
50540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
50540 KB |
Output is correct |
2 |
Correct |
32 ms |
50512 KB |
Output is correct |
3 |
Correct |
31 ms |
50540 KB |
Output is correct |
4 |
Correct |
27 ms |
50540 KB |
Output is correct |
5 |
Correct |
26 ms |
50540 KB |
Output is correct |
6 |
Correct |
26 ms |
50540 KB |
Output is correct |
7 |
Correct |
26 ms |
50540 KB |
Output is correct |
8 |
Correct |
27 ms |
50540 KB |
Output is correct |
9 |
Correct |
28 ms |
50540 KB |
Output is correct |
10 |
Correct |
28 ms |
50540 KB |
Output is correct |
11 |
Correct |
27 ms |
50688 KB |
Output is correct |
12 |
Correct |
26 ms |
50540 KB |
Output is correct |
13 |
Correct |
26 ms |
50540 KB |
Output is correct |
14 |
Correct |
26 ms |
50540 KB |
Output is correct |
15 |
Correct |
27 ms |
50540 KB |
Output is correct |
16 |
Correct |
27 ms |
50560 KB |
Output is correct |
17 |
Correct |
28 ms |
50540 KB |
Output is correct |
18 |
Correct |
27 ms |
50540 KB |
Output is correct |
19 |
Correct |
29 ms |
50540 KB |
Output is correct |
20 |
Correct |
26 ms |
50540 KB |
Output is correct |
21 |
Correct |
26 ms |
50540 KB |
Output is correct |
22 |
Correct |
27 ms |
50540 KB |
Output is correct |
23 |
Correct |
27 ms |
50540 KB |
Output is correct |
24 |
Correct |
26 ms |
50540 KB |
Output is correct |
25 |
Correct |
25 ms |
50476 KB |
Output is correct |
26 |
Correct |
26 ms |
50540 KB |
Output is correct |
27 |
Correct |
31 ms |
50540 KB |
Output is correct |
28 |
Correct |
30 ms |
50540 KB |
Output is correct |
29 |
Correct |
26 ms |
50540 KB |
Output is correct |
30 |
Correct |
33 ms |
50796 KB |
Output is correct |
31 |
Correct |
32 ms |
50816 KB |
Output is correct |
32 |
Correct |
33 ms |
50924 KB |
Output is correct |
33 |
Correct |
32 ms |
50796 KB |
Output is correct |
34 |
Correct |
37 ms |
50796 KB |
Output is correct |
35 |
Correct |
30 ms |
50668 KB |
Output is correct |
36 |
Correct |
30 ms |
50668 KB |
Output is correct |
37 |
Correct |
29 ms |
50668 KB |
Output is correct |
38 |
Correct |
30 ms |
50668 KB |
Output is correct |
39 |
Correct |
29 ms |
50668 KB |
Output is correct |
40 |
Correct |
30 ms |
50668 KB |
Output is correct |
41 |
Correct |
31 ms |
50668 KB |
Output is correct |
42 |
Correct |
29 ms |
50668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
424 ms |
67092 KB |
Output is correct |
2 |
Correct |
440 ms |
66924 KB |
Output is correct |
3 |
Correct |
426 ms |
67052 KB |
Output is correct |
4 |
Correct |
436 ms |
67052 KB |
Output is correct |
5 |
Correct |
450 ms |
67564 KB |
Output is correct |
6 |
Correct |
480 ms |
66284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1105 ms |
84204 KB |
Output is correct |
2 |
Correct |
1012 ms |
83376 KB |
Output is correct |
3 |
Correct |
1098 ms |
83308 KB |
Output is correct |
4 |
Correct |
1016 ms |
83564 KB |
Output is correct |
5 |
Correct |
1018 ms |
83564 KB |
Output is correct |
6 |
Correct |
1025 ms |
80748 KB |
Output is correct |
7 |
Correct |
1024 ms |
82924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
50540 KB |
Output is correct |
2 |
Correct |
32 ms |
50512 KB |
Output is correct |
3 |
Correct |
31 ms |
50540 KB |
Output is correct |
4 |
Correct |
27 ms |
50540 KB |
Output is correct |
5 |
Correct |
26 ms |
50540 KB |
Output is correct |
6 |
Correct |
26 ms |
50540 KB |
Output is correct |
7 |
Correct |
26 ms |
50540 KB |
Output is correct |
8 |
Correct |
27 ms |
50540 KB |
Output is correct |
9 |
Correct |
28 ms |
50540 KB |
Output is correct |
10 |
Correct |
28 ms |
50540 KB |
Output is correct |
11 |
Correct |
27 ms |
50688 KB |
Output is correct |
12 |
Correct |
26 ms |
50540 KB |
Output is correct |
13 |
Correct |
26 ms |
50540 KB |
Output is correct |
14 |
Correct |
26 ms |
50540 KB |
Output is correct |
15 |
Correct |
27 ms |
50540 KB |
Output is correct |
16 |
Correct |
27 ms |
50560 KB |
Output is correct |
17 |
Correct |
28 ms |
50540 KB |
Output is correct |
18 |
Correct |
27 ms |
50540 KB |
Output is correct |
19 |
Correct |
29 ms |
50540 KB |
Output is correct |
20 |
Correct |
26 ms |
50540 KB |
Output is correct |
21 |
Correct |
26 ms |
50540 KB |
Output is correct |
22 |
Correct |
27 ms |
50540 KB |
Output is correct |
23 |
Correct |
27 ms |
50540 KB |
Output is correct |
24 |
Correct |
26 ms |
50540 KB |
Output is correct |
25 |
Correct |
25 ms |
50476 KB |
Output is correct |
26 |
Correct |
26 ms |
50540 KB |
Output is correct |
27 |
Correct |
31 ms |
50540 KB |
Output is correct |
28 |
Correct |
30 ms |
50540 KB |
Output is correct |
29 |
Correct |
26 ms |
50540 KB |
Output is correct |
30 |
Correct |
33 ms |
50796 KB |
Output is correct |
31 |
Correct |
32 ms |
50816 KB |
Output is correct |
32 |
Correct |
33 ms |
50924 KB |
Output is correct |
33 |
Correct |
32 ms |
50796 KB |
Output is correct |
34 |
Correct |
37 ms |
50796 KB |
Output is correct |
35 |
Correct |
30 ms |
50668 KB |
Output is correct |
36 |
Correct |
30 ms |
50668 KB |
Output is correct |
37 |
Correct |
29 ms |
50668 KB |
Output is correct |
38 |
Correct |
30 ms |
50668 KB |
Output is correct |
39 |
Correct |
29 ms |
50668 KB |
Output is correct |
40 |
Correct |
30 ms |
50668 KB |
Output is correct |
41 |
Correct |
31 ms |
50668 KB |
Output is correct |
42 |
Correct |
29 ms |
50668 KB |
Output is correct |
43 |
Correct |
424 ms |
67092 KB |
Output is correct |
44 |
Correct |
440 ms |
66924 KB |
Output is correct |
45 |
Correct |
426 ms |
67052 KB |
Output is correct |
46 |
Correct |
436 ms |
67052 KB |
Output is correct |
47 |
Correct |
450 ms |
67564 KB |
Output is correct |
48 |
Correct |
480 ms |
66284 KB |
Output is correct |
49 |
Correct |
1105 ms |
84204 KB |
Output is correct |
50 |
Correct |
1012 ms |
83376 KB |
Output is correct |
51 |
Correct |
1098 ms |
83308 KB |
Output is correct |
52 |
Correct |
1016 ms |
83564 KB |
Output is correct |
53 |
Correct |
1018 ms |
83564 KB |
Output is correct |
54 |
Correct |
1025 ms |
80748 KB |
Output is correct |
55 |
Correct |
1024 ms |
82924 KB |
Output is correct |
56 |
Correct |
438 ms |
66436 KB |
Output is correct |
57 |
Correct |
473 ms |
66612 KB |
Output is correct |
58 |
Correct |
444 ms |
66924 KB |
Output is correct |
59 |
Correct |
446 ms |
65516 KB |
Output is correct |
60 |
Correct |
451 ms |
66924 KB |
Output is correct |
61 |
Correct |
461 ms |
67052 KB |
Output is correct |
62 |
Correct |
469 ms |
65772 KB |
Output is correct |
63 |
Correct |
457 ms |
66924 KB |
Output is correct |
64 |
Correct |
535 ms |
67052 KB |
Output is correct |
65 |
Correct |
539 ms |
67820 KB |
Output is correct |
66 |
Correct |
604 ms |
69612 KB |
Output is correct |
67 |
Correct |
710 ms |
71404 KB |
Output is correct |
68 |
Correct |
1043 ms |
80624 KB |
Output is correct |
69 |
Correct |
1066 ms |
82156 KB |
Output is correct |
70 |
Correct |
818 ms |
75224 KB |
Output is correct |