PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000280. Tổng đoạn trên mảng vòng (Circular Range Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng đoạn trên mảng vòng (Circular Range Sum)

Circular Range Sum

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

An integer array a1,…,ana_1,\ldots,a_n is arranged in a circle in index order. Index 1 follows index nn. For each query (s,k)(s,k), sum exactly kk consecutive elements starting at ss, including asa_s, moving in increasing index order and wrapping as needed. Each query satisfies 1≤k≤n1\le k\le n.

Input

First line: n,qn,q. Second line: nn integers aia_i. Each of the next qq lines contains two integers s,ks,k.

Output

Print one answer per line, in query order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤q≤401\le q\le40; ∣ai∣≤1000000|a_i|\le1000000; 1≤s,k≤n1\le s,k\le n.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le200000; 1≤q≤2000001\le q\le200000; ∣ai∣≤1000000|a_i|\le1000000; 1≤s,k≤n1\le s,k\le n; k=nk=n for every query.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le200000; 1≤q≤2000001\le q\le200000; ∣ai∣≤1000000|a_i|\le1000000; 1≤s,k≤n1\le s,k\le n.

Example 1

Input

4 3
2 -3 4 1
3 3
4 2
2 4

Output

7
3
4

Explanation

The selected sequences are (4,1,2), (1,2), (-3,4,1,2); their sums are 7, 3, 4.

Example 2

Input

1 1
-7
1 1

Output

-7

Explanation

The circle has one element.