Codeforces Round 802 (Div. 2)


A. Optimal Path
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
7
1 1
2 3
3 2
7 1
1 10
5 5
10000 10000
Output
1
12
13
28
55
85
500099995000
----------------------------------------------------------------------------------------------------
B. Palindromic Numbers
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
2
99
4
1023
3
385
Output
32
8646
604
----------------------------------------------------------------------------------------------------
C. Helping the Nature
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4
3
-2 -2 -2
3
10 4 7
4
4 -4 4 -4
5
1 -2 3 -4 5
Output
2
13
36
33
----------------------------------------------------------------------------------------------------
D. River Locks
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
4 1 5 4 1
6
1
6
2
3
4
5
Output
-1
3
-1
-1
4
3
Input
5
4 4 4 4 4
6
1
3
6
5
2
4
Output
-1
-1
4
4
-1
5
----------------------------------------------------------------------------------------------------
E. Serega the Pirate
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3 3
2 1 3
6 7 4
9 8 5
Output
0
Input
2 3
1 6 4
3 2 5
Output
1 3
Input
1 6
1 6 5 4 3 2
Output
2
----------------------------------------------------------------------------------------------------
F. Puzzle
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
0 1 0 1 0
1 1 0 0 1
1 0 1 0 1
0 0 1 1 0
Output
5
Input
3
1 0 0
0 0 0
0 0 0
0 0 0
Output
-1
----------------------------------------------------------------------------------------------------
