cswork / OAOpen practice workspace

AmazonOA #70

Compute Min Delivery Time (Circular Drone Hubs)

Estimated EasyJudge-ready

Topics (AI-labelled)Prefix & Ranges

The i-th transitionTime segment connects hub i and hub i+1, and the last segment connects m and 1; every segment can be traveled in both directions. Starting from 1, visit the requested hubs in order and find the minimum total time. With these edge weights, the answer to the source example is 3, not 4.

Input

The first line contains m n (1≤m≤5000, 1≤n≤200000), the second line contains m edge weights (1..1000000), and the third line contains n requested hub numbers (1..m).

Output

Output a single integer answer.

Example 1

Input:

3 4
5 2 1
1 3 3 2

Output:

3

Example 2

Input:

1 2
7
1 1

Output:

0

Example 3

Input:

2 2
2 3
2 1

Output:

4

Example notes

Example 1: 1→1 takes 0, 1→3 via the last edge takes 1, 3→3 takes 0, and 3→2 takes 2, for a total of 3. Example 2: there is only one hub, so the total time is 0. Example 3: of the two routes between the two hubs, take the shorter one, 2; two round trips total 4.

Editorial

Loading solution…