AmazonOA #137
Drone Delivery Route
Estimated EasyJudge-ready
Topics (AI-labelled)Prefix & Ranges
m stations form a two-way ring, and the edge lengths give the flight time between adjacent stations. The drone starts at station 1 and must reach all requested stations in order; find the minimum total time. A repeated visit to the same station requires no movement.
Input
The first line contains m q, the second line contains m edge lengths, and the third line contains q 1-based requested stations. 1≤m,q≤100000, edge lengths 1..10^9.
Output
Output a single integer answer.
Example 1
Input:
3 4
3 2 1
1 3 3 2
Output:
3
Example 2
Input:
4 3
5 1 2 6
2 4 1
Output:
14
Example 3
Input:
1 2
7
1 1
Output:
0
Example notes
Example 1: 1→1 costs 0, 1→3 costs 1, 3→3 costs 0, and 3→2 costs 2, for a total of 3. Example 2: costs 5, 3 and 6, for a total of 14. Example 3: the drone always stays at the only station, so the answer is 0.
Editorial
Loading solution…