Where to Build a Brewery?
Time limit3sMemory limit512 MB
On a ring of cities with given edge lengths and demands, pick the city minimizing total demand-weighted shortest-path distance around the ring.
- Level
Medium7 of 10
- Topics
- Prefix sum, Two pointers, Greedy, Math
- Solved
- No attempts yet
Problem
The residents of the island of Abstinence love alcohol-free beer. Until now it was imported from Poland, but this year one of the island's cities will build a brewery. Every city sits on the coast, and they are all linked by a single highway that runs around the island along the shore, so the cities form one big ring. The investor has gathered, for every city, the number of beer tanks it needs each day (its demand) and the distances between neighbouring cities. Moving one tank of beer one mile costs 1 thaler. The daily transport cost is the total cost of shipping the required number of tanks from the brewery to every city, where the beer for each city travels along the shorter of the two directions around the ring. This cost depends on where the brewery is built, and the investor wants to choose the city that makes it as small as possible.
Write a program that
- reads the number of cities, the distances between them, and each city's daily beer demand,
- computes the minimal daily transport cost,
- writes the result to standard output.
Input
The first line contains one integer , the number of cities (). Cities are numbered along the highway, so neighbouring cities have consecutive numbers, and cities and are neighbours too. Each of the next lines contains two non-negative integers separated by a single space: and . Here is the daily beer demand of city , and is the distance in miles from city to the next city along the highway. The total length of the highway does not exceed miles. The demand of each city does not exceed tanks.
Output
Print a single integer: the minimal daily transport cost.