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
210
211
212
213
214
215
216
217
218
219
220
221
222
#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")

#define st first
#define nd second
#define pb(x) push_back(x)
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)

using namespace std;

///~~~~~~~~~~~~~~~~~~~~~~~~~~

void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);

///~~~~~~~~~~~~~~~~~~~~~~~~~

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;

#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int N=1<<19, INF=1e9+5, mod=1e9+7;
ll ans[N];
int tab[N];
ll pref[N], ile[N];
ll dp[N];
int n, k;
vector<pair<pii, int> > quer[N+N];
void solve(int l, int r, int v){
	int m=(l+r)>>1;
	//deb(l, r);
	if(l<r){
		solve(l, m, v+v);
		solve(m+1, r, v+v+1);
	}
	vector<pair<pii, int> > Q=quer[v];
	//if(Q.empty())return;
	for(int i=max(m-k-1, l); i<=max(m+5, l+k+5) && i<=r; i++){
		//cout<<i<<"\n";
		dp[i-1]=0;
		for(int j=i; j<=r; j++){
			dp[j]=dp[j-1];
			if(j-k>=i-1)dp[j]=max(dp[j], dp[j-k]+ile[j]);
		}
		dp[i]=0;
		for(int j=i-1; j>=l; j--){
			dp[j]=dp[j+1];
			if(j+k<=i)dp[j]=max(dp[j], dp[j+k]+ile[j+k-1]);
		}
		if(k==1){
			dp[i]=max(0, tab[i]);
			dp[i-1]=max(0, tab[i-1]);
		}
		
		for(int j=0; j<Q.size(); j++){
			
			if(Q[j].st.st<=i-1 && Q[j].st.nd>=i){
				ans[Q[j].nd]=max(ans[Q[j].nd], dp[Q[j].st.st]+dp[Q[j].st.nd]);
			}
			if(Q[j].st.nd>=i && Q[j].st.st==i){
				ans[Q[j].nd]=max(ans[Q[j].nd], dp[Q[j].st.nd]);
			}
			if(Q[j].st.st<=i-1 && Q[j].st.nd==i-1){
				ans[Q[j].nd]=max(ans[Q[j].nd], dp[Q[j].st.st]);
			}
		}
	}
	for(int j=0; j<Q.size(); j++){
		if(Q[j].st.st+k-1==Q[j].st.nd){
			ans[Q[j].nd]=max(ans[Q[j].nd], ile[Q[j].st.nd]);
		}
	}
}
void solve_small(vector<pair<pii, int> > Q){
	for(int i=0; i<Q.size(); i++){
		int l=1, r=n;
		int v=1;
		if(Q[i].st.nd-Q[i].st.st<3*k){
			int l2=Q[i].st.st, r2=Q[i].st.nd;
			dp[l2-1]=0;
			for(int i=l2; i<=r2; i++){
				dp[i]=dp[i-1];
				if(i-k>=l2-1)dp[i]=max(dp[i], dp[i-k]+ile[i]);
			}
			ans[Q[i].nd]=dp[r2];
			continue;
		}
		while(v<N){
			int m=(l+r)>>1;
			if(l<r && Q[i].st.nd<=m){
				r=m;
				v=2*v;
			}
			else if(l<r && Q[i].st.st>m){
				l=m+1;
				v=2*v+1;
			}
			else{
				quer[v].pb(Q[i]);
				break;
			}
		}
	}
	solve(1, n, 1);

}
const int K=25600, L=80;
int reszt[N], dob[N];
void solve_big(vector<pair<pii, int> > Q){
	for(int i=1; i<=k; i+=L){
		reszt[i%k]=1;
	}
	int wsk=0;
	for(int i=1; i<=n; i++){
		if(reszt[i%k]){
			dob[i]=++wsk;
			dp[i-1]=0;
			for(int j=i; j<=n; j++){
				dp[j]=dp[j-1];
				if(j-k>=i-1)dp[j]=max(dp[j], dp[j-k]+ile[j]);
			}
			dp[i]=0;
			for(int j=i-1; j>=1; j--){
				dp[j]=dp[j+1];
				if(j+k<=i)dp[j]=max(dp[j], dp[j+k]+ile[j+k-1]);
			}
			if(k==1){
				dp[i]=max(0, tab[i]);
				dp[i-1]=max(0, tab[i-1]);
			}
			for(int j=0; j<Q.size(); j++){
				if(Q[j].st.st<=i-1 && Q[j].st.nd>=i){
					ans[Q[j].nd]=max(ans[Q[j].nd], dp[Q[j].st.st]+dp[Q[j].st.nd]);
				}
				/*if(Q[j].st.st<=i-1 && Q[j].st.nd==i-1){
					ans[Q[j].nd]=max(ans[Q[j].nd], dp[Q[j].st.st]);
				}
				if(Q[j].st.st==i && Q[j].st.nd>=i){
					ans[Q[j].nd]=max(ans[Q[j].nd], dp[Q[j].st.nd]);
				}*/
			}
		}
	}
	//cout<<ans[0]<<"\n";
	dob[0]=1;
	for(auto j:Q){
		int l=j.st.st, r=j.st.nd, nr=j.nd;
		if(r-l+1<k)continue;
		int l2=l, r2=l+1;
		while(!dob[l2])l2--;
		while(!dob[r2] && r2<=r)r2++;
		l2--;
		r2--;
		//cout<<l<<" "<<r<<" "<<l2<<" "<<r2<<"\n";
		for(int i=l2; i<l-1; i++)dp[i]=-1e18;
		for(int i=l-1; i<=r2; i++)dp[i]=0;
		if(r2-k>=l-1)dp[r2]=max(0ll, ile[r2]);
		for(int m=1; m*k+l2<=r; m++){
			for(int i=m*k+l2; i<=m*k+r2 && i<=n; i++){
				dp[i]=0;
				if(i!=m*k+l2 || r2==l2+k){
					dp[i]=dp[i-1];
					//assert(vis[i-1]);
				}
				if(dp[i-k]+ile[i]>dp[i]){
					dp[i]=dp[-k+i]+ile[i];
					//assert(vis[i-k]);
				}
				//vis[i]=1;
				if(i==r)ans[nr]=max(ans[nr], dp[r]);
			}
		}
	}
}
int main(){
	BOOST;
	int q;
	cin>>n>>k>>q;
	//n=q=3e5;
	//k=2151;
	for(int i=1; i<=n; i++){
		cin>>tab[i];
		//tab[i]=rng()%int(1e9);
		pref[i]=pref[i-1]+tab[i];
		if(i>=k)ile[i]=pref[i]-pref[i-k];
	}
	vector<pair<pii, int> > Q;
	for(int i=0; i<q; i++){
		int a, b;
		cin>>a>>b;
		//a=rng()%k+1;
		//b=n-rng()%k;
		//if(a>b)swap(a, b);
		//a=5, b=n;
		Q.pb(mp(mp(a, b), i));
	}

	if(k>2150)solve_big(Q);
	else solve_small(Q);
	for(int i=0; i<q; i++){
		cout<<ans[i]<<"\n";
	}
}