Candy Density
InterviewTime limit2sMemory limit512 MB
Choose a candy density d minimizing the sum of |W_i - d*C_i|, and print the minimum as an exact reduced fraction.
- Level
Medium6 of 10
- Topics
- Math, Greedy, Sorting, Binary search
- Solved
- No attempts yet
Problem
Seongwon is a guest at an algorithm camp and makes candy for the participants. There are participants, numbered 0 to .
Participant holds a basket with a capacity of liters and wants to receive grams of candy.
Seongwon fills every basket to its capacity, so participant receives exactly liters of candy. He can make only one kind of candy, so a single density (grams per liter) applies to all of it. He may choose any positive real number for .
Once the density is , participant actually receives grams. To satisfy as many participants as possible, Seongwon picks the that minimizes the total difference between the requested weight and the received weight:
Write a program that computes this minimum.
Input
The first line contains the number of participants ().
The second line contains and the third line contains , separated by spaces. ()
Output
Print the minimum total difference on one line as a reduced fraction.
The minimum is always a rational number. Write it as , where is a nonnegative integer, is a positive integer, and . If , print only p; otherwise print it in the form p/q. A decimal or an approximate value is not accepted.