Universal Cup Judging System

Universal Cup

時間限制: 4 s 記憶體限制: 512 MB 總分: 100
统计

You are a trader at an investment company. One day, you discover that the stock you’re responsible for trading follows an exactly repeating daily pattern: the day is divided into $n$ equal time intervals, and the stock’s price during the $i$-th interval is always $a_i$. As the $n$-th interval of one day concludes, the first interval of the next day immediately follows, with the price shifting from $a_n$ to $a_1$. This is a once-in-a-lifetime opportunity — if you can just buy and sell according to this pattern you’ve already figured out, during your working hours, there’s a guaranteed way to profit!

Your working hours vary, and a working period may extend past midnight. You are given $q$ working periods. The $i$-th period starts at slot $l_i$ and ends at slot $r_i$, including both endpoints. The slots in chronological order are:

  • $l_i, l_i + 1, \ldots, r_i$ if $l_i \le r_i$;
  • $l_i, l_i + 1, \ldots, n, 1, 2, \ldots, r_i$ if $l_i > r_i$.

In each working period, you may complete at most $k$ transactions. Each transaction consists of buying one share and selling it in a later time slot, with profit equal to the sale price minus the purchase price. You may hold at most one share at a time, so you must sell your current share before buying another. All purchases and sales must take place within that working period and follow the chronological order above. You may also make no transactions, earning a profit of $0$.

For each working period, independently find the maximum total profit you can earn.

Input

The first line contains three integers $n, k, q$ ($1 \le n \le 10^5$, $1 \le k \le 800$, $1 \le q \le 3 \times 10^5$): the number of time slots in a day, the maximum number of transactions per working period, and the number of working periods, respectively.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the stock price in the $i$-th time slot of each day.

Each of the next $q$ lines contains two integers $l_i, r_i$ ($1 \le l_i, r_i \le n$), describing the $i$-th working period.

Let $\mathrm{len}_i$ be the number of time slots in this period. If $l_i \le r_i$, then $\mathrm{len}_i = r_i - l_i + 1$; otherwise, $\mathrm{len}_i = n - l_i + 1 + r_i$.

For each working period, $l_i$ and $\mathrm{len}_i$ are generated independently and uniformly at random from $[1,n]$ and $[\max(1,\lfloor 0.15n \rfloor), \max(1,\lfloor 0.85n \rfloor)]$, respectively. The value of $r_i$ is uniquely determined by $l_i$ and $\mathrm{len}_i$. Specifically, when the interval is $[1,1]$, the random result will always be $1$.

There are exactly 50 test cases, excluding the sample.

Output

Output $q$ lines. The $i$-th line should contain one integer: the maximum profit you can earn during the $i$-th working period with at most $k$ transactions.

Examples

Input 1

6 2 3
3 1 4 1 5 9
2 5
5 2
4 4

Output 1

7
4
0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.