Let's fly!
InterviewTime limit2sMemory limit1024 MB
Given altitudes h_i and weights a_i, find the single altitude minimizing the sum of |h_i - x| * a_i and print that altitude with the minimum cost.
- Level
Medium4 of 10
- Topics
- Sorting, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
Malysh and Karlson decided to go out for a walk. More precisely, a flight. Karlson, being a responsible person, drew up a flight plan and showed it to Malysh. The plan works like this: for every moment in time , Karlson assigned an altitude at which he will be flying at that moment.
But after yet another round of eating too much jam, Malysh did not like the altitude changes in Karlson's plan. He decided to adjust the plan slightly so that the flight stays at one altitude the whole time. Karlson, in turn, said he would be very upset if Malysh changed the plan a lot. Precisely: if Malysh changes the altitude at moment by one meter, Karlson's displeasure grows by , where is how much Karlson wants to fly at altitude at moment .
So Malysh can change the altitude at any point by any amount, but replacing with at point increases Karlson's displeasure by . Help Malysh fix the plan by replacing all altitudes with one single value, so that Karlson's displeasure is minimal.
Input
The first line contains one integer (), the duration of the flight. The second line contains integers (), the altitude at moment . The third line contains integers (), Karlson's displeasure coefficient at moment .
Output
Print two numbers: the final altitude of the flight and the total displeasure of Karlson. If several altitudes are possible, print the smallest one.
Hint
If there are several answers, print the lexicographically smallest pair.