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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define PB push_back
#define EB emplace_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vpii vector<pii>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;
using ld = long double;

template <class T>
istream& operator>> (istream& is, vector<T>& vec){
    FOR(i,0,vec.size()) is >> vec[i];
    return is;
}
template <class T>
ostream& operator<< (ostream& os, vector<T>& vec){
    for(auto& t : vec) os << t << " ";
    return os;
}

struct query {
    int l, r, idx;
    ll ans;
};

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, k, q;
    cin >> n >> k >> q;
    vi A(n);
    cin >> A;
    vll P(n + 1);
    P[0] = 0;
    FOR(i,0,n) P[i + 1] = P[i] + A[i];
    vector<query> Q(q);
    FOR(i,0,q){
        int a, b;
        cin >> a >> b;
        Q[i].l = a - 1;
        Q[i].r = b;
        Q[i].idx = i;
        Q[i].ans = 0;
    }

    sort(ALL(Q), [](const query& a, const query& b){
        return a.l < b.l || (a.l == b.l && a.r < b.r);
    });
    vll dp(n, 0);
    int prevl = 0, prevr = 0;
    FOR(i,0,q){
        if (prevl != Q[i].l){
            FOR(j,prevl, Q[i].l) dp[j] = 0;
            prevl = prevr = Q[i].l;
        }
        FOR(j,prevr, Q[i].r){
            if (j > 0) dp[j] = dp[j - 1];
            else dp[j] = 0;
            if (j >= k + prevl - 1){
                //cout << "Proba z j = " << j << endl;
                dp[j] = max(dp[j], (j >= k ? dp[j - k] : 0) + P[j + 1] - P[j - k + 1]);
            }
        }
        //cout << Q[i].l << " " << Q[i].r << " -> " << dp << endl;
        Q[i].ans = dp[Q[i].r - 1];
    }

    sort(ALL(Q), [](const query& a, const query& b){
        return a.idx < b.idx;
    });
    FOR(i,0,q) cout << Q[i].ans << endl;
}