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()
{
    ios_base::sync_with_stdio(false); cin.tie(); cout.tie(0);
    long long n,k,q;
    cin>>n>>k>>q;
    vector<long long> v(n);
    for(long long i=0; i<n; ++i)
        cin>>v[i];
    
    vector<vector<long long>> dp(n, vector<long long>(n,0));
    for(long long i=0; i<k; ++i)
        dp[0][k-1]+=v[i];
    dp[0][k-1]=max(dp[0][k-1],(long long)0);
    for(long long i=1; i<n-k+1; ++i)
        dp[i][i+k-1]=max(dp[i-1][i+k-2]-v[i-1]+v[i+k-1],(long long)0);

    for(long long d=k+1; d<=n; ++d)
        for(long long i=0; i+d-1<n; ++i)
        {
            long long j=i+d-1;
            for(long long p=i; p<j; ++p)
                dp[i][j]=max(dp[i][j],dp[i][p]+dp[p+1][j]);
        }

    for(long long i=0; i<q; ++i)
    {
        long long l,r;
        cin>>l>>r;
        cout<<dp[l-1][r-1]<<endl;
    }
    return 0;
}