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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>

using namespace std;

int n, k, q, pos;
vector<long long> V, pref, dp, res, pref1;
vector<pair<int, int> >Q;
set<int> b, e;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k>>q;
    V.assign(n+1, 0);
    pref.assign(n+1, 0);
    pref1.assign(n+1, 0);
    for(int i=1;i<n+1;i++)
    {
        cin>>V[i];
        pref[i]=pref[i-1]+V[i];
    }
    for(int i=n-1;i>=0;i--)pref1[n-i]=pref1[n-i-1]+V[i+1];
    for(int i=0;i<q;i++)
    {
        int x, y;
        cin>>x>>y;
        b.insert(x);
        e.insert(y);
        Q.push_back(make_pair(x, y));
        res.push_back(0);
    }
    /*for(int i=0;i<n+1;i++)
    {
        cout<<pref1[i]<<' ';
    }*/
    if(b.size()<=e.size())
    {
    //cout<<1<<endl;
        for(set<int>::iterator it=b.begin();it!=b.end();++it)
        {
            dp.clear();
            dp.assign(n+1, 0);
            for(int i=(*it)+k-1;i<=n;i++)
            {
                dp[i]=max(dp[i-1], dp[i-k]+pref[i]-pref[i-k]);
            }
            for(int i=0;i<q;i++)
            {
                if(Q[i].first==*it)
                {
                    res[i]=dp[Q[i].second];
                }
            }
        }
        for(int i=0;i<q;i++)
        {
            cout<<res[i]<<endl;
        }
        //cout<<q;
    }
    if(b.size()>e.size())
    {
    //cout<<1<<endl;
        for(set<int>::iterator it=e.begin();it!=e.end();++it)
        {
            dp.clear();
            dp.assign(n+1, 0);
            int val=n-(*it)+1;
            for(int i=val+k-1;i<=n;i++)
            {
                dp[i]=max(dp[i-1], dp[i-k]+pref1[i]-pref1[i-k]);
            }
            for(int i=0;i<q;i++)
            {
                if(Q[i].second==*it)
                {
                    res[i]=dp[n-Q[i].first+1];
                }
            }
        }
        for(int i=0;i<q;i++)
        {
            cout<<res[i]<<endl;
        }
        //cout<<"DUPA";
    }
    //cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    return 0;
}