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
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

struct query
{
	int endpoint;
	int index;
};

struct result
{
	long long sum;
	int index;
	bool operator<(const result &b) { return this->index < b.index; }
};

int n, k, q;
std::vector<int> values;
std::vector<long long> dp;
std::vector<long long> prefix_sum;
std::vector<long long> suffix_sum;
std::map<std::pair<int, int>, long long> results;
const int INF = 1000000007;

long long solve(int starting_position, int n)
{
	if (starting_position > n)
		return -INF;
	else if (starting_position == n)
		return 0;
	if (dp[starting_position] != -1)
	{
		return dp[starting_position];
	}
	long long max_value = 0;
	long long current_interval_sum = 0ll;
	if (starting_position + k - 1 < n)
		current_interval_sum = prefix_sum[starting_position + k - 1];
	if (starting_position > 0)
		current_interval_sum -= prefix_sum[starting_position - 1];
	max_value = std::max(solve(starting_position + 1, n), current_interval_sum + solve(starting_position + k, n));
	dp[starting_position] = max_value;
	return max_value;
}

long long solve_reverse(int starting_position, int n)
{
	if (starting_position < n - 1)
		return -INF;
	else if (starting_position == n - 1)
		return 0;
	if (dp[starting_position] != -1)
	{
		return dp[starting_position];
	}
	long long max_value = 0;
	long long current_interval_sum = 0ll;
	if (starting_position - k + 1 >= 0)
		current_interval_sum = suffix_sum[starting_position - k + 1];
	if (starting_position < suffix_sum.size() - 1)
	{
		current_interval_sum -= suffix_sum[starting_position + 1];
	}
	max_value = std::max(solve_reverse(starting_position - 1, n), current_interval_sum + solve_reverse(starting_position - k, n));
	dp[starting_position] = max_value;
	return max_value;
}

int main()
{
	scanf("%d %d %d", &n, &k, &q);
	values.resize(n);
	prefix_sum.assign(n, 0);
	suffix_sum.assign(n, 0);
	dp.assign(n, -1);
	for (int i = 0; i < n; i++)
	{
		int value;
		scanf("%d", &value);
		values[i] = value;
		prefix_sum[i] += value;
		if (i != 0)
			prefix_sum[i] += prefix_sum[i - 1];
	}
	for (int i = n - 1; i >= 0; i--)
	{
		suffix_sum[i] += values[i];
		if (i != n - 1)
		{
			suffix_sum[i] += suffix_sum[i + 1];
		}
	}
	/*for (int i = 0; i < q; i++)
	{
		int l, r;
		dp.clear();
		dp.assign(n, -1);
		scanf("%d %d", &l, &r);
		long long result;
		if (results.find({l, r}) == results.end())
		{
			result = solve(l - 1, r);
			results.insert({{l, r}, result});
		}
		else
			result = results[{l, r}];
		printf("%lld\n", result);
	}*/
	std::vector<std::vector<query>> from_left_queries(n + 1);
	std::vector<std::vector<query>> from_right_queries(n + 1);
	int l_size = 0;
	int r_size = 0;
	for (int i = 0; i < q; i++)
	{
		int l, r;
		scanf("%d %d", &l, &r);
		if (from_left_queries[l].size() == 0)
			l_size++;
		if (from_right_queries[r].size() == 0)
			r_size++;
		from_left_queries[l].push_back({r, i});
		from_right_queries[r].push_back({l, i});
	}
	std::vector<result> results;
	if (l_size > r_size)
	{
		for (int i = 1; i <= n; i++)
		{
			if (from_right_queries[i].size() > 0)
			{
				dp.clear();
				dp.assign(n, -1);
				solve(0, i);
				for (auto r_query : from_right_queries[i])
				{
					results.push_back({dp[r_query.endpoint - 1], r_query.index});
				}
			}
		}
	}
	else
	{
		for (int i = 1; i <= n; i++)
		{
			if (from_left_queries[i].size() > 0)
			{
				dp.clear();
				dp.assign(n, -1);
				solve_reverse(n - 1, i - 1);
				for (auto l_query : from_left_queries[i])
				{
					results.push_back({dp[l_query.endpoint -1], l_query.index});
				}
			}
		}
	}
	std::sort(results.begin(), results.end());
	for (int i = 0; i < q; i++)
	{
		printf("%lld\n", results[i]);
	}
}