Time Limit: 20 seconds
Memory Limit: 256 MB
You are given an array of
A set of chants is acceptable if:
- No overlap: no two chosen chants share an index.
-
Silence rule: between any two consecutive chosen chants there are at least
$D$ untouched stones. Formally, for two chosen chants$(l_1,r_1)$ and$(l_2,r_2)$ with$r_1 < l_2$ , it must hold that$$l_2 - r_1 - 1 \ge D \quad \text{(equivalently } l_2 > r_1 + D\text{)}.$$ -
Same resonance: all chosen chants have the same sum
$$S = \sum_{i=l}^{r} a[i].$$
Among all acceptable sets, choose the unique one according to the following priorities:
- Maximize the number of chants
$k$ . - Among those, minimize the common sum
$S$ . - Among those, sort the chosen chants by increasing
$r$ (and for equal$r$ , increasing$l$ ). Let this ordered list be$$(r_1,l_1),(r_2,l_2),\dots,(r_k,l_k).$$ Choose the solution with the lexicographically smallest such sequence.
You must output the chosen chants in the required order (increasing
Input Format:-
- The first line contains three integers
$n$ ,$M$ ,$D$ . - The second line contains
$n$ integers$a[1],a[2],\dots,a[n]$ .
Output Format:-
- Print two integers
$k$ and$S$ . - Then print
$k$ lines, each containing two integers$l$ and$r$ describing a chosen chant, in increasing order of$r$ (and for equal$r$ , increasing$l$ ).
Constraints:-
$1 \le n \le 200000$ $1 \le M \le 20$ $0 \le D \le n$ -
$-10^9 \le a[i] \le 10^9$ Examples:- - Input:
5 5 0
1000000000 1000000000 1000000000 1000000000 -1000000000
- Output:
4 1000000000
1 1
2 2
3 3
4 4
- Input:
7 4 0
2 -2 2 -2 2 -2 2
- Output:
4 2
1 1
3 3
5 5
7 7
Note:-
In the first example, since
In the first example, the chosen chants are printed in increasing order of
In the second example, the array alternates