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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

struct pyt
{
    int k, nr;
};

bool por1(pyt a, pyt b)
{
    if (a.k < b.k)
        return true;
    if (a.k > b.k)
        return false;
    return a.nr < b.nr;
}

bool por2(pyt a, pyt b)
{
    if (a.k > b.k)
        return true;
    if (a.k < b.k)
        return false;
    return a.nr < b.nr;
}

int main()
{
    ios_base::sync_with_stdio(0);
    int n, k, q;
    cin >> n >> k >> q;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
        if (i != 0)
            a[i] += a[i - 1];
    }
    vector<int> maxKon(n, -1), minPocz(n, n);
    vector<int> l(q), r(q);
    for (int i = 0; i < q; ++i)
    {
        cin >> l[i] >> r[i];
        --l[i];
        --r[i];
        maxKon[l[i]] = max(maxKon[l[i]], r[i]);
        minPocz[r[i]] = min(minPocz[r[i]], l[i]);
    }
    ll s1 = 0, s2 = 0;
    for (int i = 0; i < n; ++i)
    {
        if (maxKon[i] != -1)
            s1 += maxKon[i] - i;
        if (minPocz[i] != n)
            s2 += i - minPocz[i];
    }
    vector< vector<pyt> > P(n);
    vector<ll> dp(n, 0);
    vector<ll> odp(q);
    if (s1 <= s2)
    {
        for (int i = 0; i < q; ++i)
            P[l[i]].push_back({r[i], i});
        for (int i = 0; i < n; ++i)
            sort(P[i].begin(), P[i].end(), por1);
        for (int i = 0; i < n; ++i)
        {
            if (P[i].size() == 0)
                continue;
            for (int j = i; j <= P[i].back().k; ++j)
            {
                if (j - i + 1 >= k)
                {
                    ll sum = a[j];
                    if (j - k >= 0)
                        sum -= a[j - k];
                    dp[j] = sum;
                    if (j - k >= i)
                        dp[j] += dp[j - k];
                    dp[j] = max(dp[j], (ll) 0);
                    if (j != i)
                        dp[j] = max(dp[j], dp[j - 1]);
                }
            }
            for (int j = 0; j < P[i].size(); ++j)
                odp[P[i][j].nr] = dp[P[i][j].k];
            for (int j = i; j <= P[i].back().k; ++j)
                dp[j] = 0;
        }
    }
    else
    {
        for (int i = 0; i < q; ++i)
            P[r[i]].push_back({l[i], i});
        for (int i = 0; i < n; ++i)
            sort(P[i].begin(), P[i].end(), por2);
        for (int i = 0; i < n; ++i)
        {
            if (P[i].size() == 0)
                continue;
            for (int j = i; j >= P[i].back().k; --j)
            {
                if (i - j + 1 >= k)
                {
                    ll sum = a[j + k - 1];
                    if (j - 1 >= 0)
                        sum -= a[j - 1];
                    dp[j] = sum;
                    if (j + k <= i)
                        dp[j] += dp[j + k];
                    dp[j] = max(dp[j], (ll) 0);
                    if (j != i)
                        dp[j] = max(dp[j], dp[j + 1]);
                }
            }
            for (int j = 0; j < P[i].size(); ++j)
                odp[P[i][j].nr] = dp[P[i][j].k];
            for (int j = i; j >= P[i].back().k; --j)
                dp[j] = 0;
        }
    }
    for (int i = 0; i < q; ++i)
        cout << odp[i] << endl;
    return 0;
}