Desant 2
Time limit42sMemory limit1024 MB
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 soldiers at a briefing. They lined up in a row and are numbered from to from left to right. The general wants to choose some number of squads to send to Bitocja. Each squad must consist of exactly 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 . 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 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 . For each scenario, compute the maximum possible sum of for the soldiers sent.
Input
The first line contains three integers , , and (, ), the number of soldiers, the number of soldiers in each squad, and the number of scenarios.
The second line contains integers ().
Each of the next lines contains two integers and (). In the -th scenario, only soldiers whose positions lie in may be sent.
Output
Print lines. The -th line contains the maximum possible sum of for the soldiers sent to Bitocja in the -th scenario.
Hint
In the first and fifth scenarios, the optimal choice is two squads, covering positions and . In the second and sixth scenarios, the optimal choice is a single squad covering positions . 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 and .