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
#include <bits/stdc++.h>

using namespace std;
struct Query
{
  size_t left, right;
};

struct TestCase
{
  size_t value_count, range_size, query_count;
  vector<long long> values;
  vector<Query> queries;
};

TestCase read_test_case();
void solve_test_case(const TestCase&);

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve_test_case(read_test_case());
}

TestCase read_test_case()
{
  TestCase tc;
  cin >> tc.value_count >> tc.range_size >> tc.query_count;
  tc.values.resize(tc.value_count);
  tc.queries.resize(tc.query_count);
  for (auto& v : tc.values) cin >> v;
  for (auto& [l, r] : tc.queries) cin >> l >> r;
  return tc;
}

long long solve_query(const TestCase& tc, Query query)
{
  query.left--;
  query.right--;
  const size_t size = query.right - query.left + 1;
  if (size < tc.range_size) return 0;

  long long sum_of_last_range = 0;
  for (size_t i = 0; i < tc.range_size; i++)
    sum_of_last_range += tc.values[query.left + i];

  vector<long long> dp(size);
  for (size_t i = 0; i < tc.range_size; i++) dp[i] = 0;
  dp[tc.range_size - 1] = max(sum_of_last_range, (long long)0);

  for (size_t i = tc.range_size; i < size; i++)
  {
    sum_of_last_range -= tc.values[query.left + i - tc.range_size];
    sum_of_last_range += tc.values[query.left + i];

    long long prev = dp[i - tc.range_size];

    dp[i] = max(dp[i - 1], sum_of_last_range + prev);
  }

  return dp[dp.size() - 1];
}

void solve_test_case(const TestCase& tc)
{
  for (auto query : tc.queries) cout << solve_query(tc, query) << "\n";
}