Dome Stadium
InterviewTime limit1sMemory limit256 MB
Choose the village coordinate that minimizes the total fan-weighted travel distance.
- Level
Medium4 of 10
- Topics
- Prefix sum
- Solved
- No attempts yet
Problem
A baseball team is planning to build a domed stadium. The city is laid out as villages in a straight line along a single highway, and each village sits at a distinct integer coordinate on the x-axis. From a survey, the team already knows how many baseball fans live in each village.
The team wants to place the stadium in one of the villages so that the total travel distance from every fan to the stadium is as small as possible. A village that holds fans and sits at distance from the stadium contributes to this total. In other words, if a village's coordinate is and the chosen stadium coordinate is , the total distance is the sum of over all villages.
For example, suppose there are 5 villages at coordinates with fan counts . Building the stadium in the village at x-coordinate minimizes the total distance from all fans.
Write a program that finds the village coordinate that minimizes the total distance from every fan to the stadium. The stadium must be built in one of the villages, and every village lies on an integer x-coordinate.
Input
The input is read from standard input. The first line contains an integer , the number of test cases. Each test case is given on three lines.
- The first line contains an integer (), the number of villages.
- The second line contains integers, the village coordinates on the x-axis in ascending order. Each coordinate is between and .
- The third line contains positive integers, the number of fans in each village. Each count is between and .
Output
Write to standard output. For each test case, print exactly one line containing a single integer: the coordinate of the village that minimizes the total distance from all fans to the stadium. If several villages achieve the same minimum, print the smallest such coordinate.