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

#PS0000293. Đi bộ trên vòng tròn nhiều vòng (Multiple Laps on a Weighted Cycle)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đi bộ trên vòng tròn nhiều vòng (Multiple Laps on a Weighted Cycle)

Multiple Laps on a Weighted Cycle

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

A directed cycle has nn vertices numbered 1 through nn, and edge ii leads from vertex ii to the next vertex with weight aia_i (wrapping from nn to 1). For query (s,k)(s,k), sum the weights of exactly kk successive edges starting at vertex ss. Repeated traversals count separately; zero edges give zero.

Input

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

Output

Print each answer on its own line.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤9|a_i|\le9, 1≤s≤n, 0≤k≤10181\le s\le n,\ 0\le k\le10^{18}. Additionally: n≤30,q≤40n\le30, q\le40, k≤100k\le100.
  • Subtask 2 (30 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤9|a_i|\le9, 1≤s≤n, 0≤k≤10181\le s\le n,\ 0\le k\le10^{18}. Additionally: all queries satisfy k mod n=0k\bmod n=0.
  • Subtask 3 (50 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤9|a_i|\le9, 1≤s≤n, 0≤k≤10181\le s\le n,\ 0\le k\le10^{18}.

Example 1

Input

3 3
2 -3 4
3 5
2 0
1 3

Output

9
0
3

Explanation

Five edges from 3 give 4+2-3+4+2=9. Zero edges give 0; one lap gives 3.

Example 2

Input

1 1
-9
1 10

Output

-90

Explanation

The sole edge is traversed 10 times.