CF534E.Berland Local Positioning System

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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

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

The fourth line contains mm integers ( 1<=bi<=n1<=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-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.

首页