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
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
typedef pair<int, int> pii;
typedef pair<lld, lld> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

constexpr int N = 1 << 19;

lld t[N];
lld p[N];
lld dp[N];
int mx[N];
vector<pii> q[N];
lld ans[N];

int n, k, T, l ,r;

int main() {
	scanf("%d%d%d", &n, &k, &T);
	
	for (int i = 1; i <= n; ++i) {
		scanf("%lld", t + i);
		p[i] = p[i - 1] + t[i];
	}
	
	for (int i = n; i >= k; --i) {
		p[i] -= p[i - k];
	}
	
	/* printf("p:\n");
	for (int i = 1; i <= n; ++i) {
		printf("%lld ", p[i]);
	}
	puts(""); */
	
	for (int i = 0; i < T; ++i) {
		scanf ("%d%d", &l, &r);
		mx[l] = max(mx[l], r);
		q[l].pb(mp(r, i));
	}
	
	for (int i = 1; i <= n; ++i) {
		if (q[i].empty())
			continue;
		
		//printf("rozpatruję %d do %d\n", i, mx[i]);
		
		for (int j = i - 1; j < i + k - 1; ++j) {
			dp[j] = 0;
		}
		
		for (int j = i + k - 1; j <= mx[i]; ++j) {
			dp[j] = max(dp[j - 1], dp[j - k] + p[j]);
			//printf("dp[%d] = %lld\n", j, dp[j]);
		}
		
		for (auto x : q[i]) {
			ans[x.ss] = dp[x.ff];
		}
	}
	
	for (int i = 0; i < T; ++i) {
		printf("%lld\n", ans[i]);
	}
	
	return 0;
}
/*
8 3 7
3 -1 10 0 10 -1 1 -1
1 8
3 5
6 8
1 2
1 7
2 8
1 6

*/