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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <bits/stdc++.h>

using namespace std;

const int N = 300'007;
const int SPLIT_K = 700;

int n, k, q;
int in[N];
long long pref[N];

long long ans[N];
pair <int, int> query[N];

void read()
{
    scanf("%d %d %d", &n, &k, &q);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &in[i]);
        pref[i] = pref[i - 1] + in[i];
    }

    for (int i = 1; i <= q; ++i) {
        scanf("%d %d", &query[i].first, &query[i].second);
    }
}

namespace Small {
    int query_order[N];
    int tmp_order[N];
    int cur_order[N];

    long long left_dp[N], right_dp[N];

    pair <int, int> reorder_queries(int from, int to, int mid_n)
    {   
        int left = from, right = to + 1;
        int other = 0;

        for (int i = from; i <= to; ++i) {
            int id = query_order[i];
            bool in_left = query[id].first <= mid_n;
            bool in_right = query[id].second > mid_n;

            if (in_left && in_right) {
                cur_order[++other] = id;
            } else if (!in_right) {
                tmp_order[left++] = id;
            } else {
                tmp_order[--right] = id;
            }
        }

        for (int i = 1; i <= other; ++i) {
            tmp_order[left + i - 1] = cur_order[i];
        }

        for (int i = from; i <= to; ++i) {
            query_order[i] = tmp_order[i];
        }

        return {left, right};
    }

    void calculate_left(int bound, int start) 
    {
        left_dp[start + 1] = 0;
        for (int i = start; i >= bound; --i) 
        {
            left_dp[i] = left_dp[i + 1];
            if (i + k - 1 <= start) {
                left_dp[i] = max(left_dp[i], left_dp[i + k] + pref[i + k - 1] - pref[i - 1]);
            }
        }
    }

    void calculate_right(int bound, int start)
    {
        right_dp[start - 1] = 0;
        for (int i = start; i <= bound; ++i)
        {
            right_dp[i] = right_dp[i - 1];
            if (i - k + 1 >= start) {
                right_dp[i] = max(right_dp[i], right_dp[i - k] + pref[i] - pref[i - k]);
            }
        }
    }

    void solve_queries(int from_n, int to_n, int from_q, int to_q)
    {
        if (from_q > to_q) {
            return;
        }

        if (to_n - from_n + 1 < k) {
            return;
        }

        if (to_n - from_n + 1 == k) {
            long long to_set = max(0LL, pref[to_n] - pref[from_n - 1]);
            for (int i = from_q; i <= to_q; ++i) {
                const int &id = query_order[i];
                if (query[id].first == from_n && query[id].second == to_n) {
                    ans[id] = to_set;
                }
            }

            return;
        }

        int mid_n = (from_n + to_n) / 2;
        auto [left_queries, right_queries] = reorder_queries(from_q, to_q, mid_n);

        calculate_left(from_n, mid_n);
        calculate_right(to_n, mid_n + 1);

        for (int i = left_queries; i < right_queries; ++i) {
            const int &id = query_order[i];
            const auto &[x, y] = query[id];
            ans[id] = max(ans[id], left_dp[x] + right_dp[y]);
        }

        for (int i = 0; i + 1 < k; ++i) {
            int intrv_start = mid_n - i;
            if (intrv_start < from_n || intrv_start + k - 1 > to_n) {
                continue;
            }

            calculate_left(from_n, intrv_start - 1);
            calculate_right(to_n, intrv_start + k);
            
            for (int j = left_queries; j < right_queries; ++j) {
                const int &id = query_order[j];
                const auto &[x, y] = query[id];
                
                if (x <= intrv_start && intrv_start + k - 1 <= y) {
                    ans[id] = max(ans[id], left_dp[x] + right_dp[y] + 
                                    pref[intrv_start + k - 1] - pref[intrv_start - 1]);
                }
            }
        }

        solve_queries(from_n, mid_n, from_q, left_queries - 1);
        solve_queries(mid_n + 1, to_n, right_queries, to_q);
    }

    void solve()
    {
        for (int i = 1; i <= q; ++i) {
            query_order[i] = i;
        }

        solve_queries(1, n, 1, q);
    }
}

namespace Big {
    int query_order[N];
    int tmp_order[N];
    int cur_order[N];

    long long dp_left[N];
    long long dp_right[N];

    pair <int, int> reorder_queries(int from, int to, int mid_k)
    {   
        int left = from, right = to + 1;
        int other = 0;

        for (int i = from; i <= to; ++i) {
            const int &id = query_order[i];
            const auto &[l, r] = query[id];

            bool in_left = l % k <= mid_k;
            bool in_right = r % k >= mid_k;

            if (in_left && in_right) {
                cur_order[++other] = id;
            } else if (!in_right) {
                tmp_order[left++] = id;
            } else {
                tmp_order[--right] = id;
            }
        }

        for (int i = 1; i <= other; ++i) {
            tmp_order[left + i - 1] = cur_order[i];
        }
        
        for (int i = from; i <= to; ++i) {
            query_order[i] = tmp_order[i];
        }

        return {left, right};
    }

    void calculate(int p, int from, int to) {
        { /* dp_right */
            int cur = p;
            while (cur <= n) {
                dp_right[cur] = (cur % k == p % k) ? 0 : dp_right[cur - 1];

                if (cur - k >= p) {
                    dp_right[cur] = max(dp_right[cur], dp_right[cur - k] + pref[cur] - pref[cur - k]);
                }

                if (cur % k == to) {
                    cur += k - to + p % k;
                } else {
                    cur++;
                }
            }
        }

        { /* dp_left */
            int cur = p;
            while (cur >= 0) {
                dp_left[cur] = (cur % k == p % k) ? 0 : dp_left[cur + 1];

                if (cur + k <= p) {
                    dp_left[cur] = max(dp_left[cur], dp_left[cur + k] + pref[cur + k] - pref[cur]);
                }

                if (cur % k == from) {
                    cur -= k + from - p % k;
                } else {
                    cur--;
                }
            }
        }
    }

    void solve_queries(int from_k, int to_k, int from_q, int to_q)
    {
        if (to_q < from_q) {
            return;
        }

        if (from_k > to_k) {
            return;
        }

        if (from_k == to_k) {
            for (int i = from_q; i <= to_q; ++i) {
                const int &id = query_order[i];
                const auto &[l, r] = query[id];

                ans[id] = max(ans[id], pref[r] - pref[l]);
            }

            return;
        }

        int mid_k = (from_k + to_k) / 2;
        auto [left_queries, right_queries] = reorder_queries(from_q, to_q, mid_k);

        for (int p = mid_k; p <= n; p += k) {
            calculate(p, from_k, to_k);

            for (int i = left_queries; i < right_queries; ++i) {
                const int &id = query_order[i];
                const auto &[l, r] = query[id];

                if (l <= p && p <= r) {
                    ans[id] = max(ans[id], dp_left[l] + dp_right[r]);
                }
            }
        }

        solve_queries(from_k, mid_k - 1, from_q, left_queries - 1);
        solve_queries(mid_k + 1, to_k, right_queries, to_q);
    }

    void solve()
    {
        for (int i = 1; i <= q; ++i) {
            query[i].first--;
        }

        for (int i = k; i <= n; i += k) {
            dp_right[i] = 0;
            for (int j = i; j <= n; ++j) {
                dp_right[j] = dp_right[j - 1];

                if (i + k <= j) {
                    dp_right[j] = max(dp_right[j], dp_right[j - k] + pref[j] - pref[j - k]);
                }
            }

            dp_left[i] = 0;
            for (int j = i - 1; j >= 0; --j) {
                dp_left[j] = dp_left[j + 1];

                if (j + k <= i) {
                    dp_left[j] = max(dp_left[j], dp_left[j + k] + pref[j + k] - pref[j]);
                }
            }

            for (int j = 1; j <= q; ++j) {
                const auto &[l, r] = query[j];
                if (l <= i && i <= r) {
                    ans[j] = max(ans[j], dp_left[l] + dp_right[r]);
                }
            }
        }

        int queries = 0;
        for (int i = 1; i <= q; ++i) {
            const auto &[l, r] = query[i];
            if (l % k <= r % k) {
                query_order[++queries] = i;
            }
        }

        solve_queries(0, k - 1, 1, queries);
    }
}

int main()
{
    read();

    if (k < SPLIT_K) {
        Small::solve();
    } else {
        Big::solve();
    }

    for (int i = 1; i <= q; ++i) {
        printf("%lld\n", ans[i]);
    }

    return 0;
}