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
206
207
208
209
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define e1 first
#define e2 second
#define pb push_back
#define OUT(x) {cout << x << "\n"; exit(0); }
#define TCOUT(x) {cout << x << "\n"; return; }
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define sz(x) int(x.size())
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

mt19937_64 rng(time(0));
int random(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}
#ifdef DEBUG
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#endif
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif

//Did you REAALLY consider sample tests?

int n, k, q;
const int maxn = 300001;
ll dp[maxn];
ll value[maxn];
ll psum[maxn];
const int inf = 1e9;
int A[maxn], B[maxn];
const ll INF = 1e15;

ll res[maxn];

ll brut_solve(int a, int b) {
	if (a > b) return 0LL;
	int nd = min(a+k-1, b);
	dp[a-1] = 0;
	for (int i=a; i<=nd; ++i) {
		dp[i] = (value[i] >= dp[i-1]) ? value[i] : dp[i-1];
	}
	for (int i=nd+1; i<=b; ++i) {
		dp[i] = (dp[i-k] + value[i] >= dp[i-1]) ? (dp[i-k] + value[i]) : dp[i-1];
	}
	
	return max(0LL, (ll)dp[b]);
}

vector <ll> calc_left(int x, int y, int offset) {
	vector <ll> calc(y - x + 1, 0);
	int iter = -1;
	for (int i = y; i >= x; --i) {
		++iter;
		if (i > offset) continue;
		calc[iter] = value[i];
		if (iter > 0) calc[iter] = max(calc[iter], calc[iter - 1]);
		if (iter >= k) calc[iter] = max(calc[iter], value[i] + calc[iter - k]);
	}

	return calc;
}

vector <ll> calc_right(int x, int y, int offset) {
	vector <ll> calc(y - x + 1, 0);
	int iter = -1;
	for (int i = x; i <= y; ++i) {
		++iter;
		if (i < offset) continue;
		calc[iter] = value[i];
		if (iter > 0) calc[iter] = max(calc[iter], calc[iter - 1]);
		if (iter >= k) calc[iter] = max(calc[iter], value[i] + calc[iter - k]);
	}

	return calc;
}

void solve_through_middle(int x, int mid, int y, const vi&requests) {
	vector <vector<ll> > dpleft(k+1);
	vector <vector<ll> > dpright(k+1);
	
	FOR(i, 0, k) {
		if (mid - i >= x) dpleft[i] = calc_left(x, mid, mid - i);
		else dpleft[i] = vector<ll>(mid - x + 1, 0);
	}
	FOR(i, 0, k) {
		if (mid + i <= y) dpright[i] = calc_right(mid, y, mid + i);
		else dpright[i] = vector<ll>(y - mid + 1, 0);
	}
	
	for (auto&id : requests) {
		int a = A[id], b = B[id];
		ll best = 0;
		
		for (int i = 0; i <= k; ++i) {
			best = max(best, dpleft[i][mid - a] + dpright[k - i][b - mid]);
		}
		
		res[id] = best;
	}
	
	dpleft.clear(); dpleft.shrink_to_fit();
	dpright.clear(); dpright.shrink_to_fit();
}

void solve_divconq(int x, int y, vi requests) {
	if (x > y || requests.empty()) return;
	int mid = (x + y) / 2;
	int ile_lewy = 0, ile_middle = 0, ile_prawy = 0;
	int s = sz(requests);
	rep(i, 0, s) {
		int id = requests[i];
		if (mid > B[id]) ++ile_lewy;
		else if (mid < A[id]) ++ile_prawy;
		else {
			assert(A[id] <= mid && mid <= B[id]);
			ile_middle++;
		}
	}
	
	vi lewy(ile_lewy), middle(ile_middle), prawy(ile_prawy);
	ile_lewy = 0; ile_middle = 0; ile_prawy = 0;
	rep(i, 0, s) {
		int id = requests[i];
		if (mid > B[id]) lewy[ile_lewy++] = id;
		else if (mid < A[id]) prawy[ile_prawy++] = id;
		else {
			assert(A[id] <= mid && mid <= B[id]);
			middle[ile_middle++] = id;
		}
	}
	
	solve_through_middle(x, mid, y, middle);
	solve_divconq(x, mid - 1, lewy);
	solve_divconq(mid + 1, y, prawy);
}


bool TEST = 0;
const int C = 300;
int main() {
	boost;
	if (!TEST) cin >> n >> k >> q;
	else {
		n = 300000; k=C; q=300000;
	}
	
	for (int i=1; i<=n; ++i) {
		if (!TEST) cin >> value[i];
		else value[i] = random(-inf, inf);
		psum[i] = psum[i-1] + value[i];
	}
	
	for (int i=k; i<=n; ++i) {
		value[i-k+1] = psum[i] - psum[i-k];
	}
	
	vi requests;
	
	FOR(i, 1, q) {
		int a, b;
		if (!TEST) cin >> a >> b;
		else {
			a = random(1, 10), b = random(n-10, n);
		}
		A[i] = a; B[i] = b;
		B[i] -= (k - 1);
		if (A[i] > B[i]) res[i] = 0LL;
		else {
			requests.pb(i);
		}
	}
	
	if (k > C) {
		FOR(i, 1, q) res[i] = brut_solve(A[i], B[i]);
	}
	else {
		solve_divconq(1, n - k + 1, requests);
	}
	FOR(i, 1, q) cout << res[i] << "\n";
}