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
#include <iostream>
#include <algorithm>

using namespace std;

int n, k, q;
int a[320000];
long long w[320000];

long long licz(int mi, int ma)
{
    if (ma-mi+1<k)
        return 0;
    long long su=0;
    for (int i=mi; i<mi+k; ++i)
    {
        su+=a[i];
        w[i]=0;
    }
    if (0<su)
        w[mi+k-1]=su;
    for (int i=mi+k; i<=ma; ++i)
    {
        su+=a[i]-a[i-k];
        w[i]=max(w[i-1], su+w[i-k]);
    }

    return w[ma];
}


int main()
{
    ios_base::sync_with_stdio(false);

    cin>>n>>k>>q;
    for (int i=0; i<n; ++i)
        cin>>a[i];
    for (int i=0; i<q; ++i)
    {
        int l, r;
        cin>>l>>r;
        cout<<licz(l-1, r-1)<<'\n';
    }
    return 0;
}