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
//Ada Kołodziejczak
//Zadanie Desant 2 (brut)
#include<bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
using namespace std;
const int N=300005;//300005
long long n, k, q, l, r;
long long maxr[N], wyn[N];
long long t[N], pr[N], dp[N];
vector<pair<pair<long long,long long>,long long> > v;

int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n>>k>>q;
    for(int i=1; i<=n; ++i)
    {
        cin>>t[i];
        pr[i]=t[i]+pr[i-1];
    }

    for(int i=0; i<q; ++i)
    {
        cin>>l>>r;
        v.pb({{l,r},i});
        maxr[l]=max(maxr[l], r);
    }
    
    v.pb({{0,0},0});
    sort(v.begin(), v.end());

    for(int i=1; i<v.size(); ++i)
    {
        if(v[i-1].ff.ff!=v[i].ff.ff)
        {
            dp[v[i].ff.ff-1]=0;
            for(int j=v[i].ff.ff; j<=maxr[v[i].ff.ff]; ++j)
            {
                if(j>=v[i].ff.ff+k-1)
                {
                    dp[j]=max(dp[j-1], dp[j-k]+pr[j]-pr[j-k]);
                }
                else dp[j]=0;
            }   
        }
        wyn[v[i].ss]=dp[v[i].ff.ss];
    }

    for(int i=0; i<q; ++i)
    {
        cout<<wyn[i]<<"\n";
    }
}