Security Company
InterviewTime limit2sMemory limit128 MB
Points lie on a line with travel times between neighbors; starting from point a and visiting every point, minimize the total first-arrival time over all points.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy, Array, Prefix sum
- Solved
- No attempts yet
Problem
Myungwoo works for a security company and is responsible for patrolling, on foot, several stores near a subway station.
The area can be represented as a single line segment. Myungwoo's office and the stores are placed, from left to right, on points on this segment. The office is at , and this point is the starting point . Myungwoo starts patrolling at and must visit every store at least once.
The time needed to travel between two adjacent points and is . The waiting time of point is the time elapsed from leaving the start until first arriving at . The waiting time of the start itself is . Myungwoo must choose a patrol route that minimizes the sum of the waiting times of all stores.
For example, suppose there are stores through , the start is , and , , , , . If Myungwoo starts by walking right from , the waiting times and become and . With a suitable patrol order the sum of the waiting times can be made , and no method achieves a smaller sum.
Given the number of points and the travel times () between adjacent points, write a program that finds the minimum possible sum of the waiting times.
Input
The first line contains the number of test cases . Each test case is structured as follows.
- The first line contains the number of stores ().
- The second line contains the position of the starting point (). The -th point is the starting point.
- Each of the next lines, the -th of them, contains ().
Output
For each test case, print on its own line the minimum sum of the waiting times over all ways of patrolling every store.