Codeforces Round 540 (Div. 3)


A. Water Buying
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4
10 1 3
7 3 2
1 1000 1
1000000000000 42 88
Output
10
9
1000
42000000000000
----------------------------------------------------------------------------------------------------
B. Tanya and Candies
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
7
5 5 4 5 5 5 6
Output
2
Input
8
4 8 8 7 8 4 4 5
Output
2
Input
9
2 3 4 2 2 3 2 2 4
Output
3
----------------------------------------------------------------------------------------------------
C. Palindromic Matrix
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4
1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1
Output
YES
1 2 2 1
8 2 2 8
8 2 2 8
1 2 2 1
Input
3
1 1 1 1 1 3 3 3 3
Output
YES
1 3 1
3 1 3
1 3 1
Input
4
1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1
Output
NO
Input
1
10
Output
YES
10 
----------------------------------------------------------------------------------------------------
D1. Coffee and Coursework (Easy version)
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5 8
2 3 1 1 2
Output
4
Input
7 10
1 3 4 2 1 4 2
Output
2
Input
5 15
5 5 5 5 5
Output
1
Input
5 16
5 5 5 5 5
Output
2
Input
5 26
5 5 5 5 5
Output
-1
----------------------------------------------------------------------------------------------------
D2. Coffee and Coursework (Hard Version)
time limit per test: 2.5 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5 8
2 3 1 1 2
Output
4
Input
7 10
1 3 4 2 1 4 2
Output
2
Input
5 15
5 5 5 5 5
Output
1
Input
5 16
5 5 5 5 5
Output
2
Input
5 26
5 5 5 5 5
Output
-1
----------------------------------------------------------------------------------------------------
E. Yet Another Ball Problem
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4 3
Output
YES
3 1
1 3
3 2
2 3
Input
10 4
Output
YES
2 1
1 3
4 2
3 4
4 3
3 2
2 4
4 1
1 4
3 1
Input
13 4
Output
NO
----------------------------------------------------------------------------------------------------
F1. Tree Cutting (Easy Version)
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
2 0 0 1 2
1 2
2 3
2 4
2 5
Output
1
Input
5
1 0 0 0 2
1 2
2 3
3 4
4 5
Output
4
Input
3
1 1 2
2 3
1 3
Output
0
----------------------------------------------------------------------------------------------------
F2. Tree Cutting (Hard Version)
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5 2
2 0 0 1 2
1 2
2 3
2 4
2 5
Output
1
Input
7 3
0 1 0 2 2 3 0
1 3
1 4
1 5
2 7
3 6
4 7
Output
4
----------------------------------------------------------------------------------------------------
