测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A9777. Berland Local Positioning System

编程题 普及/提高-

题目描述

The first line contains integer $n$ ( $2<=n<=2·10^{5}$ ) — the number of stops.

The second line contains $n$ integers ( $1<=a_{i}<=10^{9}$ ) — the distance from the $i$ -th stop to the central square. The numbers in the second line go in the increasing order.

The third line contains integer $m$ ( $1<=m<=4·10^{5}$ ) — the number of stops the bus visited on some segment of the path.

The fourth line contains $m$ integers ( $1<=b_{i}<=n$ ) — the sorted list of numbers of the stops visited by the bus on the segment of the path. The number of a stop occurs as many times as it was visited by a bus.

It is guaranteed that the query corresponds to some segment of the path.

输入格式

In the single line please print the distance covered by a bus. If it is impossible to determine it unambiguously, print $-1$ .

输出格式

The first test from the statement demonstrates the first example shown in the statement of the problem.

The second test from the statement demonstrates the second example shown in the statement of the problem.

In the third sample there are two possible paths that have distinct lengths, consequently, the sought length of the segment isn't defined uniquely.

In the fourth sample, even though two distinct paths correspond to the query, they have the same lengths, so the sought length of the segment is defined uniquely.

输入输出样例

输入 #1
6
2 3 5 7 11 13
5
3 4 5 5 6
输出 #1
10
输入 #2
6
2 3 5 7 11 13
9
1 2 2 3 3 4 5 5 6
输出 #2
16
输入 #3
3
10 200 300
4
1 2 2 3
输出 #3
-1
输入 #4
3
1 2 3
4
1 2 2 3
输出 #4
3

说明/提示

The first test from the statement demonstrates the first example shown in the statement of the problem.

The second test from the statement demonstrates the second example shown in the statement of the problem.

In the third sample there are two possible paths that have distinct lengths, consequently, the sought length of the segment isn't defined uniquely.

In the fourth sample, even though two distinct paths correspond to the query, they have the same lengths, so the sought length of the segment is defined uniquely.
上一题 去做题 下一题