Under construction

Still being built. Things may change

Desant 2

Time limit42sMemory limit1024 MB

Summary
Given an array and k, answer range queries by finding the maximum sum of non-overlapping length-k blocks chosen inside each query range.
Level

Hard9 of 10

Topics
Dynamic programming, Divide and conquer, Prefix sum
Solved
No attempts yet

Statement

Bajtocja plans to attack Bitocja again. A landing behind enemy lines is a job for real tough guys, so soldiers of Bajtogrom, the best special unit of Bajtocja, will take part.

General Bajtczak has gathered nn soldiers at a briefing. They lined up in a row and are numbered from 11 to nn from left to right. The general wants to choose some number of squads to send to Bitocja. Each squad must consist of exactly kk soldiers that occupy a contiguous range of positions. Each soldier belongs to at most one squad. The general has no preference for the number of squads, and he may choose none and call off the attack for now.

Each soldier is described by an integer aia_i. The higher the value, the more effective the soldier is in combat. The value can also be negative, meaning that soldier will only get in the way.

The general wants to maximize the sum of aia_i over all soldiers sent on the landing. However, some of the first soldiers in the row must be sent to the front with Intocja, and some of the last soldiers must go on intelligence work in Longlongtocja. Therefore, squads may only be chosen from soldiers whose positions lie in the range [li,ri][l_i, r_i]. For each scenario, compute the maximum possible sum of aia_i for the soldiers sent.

Input

The first line contains three integers nn, kk, and qq (1≤n,q≤3⋅1051 \le n, q \le 3 \cdot 10^5, 1≤k≤n1 \le k \le n), the number of soldiers, the number of soldiers in each squad, and the number of scenarios.

The second line contains nn integers a1,…,ana_1, \ldots, a_n (−109≤ai≤109-10^9 \le a_i \le 10^9).

Each of the next qq lines contains two integers lil_i and rir_i (1≤li≤ri≤n1 \le l_i \le r_i \le n). In the ii-th scenario, only soldiers whose positions lie in [li,ri][l_i, r_i] may be sent.

Output

Print qq lines. The ii-th line contains the maximum possible sum of aia_i for the soldiers sent to Bitocja in the ii-th scenario.

Hint

In the first and fifth scenarios, the optimal choice is two squads, covering positions [1,2,3][1, 2, 3] and [5,6,7][5, 6, 7]. In the second and sixth scenarios, the optimal choice is a single squad covering positions [3,4,5][3, 4, 5]. In the third and fourth scenarios, the general should not form any squad and instead think the plan over calmly. In the seventh scenario, the general should form two squads, covering positions [1,2,3][1, 2, 3] and [4,5,6][4, 5, 6].

Examples1

  1. Example 1

    Input
    8 3 7
    3 -1 10 0 10 -1 1 -1
    1 8
    3 5
    6 8
    1 2
    1 7
    2 8
    1 6
    
    Expected output
    22
    20
    0
    0
    22
    20
    21