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
#include <iostream>
#include <vector>
#include <algorithm>
typedef long long ll;
using namespace std;

ll sum(int l, int r, vector<ll> &pref)
{
    return pref[r] - pref[l-1];
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k, q; cin >> n >> k >> q;
    vector<ll> a(n+1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    vector<pair<pair<int, int>, int>> query(q);
    vector<ll> ans(q);
    for (int i = 0; i < q; i++) 
    {
        int l, r; cin >> l >> r;
        query[i] = {{l, r}, i};
    } 
    sort(query.begin(), query.end());
    vector<ll> dp(n+1), pref(n+1);
    for (int i = 1; i <= n; i++) pref[i] = pref[i-1] + a[i];
    for (int i = 0; i < q; i++)
    {
        int l = query[i].first.first, r = query[i].first.second;
        dp[l-1] = 0;
        for (int j = l; j <= r; j++)
        {
            dp[j] = dp[j-1];
            if (j-l+1 >= k) dp[j] = max(dp[j], dp[j-k]+sum(j-k+1, j, pref));
        }
        ans[query[i].second] = dp[r];
        while (i+1<q && query[i+1].first.first == l)
        {
            i++;
            int r2 = query[i].first.second;
            for (int j = r+1; j <= r2; j++)
            {
                dp[j] = dp[j-1];
                if (j-l+1 >= k) dp[j] = max(dp[j], dp[j-k]+sum(j-k+1, j, pref));
            }
            r = r2;
            ans[query[i].second] = dp[r];
        }
    }
    for (ll x: ans) cout << x << '\n';
}