Codeforces Round 121 (Div. 1)


A. Dynasty Puzzles
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
abc
ca
cba
Output
6
Input
4
vvp
vvp
dam
vvp
Output
0
Input
3
ab
c
def
Output
1
----------------------------------------------------------------------------------------------------
B. Demonstration
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5 2
8
2 4 5 3 1
Output
2
Input
5 2
8
3 2 4 1 5
Output
5
Input
5 4
1000000000000000
5 4 3 2 1
Output
5
----------------------------------------------------------------------------------------------------
C. Fools and Roads
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
1 2
1 3
2 4
2 5
2
1 4
3 5
Output
2 1 1 1 
Input
5
3 4
4 5
1 4
2 4
3
2 3
1 3
3 5
Output
3 1 1 1 
----------------------------------------------------------------------------------------------------
D. Metro Scheme
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

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

Examples
Input
3 4
1 4 2
Output
4
Input
4 6
2 -1 2 -1
Output
1
Input
8 10
1 -2 3 -4 5 -6 7 -8
Output
2
----------------------------------------------------------------------------------------------------
