1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <algorithm>

const int maxN = 300 * 1000 + 20;

int n;
int k;
long long a[maxN];
long long sum[maxN];
long long solution[maxN];
int l, r;

int q;

int main(){

        std::cin >> n >> k >> q;
        for(int i = 0; i < n; i++)
                std::cin >> a[i];

        sum[0] = 0;
        for(int i = 1; i <= n; i++)
                sum[i] = sum[i-1] + a[i-1];

        for(int i = 0; i <= n - k; i++)
                sum[i] = sum[i+k] - sum[i];
        //for(int i = 0; i < n; i++)
        //      std::cout << sum[i] << " ";
        //std::cout << "\n";


        for(int _i = 0; _i < q; _i++){

                std::cin >> l >> r;
                l--;
                r--;

                for(int i = r; i > r+1-k; i--)
                        solution[i] = 0;
                solution[r+1-k] = std::max((long long) 0, sum[r+1-k]);
                for(int i = r-k; i >= l; i--){
                        solution[i] = std::max(
                                solution[i+1],
                                sum[i] + solution[i+k]
                        );
                }

                //for(int i = l; i <= r; i++)
                //      std::cout << solution[i] << " ";
                //std::cout << "\n";

                std::cout << solution[l] << "\n";
        }

        return 0;
}