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

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, k, q;
    cin >> n >> k >> q;
    vector<int> v(n);
    vector<long long> p(n), dp(n);
    auto solve = [&](int L, int R) -> long long {
        auto sum = [&](int l, int r) {
            return p[r] - (l == L ? l == 0 ? 0 : p[l - 1] : p[l - 1]);
        };
        long long mx = 0, ans = 0;
        for (int r = L + k - 1; r < R; r++) {
            dp[r] = sum(r - k + 1, r) + mx;
            if (r - k + 1 >= L + k - 1) mx = max(mx, dp[r - k + 1]);
            ans = max(ans, dp[r]);
        }
        return ans;
    };
    for (int i = 0; i < n; i++) {
        cin >> v[i];
        p[i] = v[i];
        if (i > 0) p[i] += p[i - 1];
    }
    while (q--) {
        int l, r;
        cin >> l >> r;
        l--;
        cout << solve(l, r) << '\n';
    }
}