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
#include <bits/stdc++.h>

using namespace std;

using LL = long long;

const int MAX = 3e5+7;
const int MAXK = 300;
const int H = 1 << 19;
const LL INF = 1e18+7;
int n, k, q;

LL DP[MAX][MAXK];
LL arr[MAX];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> k >> q;

    for (int i = 0; i < n; ++i)
    {
        cin >> arr[i];
    }

    for (int h = 0; h < q; ++h)
    {
        int a, b;
        cin >> a >> b;
        --a;
        --b;

        for (int i = 0; i <= k; ++i)
            DP[a][i] = -INF;
        
        DP[a][0] = 0;

        if (k != 1)
            DP[a][1] = arr[a];
        else
            DP[a][0] = arr[a];

        for (int i = a + 1; i <= b; ++i)
        {
            for (int z = 0; z < min(MAXK, k); ++z)
            {
                int zz = z + 1;

                if (zz >= k)
                    zz -= k;

                DP[i][zz] = DP[i - 1][z] + arr[i];
            }

            DP[i][0] = max(DP[i][0], DP[i - 1][0]);
        }

        cout << DP[b][0] << "\n";
    }

    return 0;
}