BASICSUM - Bas1c and the game of sum

no tags 

Bas1c is attending the collab social meeting of the Computer Society and the Gaming Society. One of the weird game that people play there is "The Sum". The rule is as follow:

Given an integer n and an array A consists of n non-negative integers A1, A2,…,An.

Each number in A will be projected onto the screen in the following order: The first number is A1, the second number is A2,…, the n-th number is An, the (n+1)-th number is A1 and so on.

There are Q questions, each given 2 numbers K and B, and the player needs to calculate the sum of K consecutive integers appearing on the screen starting from the B-th integer.

Bas1c really want to win the prize to impress his friends. Help him answer all the questions!

Input

- First line: An integer n (1 <= n <= 106) and an integer Q (1 <= Q <= 106)

- Second line: An array consisting of N non-negative integers A1, A2,…, An (0<Ai<109 for every 1 ≤ i ≤ n) separated by a space

- Q following lines: Each containing two numbers K and B (1 ≤ B ≤ 106; 1 ≤ K ≤ 106)

Output

Q lines containing the answer of respective question

Example

Input:

5 2

4 2 6 1 7

3 2

3 9

Output:

9

12

 

Explanation:

First question: K=3, B=2 then the sum is 2+6+1=9

Second question: K=3, B=9 then the sum is 1+7+4=12

Subtasks:

25% with B+K ≤ N and N,Q ≤ 103

25% with B+K > N, K ≤ 103 and Q ≤ 103


hide comments
nadstratosfer: 2022-10-29 01:57:50

You know there are many like this one, here we just have a twist that is easy to handle.

Vipul Srivastava: 2022-04-17 11:02:24

I thought this was okay to be in Classical, not sure if a same problem exists. I think this did require some thinking and logic to solve.


Added by:LTU ComSoc
Date:2022-03-31
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All