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
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#define MAXN 300003

using namespace std;

int l, r, n, k, q;
long long a[MAXN];
long long sum_pref[MAXN];
long long dp[MAXN];

struct str {
	int index;
	int r;
};

vector<str> R[MAXN];
int max_r[MAXN];

long long res[MAXN];

int main()
{
  scanf("%d %d %d", &n, &k, &q);
 
  for (int i = 1; i <= n; i++) {
	scanf("%lld", &a[i]);
    sum_pref[i] = sum_pref[i - 1] + a[i];
  }
  
  for (int i = 0; i < q; i++) {
	scanf("%d %d", &l, &r); 
	R[l].push_back(str{i, r});
	max_r[l] = max(max_r[l], r);
  }
  
  for (int l = 1; l <= n; l++) {
	if (max_r[l] != 0) {
		for (int j = l-1; j < (l + k - 1); j++) {
			dp[j] = 0;
		}	
		for (int j = (l + k - 1); j <= max_r[l]; j++) {
			dp[j] = max(dp[j-1], dp[j-k] + sum_pref[j] - sum_pref[j - k]);
		}
		
		for (int i = 0; i < R[l].size(); i++) {
			res[R[l][i].index] = dp[R[l][i].r];
		}
	}
  }
  
  for (int i = 0; i < q; i++) {
	printf("%lld\n", res[i]);
  }
 
}