Museum Tour
InterviewTime limit2sMemory limit1024 MB
Given each kitten's viewing order, find the hall where the entrance minimizes the total walking distance for all kittens, choosing the smallest number on ties.
- Level
Medium4 of 10
- Topics
- Prefix sum, Greedy, Array
- Solved
- No attempts yet
Problem
kittens are going on a museum tour, and each has planned the order in which to view the exhibits. The museum is a row of exhibit halls, and the -th hall from the left holds exhibit .
Every kitten enters through the entrance, views the exhibits in the order it wants, and then leaves through the entrance. The distance between the -th and -th halls is .

Chunbae, the museum director, wants to place the entrance in one of the halls. Chunbae wants the spot where the sum of all kittens' travel distances is smallest, but the museum is large and choosing that spot is hard.
Given the viewing plans of the kittens, write a program that computes where the entrance should be placed.
Input
The first line contains and , separated by a space.
The next lines describe the kittens' viewing plans. Each line has the following form.
- : the kitten enters through the entrance, views exhibits in that order, and then leaves through the entrance. ,
Output
Print the number of the exhibit hall where the entrance should be placed. If several halls work, print the smallest number.