Logo
(追記) (追記ここまで)

31059번 - Haybale Distribution 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB4501219832.237%

문제

Farmer John is distributing haybales across the farm!

Farmer John's farm has $N$ $(1\le N\le 2\cdot 10^5)$ barns, located at integer points $x_1,\dots, x_N$ $(0 \le x_i \le 10^6)$ on the number line. Farmer John's plan is to first have $N$ shipments of haybales delivered to some integer point $y$ $(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 $a_i$ and $b_i$ $(1\le a_i, b_i\le 10^6),ドル $a_i$ haybales are wasted per unit of distance left each shipment is transported, and $b_i$ haybales are wasted per unit of distance right each shipment is transported. Formally, for a shipment being transported from point $y$ to a barn at point $x,ドル the number of haybales wasted is given by

$$\begin{cases} 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

For example, to answer the second query, it is optimal to select $y=2$. Then the number of wasted haybales is equal to 2ドル(2-1)+2(2-2)+1(3-2)+1(4-2)+1(10-2)=1+0+1+2+8=13$.

힌트

출처

Olympiad > USA Computing Olympiad > 2023-2024 Season > USACO 2023 December Contest > Gold 3번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /