#include <bits/stdc++.h>
using namespace std;
const int kN = 2e5;
const int64_t INF = 2e18L;
int a[2 * kN], v[1 + kN];
tuple<int, int, int> intervals[2 * kN];
vector<pair<int, int>> rEnd[1 + kN];
void minSelf(int64_t &x, int64_t y) {
if (y < x) {
x = y;
}
}
struct ST {
int n;
vector<int64_t> t, lazy;
ST(int N) : n(N) {
int dim = 1;
while (dim < n + 1) {
dim *= 2;
}
dim *= 2;
t.resize(dim);
lazy.resize(dim);
}
void push(int x) {
if (lazy[x] == 0) {
return;
}
for (int y = 2 * x; y <= 2 * x + 1; ++y) {
t[y] += lazy[x];
lazy[y] += lazy[x];
}
lazy[x] = 0;
}
void rangeAdd(int x, int lx, int rx, int st, int dr, int64_t v) {
if (st <= lx && rx <= dr) {
t[x] += v;
lazy[x] += v;
return;
}
push(x);
int mid = (lx + rx) / 2;
if (st <= mid) {
rangeAdd(x * 2, lx, mid, st, dr, v);
}
if (mid < dr) {
rangeAdd(x * 2 + 1, mid + 1, rx, st, dr, v);
}
t[x] = min(t[x * 2], t[x * 2 + 1]);
}
void rangeAdd(int st, int dr, int64_t v) {
rangeAdd(1, 0, n, st, dr, v);
}
int64_t query(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
return t[x];
}
push(x);
int mid = (lx + rx) / 2;
int64_t ans = INF;
if (st <= mid) {
minSelf(ans, query(x * 2, lx, mid, st, dr));
}
if (mid < dr) {
minSelf(ans, query(x * 2 + 1, mid + 1, rx, st, dr));
}
return ans;
}
int64_t query(int st, int dr) {
return query(1, 0, n, st, dr);
}
};
int64_t solve(int n) {
ST t(n);
for (int i = 1; i <= n; ++i) {
t.rangeAdd(i, i, t.query(0, i - 1));
t.rangeAdd(0, i - 1, v[i]);
for (auto it : rEnd[i]) {
int start, cost;
tie(start, cost) = it;
t.rangeAdd(start, i, cost);
}
}
return t.t[1];
}
void testCase() {
int n, m;
cin >> n >> m;
int N = n + m - 1;
for (int i = 1; i <= N; ++i) {
cin >> a[i];
}
int cost;
for (int i = 1; i <= N; ++i) {
cin >> cost;
int y = min(i - 1, m - 1);
int x = i - 1 - y;
int index = x - y + m;
int len = min(y + 1, n - x);
intervals[i] = {index, len, cost};
}
int64_t ans = 0;
for (int par = 0; par <= 1; ++par) {
if (par == 0 && n == 1 && m == 1) {
continue;
}
int p = get<0>(intervals[2 - par]) % 2;
int M = (N + p) / 2;
for (int i = 2 - p; i <= N; i += 2) {
v[(i + p) / 2] = a[i];
}
for (int i = 1; i <= M; ++i) {
rEnd[i].clear();
}
for (int i = 2 - par; i <= N; i += 2) {
int index, len, cost;
tie(index, len, cost) = intervals[i];
rEnd[(index + 1) / 2 + len - 1].emplace_back((index + 1) / 2, cost);
}
ans += solve(M);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4960 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5028 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4960 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5028 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
2 ms |
5036 KB |
Output is correct |
25 |
Correct |
3 ms |
5028 KB |
Output is correct |
26 |
Correct |
2 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
5028 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4960 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5028 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
2 ms |
5036 KB |
Output is correct |
25 |
Correct |
3 ms |
5028 KB |
Output is correct |
26 |
Correct |
2 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
5028 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
30 |
Correct |
6 ms |
5204 KB |
Output is correct |
31 |
Correct |
6 ms |
5204 KB |
Output is correct |
32 |
Correct |
6 ms |
5204 KB |
Output is correct |
33 |
Correct |
6 ms |
5172 KB |
Output is correct |
34 |
Correct |
5 ms |
5236 KB |
Output is correct |
35 |
Correct |
5 ms |
5168 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5172 KB |
Output is correct |
38 |
Correct |
5 ms |
5072 KB |
Output is correct |
39 |
Correct |
4 ms |
5152 KB |
Output is correct |
40 |
Correct |
5 ms |
5172 KB |
Output is correct |
41 |
Correct |
7 ms |
5180 KB |
Output is correct |
42 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
197 ms |
15780 KB |
Output is correct |
2 |
Correct |
172 ms |
19664 KB |
Output is correct |
3 |
Correct |
174 ms |
19628 KB |
Output is correct |
4 |
Correct |
191 ms |
19908 KB |
Output is correct |
5 |
Correct |
191 ms |
20108 KB |
Output is correct |
6 |
Correct |
171 ms |
19108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
456 ms |
23420 KB |
Output is correct |
2 |
Correct |
440 ms |
23372 KB |
Output is correct |
3 |
Correct |
449 ms |
23420 KB |
Output is correct |
4 |
Correct |
433 ms |
23400 KB |
Output is correct |
5 |
Correct |
457 ms |
23420 KB |
Output is correct |
6 |
Correct |
430 ms |
23388 KB |
Output is correct |
7 |
Correct |
459 ms |
23432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5028 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
5032 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4960 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
5028 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5028 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
2 ms |
5036 KB |
Output is correct |
25 |
Correct |
3 ms |
5028 KB |
Output is correct |
26 |
Correct |
2 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
5028 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
30 |
Correct |
6 ms |
5204 KB |
Output is correct |
31 |
Correct |
6 ms |
5204 KB |
Output is correct |
32 |
Correct |
6 ms |
5204 KB |
Output is correct |
33 |
Correct |
6 ms |
5172 KB |
Output is correct |
34 |
Correct |
5 ms |
5236 KB |
Output is correct |
35 |
Correct |
5 ms |
5168 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
5 ms |
5172 KB |
Output is correct |
38 |
Correct |
5 ms |
5072 KB |
Output is correct |
39 |
Correct |
4 ms |
5152 KB |
Output is correct |
40 |
Correct |
5 ms |
5172 KB |
Output is correct |
41 |
Correct |
7 ms |
5180 KB |
Output is correct |
42 |
Correct |
4 ms |
5204 KB |
Output is correct |
43 |
Correct |
197 ms |
15780 KB |
Output is correct |
44 |
Correct |
172 ms |
19664 KB |
Output is correct |
45 |
Correct |
174 ms |
19628 KB |
Output is correct |
46 |
Correct |
191 ms |
19908 KB |
Output is correct |
47 |
Correct |
191 ms |
20108 KB |
Output is correct |
48 |
Correct |
171 ms |
19108 KB |
Output is correct |
49 |
Correct |
456 ms |
23420 KB |
Output is correct |
50 |
Correct |
440 ms |
23372 KB |
Output is correct |
51 |
Correct |
449 ms |
23420 KB |
Output is correct |
52 |
Correct |
433 ms |
23400 KB |
Output is correct |
53 |
Correct |
457 ms |
23420 KB |
Output is correct |
54 |
Correct |
430 ms |
23388 KB |
Output is correct |
55 |
Correct |
459 ms |
23432 KB |
Output is correct |
56 |
Correct |
168 ms |
19120 KB |
Output is correct |
57 |
Correct |
181 ms |
19340 KB |
Output is correct |
58 |
Correct |
185 ms |
19608 KB |
Output is correct |
59 |
Correct |
170 ms |
18336 KB |
Output is correct |
60 |
Correct |
215 ms |
19752 KB |
Output is correct |
61 |
Correct |
204 ms |
19612 KB |
Output is correct |
62 |
Correct |
179 ms |
18368 KB |
Output is correct |
63 |
Correct |
211 ms |
19576 KB |
Output is correct |
64 |
Correct |
206 ms |
19708 KB |
Output is correct |
65 |
Correct |
232 ms |
20040 KB |
Output is correct |
66 |
Correct |
262 ms |
20780 KB |
Output is correct |
67 |
Correct |
274 ms |
21712 KB |
Output is correct |
68 |
Correct |
448 ms |
28492 KB |
Output is correct |
69 |
Correct |
482 ms |
30120 KB |
Output is correct |
70 |
Correct |
333 ms |
27512 KB |
Output is correct |