Under construction

Still being built. Things may change

Sequence and Queries 33

Time limit5sMemory limit512 MB

Summary
Given an array and range queries (l, r, k), find the maximum total sum of k non-overlapping non-empty subarrays inside A[l..r].
Level

Hard8 of 10

Topics
Dynamic programming, Segment tree, Divide and conquer
Solved
No attempts yet

Problem

Given a sequence A1,A2,…,ANA_1, A_2, \ldots, A_N of length NN, write a program that processes the following query.

  • l r k: For the subarray Al,Al+1,…,ArA_l, A_{l+1}, \ldots, A_r, choose kk subarrays from it, and print the maximum possible sum of the elements of the chosen subarrays. Each chosen subarray must be non-empty, and the chosen subarrays must not overlap.

Input

The first line contains the size of the sequence NN and the number of queries MM (1≤N,M≤350001 \le N, M \le 35000).

The second line contains A1,A2,…,ANA_1, A_2, \ldots, A_N (−35000≤Ai<35000-35000 \le A_i < 35000).

Each of the next MM lines contains one query (1≤l≤r≤N1 \le l \le r \le N, 1≤k≤r−l+11 \le k \le r - l + 1).

Output

For each query, print the result on its own line.

Examples2

  1. Example 1

    Input
    5 5
    -1 2 -3 4 -5
    1 5 1
    1 5 2
    1 5 3
    1 5 4
    1 5 5
    
    Expected output
    4
    6
    5
    2
    -3
    
  2. Example 2

    Input
    5 1
    7 7 7 7 7
    1 5 1
    
    Expected output
    35