Codeforces Round 507 (Div. 1, based on Olympiad of Metropolises)


A. Timetable
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
3 10
4 6 8
2 2 3
Output
Yes
16 17 21 
Input
2 1
1 2
2 1
Output
No
----------------------------------------------------------------------------------------------------
B. Subway Pursuit
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
10 2
Yes
No
Yes
Yes
Output
3 5
3 3
3 4
5 5
----------------------------------------------------------------------------------------------------
C. Network Safety
time limit per test: 3 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
4 4 2
0 1 0 1
1 2
2 3
3 4
4 1
Output
50
Input
4 5 3
7 1 7 2
1 2
2 3
3 4
4 1
2 4
Output
96
----------------------------------------------------------------------------------------------------
D. You Are Given a Tree
time limit per test: 7 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
7
1 2
2 3
3 4
4 5
5 6
6 7
Output
7
3
2
1
1
1
1
Input
6
1 2
2 3
2 4
1 5
5 6
Output
6
2
2
1
1
0
----------------------------------------------------------------------------------------------------
E. Summer Oenothera Exhibition
time limit per test: 7 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

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