Codeforces Round 397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)


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

Examples
Input
4
SVO
SVO->CDG
LHR->SVO
SVO->LHR
CDG->SVO
Output
home
Input
3
SVO
SVO->HKT
HKT->SVO
SVO->RAP
Output
contest
----------------------------------------------------------------------------------------------------
B. Code obfuscation
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
abacaba
Output
YES
Input
jinotega
Output
NO
----------------------------------------------------------------------------------------------------
C. Table Tennis Game 2
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
11 11 5
Output
1
Input
11 2 3
Output
-1
----------------------------------------------------------------------------------------------------
D. Artsem and Saunders
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
3
1 2 3
Output
3
1 2 3
1 2 3
Input
3
2 2 2
Output
1
1 1 1
2
Input
2
2 1
Output
-1
----------------------------------------------------------------------------------------------------
E. Tree Folding
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

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

Examples
Input
8
3 1 4 1 5 9 2 6
4
1 8
1 3
4 8
5 7
Output
0
1
1
3
----------------------------------------------------------------------------------------------------
G. Math, math everywhere
time limit per test: 5 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
1
2
2 1
3 1
Output
2
Input
01
2
3 2
5 1
Output
15
Input
1011
1
3 1000000000
Output
411979884
----------------------------------------------------------------------------------------------------
