#644
Unrated
Haybale Distribution
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Farmer John is distributing haybales across the farm!

Farmer John's farm has NN (1N2105)(1\le N\le 2\cdot 10^5) barns, located at integer points x1,,xNx_1,\dots, x_N (0xi106)(0 \le x_i \le 10^6) on the number line. Farmer John's plan is to first have NN shipments of haybales delivered to some integer point yy (0y106)(0 \le y \le 10^6) and then distribute one shipment to each barn.

Unfortunately, Farmer John's distribution service is very wasteful. In particular, for some aia_i and bib_i (1ai,bi106)(1\le a_i, b_i\le 10^6), aia_i haybales are wasted per unit of distance left each shipment is transported, and bib_i haybales are wasted per unit of distance right each shipment is transported. Formally, for a shipment being transported from point yy to a barn at point xx, 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.
코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.