Minimum Cost Paths

아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

Farmer John's pasture can be regarded as an N×MN\times M (2N1092\le N\le 10^9, 2M21052\le M\le 2\cdot 10^5) 2D grid of square "cells" (picture a huge chessboard). The cell at the xx-th row from the top and yy-th column from the right is denoted by (x,y)(x,y) for each x\[1,N],y\[1,M]x\in \[1,N], y\in \[1,M]. Furthermore, for each y\[1,M]y\in \[1,M], the yy-th column is associated with the cost c_yc\_y (1c_y1091\le c\_y\le 10^9).

Bessie starts at the cell (1,1)(1,1). If she is currently located at the cell (x,y)(x,y), then she may perform one of the following actions:

  • If y\<My\<M, Bessie may move to the next column (increasing yy by one) for a cost of x2x^2.
  • If x\<Nx\<N, Bessie may move to the next row (increasing xx by one) for a cost of c_yc\_y.

Given QQ (1Q21051\le Q\le 2\cdot 10^5) independent queries each of the form (x_i,y_i)(x\_i,y\_i) (x_i\[1,N],y_i\[1,M]x\_i\in \[1,N], y\_i\in \[1,M]), compute the minimum possible total cost for Bessie to move from (1,1)(1,1) to (x_i,y_i)(x\_i,y\_i).

입력

The first line contains NN and MM.

The second line contains MM space-separated integers c_1,c_2,,c_Mc\_1,c\_2,\ldots,c\_M.

The third line contains QQ.

The last QQ lines each contain two space-separated integers x_ix\_i and y_iy\_i.

출력

QQ lines, containing the answers for each query.

Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

힌트

The output in grid format:

    1  2  3  4
  *--*--*--*--*
1 | 0| 1| 2| 3|
  *--*--*--*--*
2 | 1| 5| 9|13|
  *--*--*--*--*
3 | 2|11|20|29|
  *--*--*--*--*
4 | 3|19|35|49|
  *--*--*--*--*
5 | 4|29|54|69|
  *--*--*--*--*