Kotlin Heroes: Episode 10


A. 1-3-5
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
5
7
8
42
2
11
Output
1
0
0
2
0
----------------------------------------------------------------------------------------------------
B. Clock in the Pool
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
1 1
5 14
5 15
10 110
99999999 1000000000
Output
1
0
10
0
99999989
----------------------------------------------------------------------------------------------------
C. Firewood
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
4
2 2
2 1
10 3
50 36679020707840
Output
1
2
10
16
----------------------------------------------------------------------------------------------------
D. Staircase
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
6
5
0 0 0 0 0
4
0 13 15 8
4
13 15 0 8
8
1 2 3 4 5 6 7 8
5
99999999 100000000 99999999 99999999 99999999
5
2 3 4 3 2
Output
0
59
64
72
899999993
24
----------------------------------------------------------------------------------------------------
E. Yet Another Permutation Constructive
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
4
5 2
5 4
2 1
3 2
Output
3 1 2 5 4
-1
1 2
2 1 3
----------------------------------------------------------------------------------------------------
F. Narrow Paths
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
2 2
Output
1 0 0
Input
10 2
Output
45 24 21 18 15 12 9 6 3 0 0
Input
10 5
Output
7812 420 210 90 30 6 0 0 0 0 0
Input
22 10
Output
467563090 1847560 1016158 534820 267410 125840 55055 22022 7865 2420 605 110 11 0 0 0 0 0 0 0 0 0 0
----------------------------------------------------------------------------------------------------
G. Observation Towers
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
20 3
2 15 10
6 2 0
3
1 20
10 11
3 4
Output
3 1 0
Input
10 2
2 9
1 2
3
4 5
5 6
1 10
Output
2 2 0
----------------------------------------------------------------------------------------------------
H. Composite Spells
time limit per test: 4 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4
4 9
1 -2 3 -4
3
3 1 4 3
4 1 2 1 2
6 6 5 6 5 6 5
4 9
1 -2 3 -4
3
3 1 4 3
4 1 2 1 2
7 6 5 6 5 6 6 5
3 31
-10 -20 30
1
6 1 2 3 1 2 3
6 20
-1 -5 -9 -7 -1 -1
4
3 6 5 2
4 3 3 7 6
6 4 8 4 4 6 7
3 6 5 7
Output
4
4
-1
-1
----------------------------------------------------------------------------------------------------
I. Equal Trees
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
1 5 5 1
1 4 1 2
Output
4
Input
2
1
1
Output
0
Input
10
4 7 10 6 2 9 7 1 1
1 2 10 3 4 6 6 5 5
Output
10
----------------------------------------------------------------------------------------------------
J. Necromancer
time limit per test: 6 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

Examples
Input
7 5
4 5 9 9 4 2 4
1 3 3 1 2 3 3
3 5
1 4
6 6
1 7
2 6
Output
4
10
0
13
7
----------------------------------------------------------------------------------------------------
