Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)


A. Alternating Sum
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
2 2 3 3
+-+
Output
7
Input
4 1 5 1
-
Output
999999228
----------------------------------------------------------------------------------------------------
B. Destruction of a Tree
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
0 1 2 1 2
Output
YES
1
2
3
5
4
Input
4
0 1 2 3
Output
NO
----------------------------------------------------------------------------------------------------
C. Cutting Rectangle
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
1
1 1 9
Output
3
Input
2
2 3 20
2 4 40
Output
6
Input
2
1 2 5
2 3 5
Output
0
----------------------------------------------------------------------------------------------------
D. Frequency of String
time limit per test: 1.5 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
aaaaa
5
3 a
3 aa
2 aaa
3 aaaa
1 aaaaa
Output
3
4
4
-1
5
Input
abbb
7
4 b
1 ab
3 bb
1 abb
2 bbb
1 a
2 abbb
Output
-1
2
-1
3
-1
1
-1
----------------------------------------------------------------------------------------------------
E. Circles of Waiting
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
0 1 1 1 1
Output
1
Input
1 1 1 1 1
Output
666666674
Input
1 1 2 1 2
Output
538461545
----------------------------------------------------------------------------------------------------
