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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#pragma GCC target("sse,sse2,sse3,popcnt,abm,mmx,tune=native") // for szkopul and sio only
typedef long long lld;
typedef double lf;
typedef long double llf;
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define For(i,s,a) for(lld i = (lld)s; i < (lld)a; ++i)
#define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it)
#define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it)
#define pb push_back
#define eb emplace_back
#define ff first
#define dd second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define make_unique(x) (x).erase( unique(all(x)), (x).end())
#define popcnt(x) __builtin_popcount(x)
#define sz size()
#define time_since duration_cast< milliseconds >(system_clock::now().time_since_epoch())
 
template<typename Ta, typename Tb>
ostream & operator <<(ostream & os, pair<Ta, Tb> x){
	return os << x.ff << " " << x.dd;
}

const int N = 3e5 + 2;
lld c[N], sum[N], rsum[N];
lld dp[N];

#define fmax(a, b) (a) > (b) ? (a) : (b)

bool getl(int g, int h, int k) {
	if(h - g + 1 < 2 * k)
		return 0;
	memset(dp + g, 0, 8 * (h - g + 1));
	if(g)
		dp[g - 1] = 0;
	lld maxn = 0;
	lld a, b;
	For(i, g + k - 1, h + 1) {
		a = i ? dp[i - 1] : 0;
		b = sum[i] + dp[i - k];
		dp[i] = fmax(a, b);
	}
	
	return 1;
}

bool getr(int g, int h, int k) {
	if(h - g + 1 < 2 * k)
		return 0;
//	cout << g << " XDD " << h << endl;
	memset(dp + g, 0, 8 * (h - g + 1));
	dp[h + 1] = 0;
	lld maxn = 0;
	for(int i = h - k + 1; i >= g; --i) {
		dp[i] = fmax(dp[i + 1], rsum[i] + dp[i + k]);
///		cout << g << " " << h << " " << i << " " << dp[i] << endl;
	}
	
	return 1;
}

pii zaps[N];
vector<int>zapsy[2][N];
bitset<N>bylo;
priority_queue<pii>R[2];
lld ans[N];
lld tre[1048576];
const int H = 524288;

int relax(int d) {
	pii x = R[d].top();
	R[d].pop();
	For(i, 0, zapsy[d][x.dd].sz) {
		if(bylo[zapsy[d][x.dd][i]]) {
			swap(zapsy[d][x.dd][i], zapsy[d][x.dd].back());
			zapsy[d][x.dd].pop_back();
			--i;
		}
	}
	if((int)zapsy[d][x.dd].sz < max(R[d ^ 1].top().ff, R[d].empty() ? 0 : R[d].top().ff)) {
		if(zapsy[d][x.dd].sz)
			R[d].push({zapsy[d][x.dd].sz, x.dd});
		return 2;
	} else
		R[d].push({zapsy[d][x.dd].sz, x.dd});
	return d;
}

lld treget(int g, int h) {
	if(g > h)
		return 0;
	g += H;
	h += H;
	lld ret = fmax(tre[g], tre[h]);
	while((g >> 1) != (h >> 1)) {
		if(!(g & 1))
			ret = fmax(ret, tre[g ^ 1]);
		if(h & 1)
			ret = fmax(ret, tre[h ^ 1]);
		g >>= 1;
		h >>= 1;
	}
	return ret;
}

void solve(void) {
	int n, k, q;
	scanf("%d%d%d", &n, &k, &q);
	/*n = 100000;
	k = 3;
	q = 300000;*/
	For(i, 0, n)
		scanf("%lld", &c[i]);
	For(i, 0, k)	
		sum[k - 1] += c[i];
	For(i, k, n)
		sum[i] = sum[i - 1] - c[i - k] + c[i];
	reverse(c, c + n);
	For(i, 0, k)	
		rsum[k - 1] += c[i];
	For(i, k, n)
		rsum[i] = rsum[i - 1] - c[i - k] + c[i];
	reverse(c, c + n);
	reverse(rsum, rsum + n);
	
	tre[k - 1 + H] = fmax(0, sum[k - 1]);
	For(i, k, n) 
		tre[i + H] = fmax(0, sum[i]);
	
	for(int i = H - 1; i; --i)
		tre[i] = fmax(tre[i << 1], tre[i << 1 | 1]);
	
	For(i, 0, q) {
		int g, h;
		scanf("%d%d", &g, &h);
		--g;
		--h;
		zaps[i] = {g, h};
		zapsy[0][g].pb(i);
		zapsy[1][h].pb(i);
	}
	
	For(i, 0, n) {
		if(zapsy[0][i].sz)
			R[0].push({zapsy[0][i].sz, i});
		if(zapsy[1][i].sz)
			R[1].push({zapsy[1][i].sz, i});
		//sort(all(zapsy[0][i]), [i](int a, int b) {return zaps[a].dd < zaps[b].dd;});
		//sort(all(zapsy[1][i]), [i](int a, int b) {return zaps[a].ff > zaps[b].ff;});
		/*for(auto s : zapsy[1][i])
			cout << i << " " << s << " " << zaps[s] << "   ";
		puts("");*/
	}
	while(R[0].size() + R[1].size() > 0) {
		int typ;
		if(R[0].top().ff > R[1].top().ff)
			typ = relax(0);
		else
			typ = relax(1);
		if(typ == 2)
			continue;
		int x = R[typ].top().dd;
		sort(all(zapsy[typ][x]), [typ](int a, int b) {return typ ? zaps[a].ff > zaps[b].ff : zaps[a].dd < zaps[b].dd;});
	//	cout << typ << " " << R[typ].top() << "   " << (!typ ? zaps[zapsy[typ][x].back()].dd : zaps[zapsy[typ][x].back()].ff) << endl;
		R[typ].pop();
		bool calculated = 0;
		if(typ == 0)
			calculated = getl(x, zaps[zapsy[typ][x].back()].dd, k);
		else
			calculated = getr(zaps[zapsy[typ][x].back()].ff, x, k);
		for(auto s : zapsy[typ][x]) {
	//		cout << typ << " " << s << " " << zaps[s] << "  xd  " << (typ ? zaps[s].ff : zaps[s].dd) << endl;
			ans[s] = calculated ? dp[typ ? zaps[s].ff : zaps[s].dd] : treget(zaps[s].ff + k - 1, zaps[s].dd);
			bylo[s] = 1;
		}
	}
	For(i, 0, q)
		printf("%lld\n", ans[i]);
}


int32_t main(void){
	int t = 1;
	//scanf("%d", &t);
	while(t--)
		solve();
}

/*
20 5 1
-20 0 -14 19 -4 -18 -18 1 -20 -18 -20 -11 9 20 13 -5 -16 15 0 3
13 18
*/