I. Parkour
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output
Example
Input
7
17 1 11 13 6 12 16
5
1 4 2
1 5 8
1 7 9
2 6 7 2 17
2 6 6 6 8
Output
45
0