#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 4e5;
vector<int> t(4 * N) , lz(4 * N);
void push(int u){
t[u * 2] += lz[u];
t[u * 2 + 1] += lz[u];
lz[u * 2] += lz[u];
lz[u * 2 + 1] += lz[u];
lz[u] = 0;
}
void make_new(int n){
for(int i = 0;i <= 4 * n;i ++){
t[i] = lz[i] = 0;
}
}
void pos_assign(int u , int l , int r , int pos , int val){
if(l == r){
t[u] = val;
return;
}
push(u);
int m = (l + r) / 2;
if(pos <= m) pos_assign(u * 2 , l , m , pos , val);
else pos_assign(u * 2 + 1 , m + 1 , r , pos , val);
t[u] = max(t[u * 2] , t[u * 2 + 1]);
}
void range_add(int u , int l , int r , int L , int R , int val){
if(l > r || r < L || l > R) return;
if(L <= l && r <= R){
t[u] += val;
lz[u] += val;
return;
}
push(u);
int m = (l + r) / 2;
range_add(u * 2 , l , m , L , R , val);
range_add(u * 2 + 1 , m + 1 , r , L , R , val);
t[u] = max(t[u * 2] , t[u * 2 + 1]);
}
int get_max(int u , int l , int r , int L , int R){
if(l > r || r < L || l > R) return 0;
if(L <= l && r <= R) return t[u];
push(u);
int m = (l + r) / 2;
return max(get_max(u * 2 , l , m , L , R) , get_max(u * 2 + 1 , m + 1 , r , L , R));
}
void pos_assign(int pos , int val , int n){
if(pos < 0 || n - 1 < pos) return;
pos_assign(1 , 0 , n , pos , val);
}
void range_add(int l , int r , int val , int n){
if(r < l) return;
range_add(1 , 0 , n , l , r , val);
}
int get_max(int l , int r , int n){
if(r < l) return 0LL;
return get_max(1 , 0 , n , l , r);
}
main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int n , m;cin >> n >> m;
vector<int> d1(N) , d2(N) , sz(N);
vector<int> f[2];
vector<vector<vector<pair<int , int>>>> cost(2 , vector<vector<pair<int , int>>>((N)));
for(int i = 0;i < n + m - 1;i ++){
cin >> d1[i];
f[i % 2].push_back(d1[i]);
if(i < min(n , m)) sz[i] = i + 1;
if(min(n , m) <= i && i < max(n , m)) sz[i] = sz[i - 1];
if(i >= max(n , m)) sz[i] = sz[i - 1] - 1;
}
int parity;
for(int i = 0;i < n + m - 1;i ++){
cin >> d2[i];
int st = abs(m - i - 1);
if(i % 2 == 0) parity = st % 2;
st /= 2;/*
cout << i << " " << st << " " << st + sz[i] - 1 << " " << parity << endl;*/
cost[i % 2][st + sz[i] - 1].push_back({d2[i] , st});
}
int ans = 0;
for(int w = 0;w < 2;w ++){
int par = (parity ^ (w == 1)) , all_cost = 0 , N = (int)f[w].size();/*
cout << "par W: " << w << " par: " << par << endl; */
make_new(N);/*
cout << "SZ: " << N << endl;*/
for(int i = 0;i < N;i ++){
int all = get_max(0 , i - 1 , N);
pos_assign(i , all + f[w][i] , N);
for(int k = 0;k < cost[par][i].size();k ++){
int end = cost[par][i][k].second;
int Cost = -cost[par][i][k].first;
range_add(end , i , Cost , N);
}
all_cost += f[w][i];
}/*
cout << "MN: ";
cout << get_max(0 , N - 1 , N) << endl;*/
ans += all_cost - max(0LL , get_max(0 , N - 1 , N));
}
cout << ans << endl;
}
Compilation message
colouring.cpp:70:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
70 | main(){
| ^~~~
colouring.cpp: In function 'int main()':
colouring.cpp:102:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int k = 0;k < cost[par][i].size();k ++){
| ~~^~~~~~~~~~~~~~~~~~~~~
colouring.cpp:95:7: warning: 'parity' may be used uninitialized in this function [-Wmaybe-uninitialized]
95 | int par = (parity ^ (w == 1)) , all_cost = 0 , N = (int)f[w].size();/*
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
63064 KB |
Output is correct |
2 |
Correct |
15 ms |
63012 KB |
Output is correct |
3 |
Correct |
15 ms |
63068 KB |
Output is correct |
4 |
Correct |
15 ms |
63064 KB |
Output is correct |
5 |
Correct |
16 ms |
63064 KB |
Output is correct |
6 |
Correct |
16 ms |
63064 KB |
Output is correct |
7 |
Correct |
17 ms |
63068 KB |
Output is correct |
8 |
Correct |
16 ms |
62976 KB |
Output is correct |
9 |
Correct |
16 ms |
63064 KB |
Output is correct |
10 |
Correct |
16 ms |
63320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
63064 KB |
Output is correct |
2 |
Correct |
15 ms |
63012 KB |
Output is correct |
3 |
Correct |
15 ms |
63068 KB |
Output is correct |
4 |
Correct |
15 ms |
63064 KB |
Output is correct |
5 |
Correct |
16 ms |
63064 KB |
Output is correct |
6 |
Correct |
16 ms |
63064 KB |
Output is correct |
7 |
Correct |
17 ms |
63068 KB |
Output is correct |
8 |
Correct |
16 ms |
62976 KB |
Output is correct |
9 |
Correct |
16 ms |
63064 KB |
Output is correct |
10 |
Correct |
16 ms |
63320 KB |
Output is correct |
11 |
Correct |
15 ms |
63320 KB |
Output is correct |
12 |
Correct |
17 ms |
63064 KB |
Output is correct |
13 |
Correct |
16 ms |
63072 KB |
Output is correct |
14 |
Correct |
18 ms |
63064 KB |
Output is correct |
15 |
Correct |
16 ms |
63064 KB |
Output is correct |
16 |
Correct |
15 ms |
63064 KB |
Output is correct |
17 |
Correct |
15 ms |
62952 KB |
Output is correct |
18 |
Correct |
15 ms |
63064 KB |
Output is correct |
19 |
Correct |
19 ms |
63036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
63064 KB |
Output is correct |
2 |
Correct |
15 ms |
63012 KB |
Output is correct |
3 |
Correct |
15 ms |
63068 KB |
Output is correct |
4 |
Correct |
15 ms |
63064 KB |
Output is correct |
5 |
Correct |
16 ms |
63064 KB |
Output is correct |
6 |
Correct |
16 ms |
63064 KB |
Output is correct |
7 |
Correct |
17 ms |
63068 KB |
Output is correct |
8 |
Correct |
16 ms |
62976 KB |
Output is correct |
9 |
Correct |
16 ms |
63064 KB |
Output is correct |
10 |
Correct |
16 ms |
63320 KB |
Output is correct |
11 |
Correct |
15 ms |
63320 KB |
Output is correct |
12 |
Correct |
17 ms |
63064 KB |
Output is correct |
13 |
Correct |
16 ms |
63072 KB |
Output is correct |
14 |
Correct |
18 ms |
63064 KB |
Output is correct |
15 |
Correct |
16 ms |
63064 KB |
Output is correct |
16 |
Correct |
15 ms |
63064 KB |
Output is correct |
17 |
Correct |
15 ms |
62952 KB |
Output is correct |
18 |
Correct |
15 ms |
63064 KB |
Output is correct |
19 |
Correct |
19 ms |
63036 KB |
Output is correct |
20 |
Correct |
15 ms |
63068 KB |
Output is correct |
21 |
Correct |
16 ms |
63064 KB |
Output is correct |
22 |
Correct |
15 ms |
63068 KB |
Output is correct |
23 |
Correct |
19 ms |
63320 KB |
Output is correct |
24 |
Correct |
15 ms |
63064 KB |
Output is correct |
25 |
Correct |
15 ms |
63064 KB |
Output is correct |
26 |
Correct |
15 ms |
63064 KB |
Output is correct |
27 |
Correct |
16 ms |
63064 KB |
Output is correct |
28 |
Correct |
16 ms |
63064 KB |
Output is correct |
29 |
Correct |
15 ms |
63064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
63064 KB |
Output is correct |
2 |
Correct |
15 ms |
63012 KB |
Output is correct |
3 |
Correct |
15 ms |
63068 KB |
Output is correct |
4 |
Correct |
15 ms |
63064 KB |
Output is correct |
5 |
Correct |
16 ms |
63064 KB |
Output is correct |
6 |
Correct |
16 ms |
63064 KB |
Output is correct |
7 |
Correct |
17 ms |
63068 KB |
Output is correct |
8 |
Correct |
16 ms |
62976 KB |
Output is correct |
9 |
Correct |
16 ms |
63064 KB |
Output is correct |
10 |
Correct |
16 ms |
63320 KB |
Output is correct |
11 |
Correct |
15 ms |
63320 KB |
Output is correct |
12 |
Correct |
17 ms |
63064 KB |
Output is correct |
13 |
Correct |
16 ms |
63072 KB |
Output is correct |
14 |
Correct |
18 ms |
63064 KB |
Output is correct |
15 |
Correct |
16 ms |
63064 KB |
Output is correct |
16 |
Correct |
15 ms |
63064 KB |
Output is correct |
17 |
Correct |
15 ms |
62952 KB |
Output is correct |
18 |
Correct |
15 ms |
63064 KB |
Output is correct |
19 |
Correct |
19 ms |
63036 KB |
Output is correct |
20 |
Correct |
15 ms |
63068 KB |
Output is correct |
21 |
Correct |
16 ms |
63064 KB |
Output is correct |
22 |
Correct |
15 ms |
63068 KB |
Output is correct |
23 |
Correct |
19 ms |
63320 KB |
Output is correct |
24 |
Correct |
15 ms |
63064 KB |
Output is correct |
25 |
Correct |
15 ms |
63064 KB |
Output is correct |
26 |
Correct |
15 ms |
63064 KB |
Output is correct |
27 |
Correct |
16 ms |
63064 KB |
Output is correct |
28 |
Correct |
16 ms |
63064 KB |
Output is correct |
29 |
Correct |
15 ms |
63064 KB |
Output is correct |
30 |
Correct |
18 ms |
63064 KB |
Output is correct |
31 |
Correct |
19 ms |
63068 KB |
Output is correct |
32 |
Correct |
18 ms |
63068 KB |
Output is correct |
33 |
Correct |
18 ms |
63068 KB |
Output is correct |
34 |
Correct |
17 ms |
63100 KB |
Output is correct |
35 |
Correct |
16 ms |
63068 KB |
Output is correct |
36 |
Correct |
16 ms |
63068 KB |
Output is correct |
37 |
Correct |
16 ms |
63032 KB |
Output is correct |
38 |
Correct |
17 ms |
63068 KB |
Output is correct |
39 |
Correct |
16 ms |
63068 KB |
Output is correct |
40 |
Correct |
17 ms |
63068 KB |
Output is correct |
41 |
Correct |
17 ms |
63068 KB |
Output is correct |
42 |
Correct |
16 ms |
63068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
63064 KB |
Output is correct |
2 |
Correct |
158 ms |
65396 KB |
Output is correct |
3 |
Correct |
144 ms |
65200 KB |
Output is correct |
4 |
Correct |
146 ms |
65456 KB |
Output is correct |
5 |
Correct |
145 ms |
65716 KB |
Output is correct |
6 |
Correct |
145 ms |
64792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
365 ms |
66736 KB |
Output is correct |
2 |
Correct |
376 ms |
74556 KB |
Output is correct |
3 |
Correct |
374 ms |
74512 KB |
Output is correct |
4 |
Correct |
372 ms |
74780 KB |
Output is correct |
5 |
Correct |
378 ms |
74676 KB |
Output is correct |
6 |
Correct |
356 ms |
71604 KB |
Output is correct |
7 |
Correct |
374 ms |
74156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
63064 KB |
Output is correct |
2 |
Correct |
15 ms |
63012 KB |
Output is correct |
3 |
Correct |
15 ms |
63068 KB |
Output is correct |
4 |
Correct |
15 ms |
63064 KB |
Output is correct |
5 |
Correct |
16 ms |
63064 KB |
Output is correct |
6 |
Correct |
16 ms |
63064 KB |
Output is correct |
7 |
Correct |
17 ms |
63068 KB |
Output is correct |
8 |
Correct |
16 ms |
62976 KB |
Output is correct |
9 |
Correct |
16 ms |
63064 KB |
Output is correct |
10 |
Correct |
16 ms |
63320 KB |
Output is correct |
11 |
Correct |
15 ms |
63320 KB |
Output is correct |
12 |
Correct |
17 ms |
63064 KB |
Output is correct |
13 |
Correct |
16 ms |
63072 KB |
Output is correct |
14 |
Correct |
18 ms |
63064 KB |
Output is correct |
15 |
Correct |
16 ms |
63064 KB |
Output is correct |
16 |
Correct |
15 ms |
63064 KB |
Output is correct |
17 |
Correct |
15 ms |
62952 KB |
Output is correct |
18 |
Correct |
15 ms |
63064 KB |
Output is correct |
19 |
Correct |
19 ms |
63036 KB |
Output is correct |
20 |
Correct |
15 ms |
63068 KB |
Output is correct |
21 |
Correct |
16 ms |
63064 KB |
Output is correct |
22 |
Correct |
15 ms |
63068 KB |
Output is correct |
23 |
Correct |
19 ms |
63320 KB |
Output is correct |
24 |
Correct |
15 ms |
63064 KB |
Output is correct |
25 |
Correct |
15 ms |
63064 KB |
Output is correct |
26 |
Correct |
15 ms |
63064 KB |
Output is correct |
27 |
Correct |
16 ms |
63064 KB |
Output is correct |
28 |
Correct |
16 ms |
63064 KB |
Output is correct |
29 |
Correct |
15 ms |
63064 KB |
Output is correct |
30 |
Correct |
18 ms |
63064 KB |
Output is correct |
31 |
Correct |
19 ms |
63068 KB |
Output is correct |
32 |
Correct |
18 ms |
63068 KB |
Output is correct |
33 |
Correct |
18 ms |
63068 KB |
Output is correct |
34 |
Correct |
17 ms |
63100 KB |
Output is correct |
35 |
Correct |
16 ms |
63068 KB |
Output is correct |
36 |
Correct |
16 ms |
63068 KB |
Output is correct |
37 |
Correct |
16 ms |
63032 KB |
Output is correct |
38 |
Correct |
17 ms |
63068 KB |
Output is correct |
39 |
Correct |
16 ms |
63068 KB |
Output is correct |
40 |
Correct |
17 ms |
63068 KB |
Output is correct |
41 |
Correct |
17 ms |
63068 KB |
Output is correct |
42 |
Correct |
16 ms |
63068 KB |
Output is correct |
43 |
Correct |
142 ms |
63064 KB |
Output is correct |
44 |
Correct |
158 ms |
65396 KB |
Output is correct |
45 |
Correct |
144 ms |
65200 KB |
Output is correct |
46 |
Correct |
146 ms |
65456 KB |
Output is correct |
47 |
Correct |
145 ms |
65716 KB |
Output is correct |
48 |
Correct |
145 ms |
64792 KB |
Output is correct |
49 |
Correct |
365 ms |
66736 KB |
Output is correct |
50 |
Correct |
376 ms |
74556 KB |
Output is correct |
51 |
Correct |
374 ms |
74512 KB |
Output is correct |
52 |
Correct |
372 ms |
74780 KB |
Output is correct |
53 |
Correct |
378 ms |
74676 KB |
Output is correct |
54 |
Correct |
356 ms |
71604 KB |
Output is correct |
55 |
Correct |
374 ms |
74156 KB |
Output is correct |
56 |
Correct |
150 ms |
64840 KB |
Output is correct |
57 |
Correct |
152 ms |
65088 KB |
Output is correct |
58 |
Correct |
152 ms |
65204 KB |
Output is correct |
59 |
Correct |
158 ms |
64112 KB |
Output is correct |
60 |
Correct |
156 ms |
65452 KB |
Output is correct |
61 |
Correct |
157 ms |
65592 KB |
Output is correct |
62 |
Correct |
155 ms |
63876 KB |
Output is correct |
63 |
Correct |
163 ms |
65204 KB |
Output is correct |
64 |
Correct |
178 ms |
65464 KB |
Output is correct |
65 |
Correct |
196 ms |
65768 KB |
Output is correct |
66 |
Correct |
210 ms |
66736 KB |
Output is correct |
67 |
Correct |
233 ms |
67764 KB |
Output is correct |
68 |
Correct |
370 ms |
71784 KB |
Output is correct |
69 |
Correct |
407 ms |
73532 KB |
Output is correct |
70 |
Correct |
282 ms |
70644 KB |
Output is correct |