Codeforces Round 884 (Div. 1 + Div. 2)


A. Subtraction Game
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
1 4
1 5
9 26
Output
2
6
3
----------------------------------------------------------------------------------------------------
B. Permutations & Primes
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
2
1
5
Output
2 1
1
5 2 1 4 3
----------------------------------------------------------------------------------------------------
C. Particles
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
6
-3 1 4 -1 5 -9
5
998244353 998244353 998244353 998244353 998244353
1
-2718
Output
9
2994733059
-2718
----------------------------------------------------------------------------------------------------
D. Row Major
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4
4
2
1
6
Output
that
is
a
tomato
----------------------------------------------------------------------------------------------------
E. Great Grids
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
4
3 4 4
1 1 2 2
2 1 3 2
1 4 2 3
2 3 3 2
2 7 2
1 1 2 2
1 2 2 1
8 5 4
1 2 2 1
1 5 2 4
7 1 8 2
7 4 8 5
8 5 4
1 2 2 1
1 5 2 4
7 1 8 2
7 5 8 4
Output
YES
NO
YES
NO
----------------------------------------------------------------------------------------------------
F1. Min Cost Permutation (Easy Version)
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
6 -7
3 1 4 1 5 9
3 2
1 3 5
1 2718
2818
Output
9 3 1 4 5 1
1 3 5
2818
----------------------------------------------------------------------------------------------------
F2. Min Cost Permutation (Hard Version)
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
6 -7
3 1 4 1 5 9
3 2
1 3 5
1 2718
2818
Output
9 3 1 4 5 1
1 3 5
2818
----------------------------------------------------------------------------------------------------
G. Tree Weights
time limit per test: 5 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
1 2
1 3
2 4
2 5
31 41 59 26
Output
31
10
18
8
Input
3
1 2
1 3
18 18
Output
-1
Input
9
3 1
4 1
5 9
2 6
5 3
5 8
9 7
9 2
236 205 72 125 178 216 214 117
Output
31
41
59
26
53
58
97
93
----------------------------------------------------------------------------------------------------
H. Multiple of Three Cycles
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
6
3 2
1 4
4 5
2 6
5 1
6 3
Output
32
8
3
2
1
1
Input
3
1 1
2 3
3 2
Output
0
0
0
Input
18
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 1
Output
671571067
353924552
521242461
678960117
896896000
68992000
6272000
627200
62720
7840
1120
160
32
8
2
1
1
1
----------------------------------------------------------------------------------------------------
