#644
Haybale Distribution
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%
문제
Farmer John is distributing haybales across the farm!
Farmer John's farm has barns, located at integer points on the number line. Farmer John's plan is to first have shipments of haybales delivered to some integer point and then distribute one shipment to each barn.
Unfortunately, Farmer John's distribution service is very wasteful. In particular, for some and , haybales are wasted per unit of distance left each shipment is transported, and haybales are wasted per unit of distance right each shipment is transported. Formally, for a shipment being transported from point to a barn at point , the number of haybales wasted is given by
a_i\cdot (y-x) & \text{if } y \ge x \\ b_i\cdot (x-y) & \text{if } x > y \end{cases}.$$ Given $Q$ $(1\le Q\le 2\cdot 10^5)$ independent queries each consisting of possible values of $(a_i,b_i)$, please help Farmer John determine the fewest amount of haybales that will be wasted if he chooses $y$ optimally. ## 입력 The first line contains $N$. The next line contains $x_1\dots x_N$. The next line contains $Q$. The next $Q$ lines each contain two integers $a_i$ and $b_i$. ## 출력 Output $Q$ lines, the $i$th line containing the answer for the $i$th query. ## 예제 입력 1 ``` 5 1 4 2 3 10 4 1 1 2 1 1 2 1 4 ``` ## 예제 출력 1 ``` 11 13 18 30 ``` ## 점수 Input 2: $N,Q\le 10$Input 3: $N,Q\le 500$Inputs 4-6: $N,Q\le 5000$Inputs 7-16: No additional constraints.코드 제출
코드를 제출하려면 로그인이 필요합니다.
로그인내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.