Codeforces Round 178 (Div. 2)


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

Examples
Input
5
10 10 10 10 10
5
2 5
3 13
2 12
1 13
4 6
Output
0
12
5
0
16
Input
3
2 4 1
1
2 2
Output
3
0
3
----------------------------------------------------------------------------------------------------
B. Shaass and Bookshelf
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
----------------------------------------------------------------------------------------------------
C. Shaass and Lights
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3 1
1
Output
1
Input
4 2
1 4
Output
2
Input
11 2
4 8
Output
6720
----------------------------------------------------------------------------------------------------
D. Shaass and Painter Robot
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3 4
1 1 DR
Output
7
Input
3 4
3 3 DR
Output
11
Input
3 3
1 1 DR
Output
-1
Input
3 3
1 2 DL
Output
4
----------------------------------------------------------------------------------------------------
E. Shaass the Great
time limit per test: 3.5 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Examples
Input
3
1 2 2
1 3 4
Output
12
Input
6
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
Output
29
Input
6
1 3 1
2 3 1
3 4 100
4 5 2
4 6 1
Output
825
----------------------------------------------------------------------------------------------------
