Codeforces Round 543 (Div. 1, based on Technocup 2019 Final Round)


A. Diana and Liana
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
7 3 2 2
1 2 3 3 2 1 2
2 2
Output
1
4 
Input
13 4 3 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
-1
Input
13 4 1 3
3 2 6 4 1 4 4 7 1 3 3 2 4
4 3 4
Output
9
1 2 3 4 5 9 11 12 13
----------------------------------------------------------------------------------------------------
B. Once in a casino
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
223
322
Output
2
1 1
2 -1
Input
2
20
42
Output
2
1 1
1 1
Input
2
35
44
Output
-1
----------------------------------------------------------------------------------------------------
C. Compress String
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3 3 1
aba
Output
7
Input
4 1 1
abcd
Output
4
Input
4 10 1
aaaa
Output
12
----------------------------------------------------------------------------------------------------
D. Power Tree
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
5 1 3 2 1
1 2
2 3
2 4
1 5
Output
4 3
2 4 5 
Input
3
1 1 1
1 2
1 3
Output
2 3
1 2 3 
----------------------------------------------------------------------------------------------------
E. The very same Munchhausen
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
2
Output
6
Input
3
Output
6669
Input
10
Output
-1
----------------------------------------------------------------------------------------------------
F. Secret Letters
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
5 1 4
0 P
1 W
3 P
5 P
8 P
10
Output
16
Input
10 10 94
17 W
20 W
28 W
48 W
51 P
52 W
56 W
62 P
75 P
78 P
87
Output
916
----------------------------------------------------------------------------------------------------
