N Cities

Limits 1s, 512 MB

In a country there are N cities numbered from 1 to N. To Travel along the cities there are total N bi directional roads. For each city numbered i (1 ≤ i < N) there is a bi-directional road between city i and city i+1. Furthermore, there is a road between city N and city 1. People can only use any of these roads to travel from a city to another. As travel technology of this country is very advanced, it requires only 1 second time to travel from city i to city j if there is a road between these two cities. Now initially you will be given the address of M people. That is, for every people i you will be given an integer ai, the city where ith person lives. Then There will be Q queries. In each query you will be given an integer X. You will have to tell the total time that is required for all people to travel from their city to city X. Now as we know everyone hates long time travelling, they will take the shortest path to travel from their cities to city X.


This is a companion discussion topic for the original entry at https://toph.co/p/n-cities