#P9982. [USACO23DEC] Haybale Distribution G
[USACO23DEC] Haybale Distribution G
Problem Description
Farmer John is distributing haybales on his farm.
There are barns on Farmer John’s farm, located at integer points on a number line . Farmer John plans to transport truckloads of haybales to an integer point , and then deliver one truckload to each barn.
Unfortunately, Farmer John’s delivery system wastes many haybales. Specifically, given some , for each truckload, moving one unit distance to the left wastes haybales, and moving one unit distance to the right wastes haybales. Formally, for one truckload moving from integer point to a barn at position , the number of wasted haybales is:
$$\begin{cases}a_i\cdot (y-x) & \text{if} \ y>x \\ b_i\cdot(x-y)&\text{if}\ x>y\end{cases}$$You are given independent queries . Each query provides a pair . Help Farmer John compute, when choosing the best , the minimum total number of haybales wasted.
Input Format
The first line contains .
The next line contains .
The next line contains .
The next lines each contain two integers .
Output Format
Output lines. The -th line contains the answer to the -th query.
5
1 4 2 3 10
4
1 1
2 1
1 2
1 4
11
13
18
30
Hint
Sample Explanation 1
In the second query of the sample, the best choice is . The number of wasted haybales is $2(2-1) + 2(2-2) + 1(3-2) + 1(4-2) + 1(10-2) = 2 + 0 + 1 + 2 + 8 = 13$.
Test Point Properties
- Test point satisfies .
- Test point satisfies .
- Test points satisfy .
- Test points have no additional constraints.
Translated by ChatGPT 5