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
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int n, k, q;
	cin >> n >> k >> q;
	vector<LL> a(n);
	REP(i,n) cin >> a[i];
	vector<pair<int,int>> z(q); // {l, r};
	vector<LL> ans(q);
	REP(i,q) {
		int l, r;
		cin >> l >> r;
		--l;
		--r;
		z[i] = {l, r};
	}
	debug(z);

	vector<LL> dp(n);
	auto calc_from_left = [&](int left, int right) {
		if (right - left + 1 < k) {
			FOR(i,left,right) {
				dp[i] = 0;
			}
			return;
		}
		LL sum = 0;
		REP(i,k) {
			sum += a[left + i];
			dp[left + i] = 0;
		}
		dp[left + k - 1] = max(sum, 0ll);
		FOR(i,left+k,right) {
			sum += a[i];
			sum -= a[i - k];
			dp[i] = max(dp[i - 1], dp[i - k] + sum);
		}	
	};

	auto calc_from_right = [&](int left, int right) {
		if (right - left + 1 < k) {
			FOR(i,left,right) {
				dp[i] = 0;
			}
			return;
		}
		LL sum = 0;
		REP(i,k) {
			sum += a[right - i];
			dp[right - i] = 0;
		}
		dp[right - k + 1] = max(sum, 0ll);
		for (int i = right - k; i >= left; --i) {
			sum += a[i];
			sum -= a[i + k];
			dp[i] = max(dp[i + 1], dp[i + k] + sum);
		}
	};

	auto solo = [&](int id) {
		/*if (z[id].second - z[id].first + 1 < k) {
			return;
		}*/
		calc_from_left(z[id].first, z[id].second);
		ans[id] = dp[z[id].second];
	};

	function<void(int,int,vector<int>)> rec = [&](int left, int right, const vector<int>& ids) {
		debug(left, right, ids);
		if (right - left + 1 < k) return;
		if (ids.empty()) return;
		const int mid = (left + right) / 2;
		debug(mid);
		vector<int> vnow, vleft, vright;
		for (auto id : ids) {
			if (z[id].first > mid) {
				vright.emplace_back(id);
			}
			else if (z[id].second <= mid) {
				vleft.emplace_back(id);
			}
			else {
				vnow.emplace_back(id);
			}
		}
		debug(vnow, vleft, vright);

		LL k_cost = (LL)k * (LL)(right - left + 1);
		LL solo_cost = 0;
		for (auto id : vnow) {
			solo_cost += z[id].second - z[id].first + 1;
		}
		if (solo_cost <= k_cost) {
			for (auto id : ids) {
				solo(id);
			}
		}
		else {
			REP(i,k) {
				if (mid + 1 - i < 0) {
					break;
				}
				calc_from_right(left, mid - i);
				calc_from_left(mid + 1 - i, right);
				for (auto id : vnow) {
					if (z[id].first > mid - i) {
						if (z[id].first == mid + 1 - i) {
							ans[id] = max(ans[id], dp[z[id].second]);
						}
					}
					else {
						ans[id] = max(ans[id], dp[z[id].first] + dp[z[id].second]);
					}
				}
			}
		}
		rec(left, mid, vleft);
		rec(mid + 1, right, vright);
	};

	vector<int> vnow;
	REP(i,q) {
		if (z[i].first == z[i].second) {
			if (k == 1) {
				ans[i] = max(0ll, a[z[i].first]);
			}
		}
		else if (z[i].second - z[i].first + 1 >= k) {
			vnow.emplace_back(i);
		}
	}
	rec(0, n - 1, vnow);

	REP(i,q) {
		cout << ans[i] << '\n';
	}
}