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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
#include <cassert>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;

struct Query{
    int l,r,id;
    ll result;
};

void just_brute_force(int n, int k, const vector<ll>&Seq, vector<Query>&Q) {
    for (Query &q : Q) {
        if (q.r < q.l) {
            q.result = 0;
            continue;
        }
        vector<ll>dp(q.r-q.l+1);
        auto get_dp  = [&](int i) {
            i -= q.l;
            return i>=0 ? dp[i] : 0;
        };
        for (int i=q.l; i<=q.r; i++)
            dp[i-q.l] = max(get_dp(i-1), Seq[i]+get_dp(i-k));
        q.result = get_dp(q.r);
    }
}

void n_squared(int n, int k, const vector<ll>&Seq, vector<Query>&Q) {
    sort(Q.begin(), Q.end(), [&](const Query &q1, const Query &q2){
        if (q1.l != q2.l)
            return q1.l < q2.l;
        return q1.r < q2.r;
    });

    int qit = 0;
    vector<ll>dp(n);
    for (int a=0; a<n; a++) {
        if (qit == Q.size() || Q[qit].l > a)
            continue;
        while (qit < Q.size() && Q[qit].r < Q[qit].l)
            Q[qit++].result = 0;

        if (qit == Q.size() || Q[qit].l > a)
            continue;

        auto get_dp  = [&](int i) {return i>=a ? dp[i] : 0;};
        for (int b=0; ; ) {
            dp[b] = max(get_dp(b-1), Seq[b]+get_dp(b-k));
            if (Q[qit].r == b) {
                Q[qit++].result = dp[b];
                if (qit == Q.size() || Q[qit].l > a)
                    break;
            }
            else
                b++;
        }
    }

    sort(Q.begin(), Q.end(), [&](const Query &q1, const Query &q2){return q1.id<q2.id;});
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n,k,q;
    cin >> n >> k >> q;

    vector<ll>ISeq(n);
    for (ll &x : ISeq)
        cin >> x;
    vector<ll>Seq;
    ll sum = 0;
    for (int i=0;i<k-1;i++)
        sum += ISeq[i];
    for(int i=k-1;i<n;i++) {
        sum += ISeq[i];
        Seq.push_back(sum);
        sum -= ISeq[i-k+1];
    }

    vector<Query>Q(q);
    for(int i=0;i<q;i++) {
        Query &qq = Q[i];
        cin >> qq.l >> qq.r;
        qq.l--;
        qq.r--;
        qq.r -= k-1;
        qq.id = i;
    }

    // if (ll(n)*ll(n) < 2000000000ll)
        n_squared(Seq.size(), k, Seq, Q);
    // else
    //     just_brute_force(Seq.size(), k, Seq, Q);

    for (Query &qq : Q)
        cout << qq.result << "\n";

    return 0;
}