Codeforces Global Round 18


A. Closing The Gap
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
3
10 10 10
4
3 2 1 2
5
1 2 3 1 5
Output
0
0
1
----------------------------------------------------------------------------------------------------
B. And It's Non-Zero
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
1 2
2 8
4 5
1 5
100000 200000
Output
1
3
0
2
31072
----------------------------------------------------------------------------------------------------
C. Menorah
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
5
11010
11010
2
01
11
3
000
101
9
100010111
101101100
9
001011011
011010101
Output
0
1
-1
3
4
----------------------------------------------------------------------------------------------------
D. X(or)-mas Tree
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4
6 5
1 2 -1
1 3 1
4 2 7
6 3 0
2 5 -1
2 3 1
2 5 0
5 6 1
6 1 1
4 5 1
5 3
1 2 -1
1 3 -1
1 4 1
4 5 -1
2 4 0
3 4 1
2 3 1
3 3
1 2 -1
1 3 -1
1 2 0
1 3 1
2 3 0
2 1
1 2 1
1 2 0
Output
YES
1 2 0
1 3 1
2 4 7
3 6 0
2 5 0
YES
1 2 1
1 3 0
1 4 1
4 5 1
NO
NO
----------------------------------------------------------------------------------------------------
E. Purple Crayon
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4 2
1 2
1 3
1 4
Output
1
Input
5 2
1 2
2 3
3 4
4 5
Output
6
Input
7 2
1 2
1 3
4 2
3 5
6 3
6 7
Output
4
Input
4 1
1 2
1 3
1 4
Output
-1
----------------------------------------------------------------------------------------------------
F. LEGOndary Grandmaster
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
6
2
00
11
3
???
???
3
??1
0?0
4
??0?
??11
5
?????
0??1?
10
?01??01?1?
??100?1???
Output
1
16
1
14
101
1674
----------------------------------------------------------------------------------------------------
G. Maximum Adjacent Pairs
time limit per test: 3 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
4
1 1 0 2
Output
1 1 2 2 
Input
5
0 0 0 0 0
Output
3 1 1 3 3
Input
5
1 2 3 4 5
Output
1 2 3 4 5 
Input
6
1 0 0 0 0 1
Output
1 2 3 3 1 1
Input
3
3 0 2
Output
3 2 2 
Input
5
1 0 2 0 1
Output
1 2 2 1 1 
Input
7
1 0 2 3 1 0 2
Output
1 2 2 3 1 1 2 
----------------------------------------------------------------------------------------------------
H. Reindeer Games
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
7 6
3 1 4 9 2 5 6
1 2
2 3
3 4
4 5
5 6
6 7
Output
1 1 4 4 4 5 6 
Input
4 6
6 5 8 2
3 1
4 1
3 2
1 2
2 3
3 1
Output
6 6 6 2 
Input
10 18
214 204 195 182 180 176 176 172 169 167
1 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
10 2
6 1
6 2
6 3
6 4
6 5
6 7
6 8
6 9
6 10
Output
204 204 195 182 180 167 176 172 169 167 
----------------------------------------------------------------------------------------------------
