C. Ticket Hoarding

C. Ticket Hoarding

As the CEO of a startup company, you want to reward each of your $k$ employees with a ticket to the upcoming concert. The tickets will be on sale for $n$ days, and by some time travelling, you have predicted that the price per ticket at day $i$ will be $a_i$. However, to prevent ticket hoarding, the concert organizers have implemented the following measures:

  • A person may purchase no more than $m$ tickets per day.
  • If a person purchases $x$ tickets on day $i$, all subsequent days (i.e. from day $i+1$ onwards) will have their prices per ticket increased by $x$.

For example, if $a = [1, 3, 8, 4, 5]$ and you purchase $2$ tickets on day $1$, they will cost $2$ in total, and the prices from day $2$ onwards will become $[5, 10, 6, 7]$. If you then purchase $3$ more tickets on day $2$, they will cost in total an additional $15$, and the prices from day $3$ onwards will become $[13, 9, 10]$.

Find the minimum spending to purchase $k$ tickets.

Input

Each test contains multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers $n$, $m$, and $k$ ($1 \le n \le 3 \cdot 10^5, 1 \le m \le 10^9, 1 \le k \le \min(nm, 10^9)$) — the number of sale days, the maximum amount of ticket purchasable each day, and the number of tickets to be bought at the end.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the price per ticket for each of the upcoming $n$ days.

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

Output

For each test case, print one integer: the minimum amount of money needed to purchase exactly $k$ tickets.

Example

input

4
4 2 3
8 6 4 2
4 2 8
8 6 4 2
5 100 1
10000 1 100 10 1000
6 3 9
5 5 5 5 5 5

output

10
64
1
72

Note

In the first test case, one optimal way to buy $3$ tickets is as follows:

  • Buy $0$ tickets on the first day. The prices per ticket for the remaining days are $[6, 4, 2]$.
  • Buy $0$ tickets on the second day. The prices per ticket for the remaining days are $[4, 2]$.
  • Buy $1$ ticket on the third day with cost $4$. The price per ticket for the remaining day is $[3]$.
  • Buy $2$ tickets on the fourth day with cost $6$.

In the second test case, there is only one way to buy $8$ tickets:

  • Buy $2$ tickets on the first day with cost $16$. The prices per ticket for the remaining days are $[8, 6, 4]$.
  • Buy $2$ tickets on the second day with cost $16$. The prices per ticket for the remaining days are $[8, 6]$.
  • Buy $2$ tickets on the third day with cost $16$. The price per ticket for the remaining day is $[8]$.
  • Buy $2$ tickets on the fourth day with cost $16$.

 

解题思路

  直观的贪心思路是,选择最小的 $x = \left\lceil \frac{k}{m} \right\rceil$ 个 $a_i$ 购买,其中前 $x-1$ 个 $a_i$ 购买的数量均为 $m$,第 $x$ 个 $a_i$ 购买的数量为 $k - (x-1)\cdot m$。下面给出证明。

  用 $b_i \, (0 \leq b_i \leq m)$ 表示购买 $a_i$ 的数量,且 $\sum\limits_{i=1}^{n}{b_i} = k$。购买 $a_i$ 的代价就是 $\left( a_i + \sum\limits_{j=1}^{i-1}{b_j} \right) b_i$。因此总代价就是:

\begin{align*}
&\sum_{i=1}^{n}{\left( a_i + \sum_{j=1}^{i-1}{b_j} \right) b_i} \\
=& \sum_{i=1}^{n}{a_ib_i} + \sum_{i=1}^{n}{b_i \sum_{j=1}^{i-1}{b_j}} \\
\end{align*}

  可以证明,将 $(a_i, a_j)$ 与 $(a_j, b_j)$ 进行交换,上述等式的值不变。其中 $\sum\limits_{i=1}^{n}{a_ib_i}$ 这部分显然没有变化,下面证明 $\sum\limits_{i=1}^{n}{b_i \sum\limits_{j=1}^{i-1}{b_j}}$ 这部分也是不变的。

  不失一般性,假设 $i \leq j$,将 $\sum\limits_{i=1}^{n}{b_i \sum\limits_{j=1}^{i-1}{b_j}}$ 根据 $i$ 和 $j$ 分成三部分的和 $\sum\limits_{k=1}^{i-1}{b_k \sum\limits_{u=1}^{k-1}{b_u}} + \sum\limits_{k=i}^{j}{b_k \sum\limits_{u=1}^{k-1}{b_u}} + \sum\limits_{k=j+1}^{n}{b_k \sum\limits_{u=1}^{k-1}{b_u}}$。其中 $\sum\limits_{k=1}^{i-1}{b_k \sum\limits_{u=1}^{k-1}{b_u}}$ 这部分显然没有变化,因为没有涉及到 $b_i$ 和 $b_j$。$\sum\limits_{k=j+1}^{n}{b_k \sum\limits_{u=1}^{k-1}{b_u}}$ 这部分也没有变化,因为 $b_i$ 和 $b_j$ 只会出现在 $\sum\limits_{u=1}^{k-1}{b_u}$ 中,而顺序并不会影响求和的结果。

  剩下的就是 $\sum\limits_{k=i}^{j}{b_k \sum\limits_{u=1}^{k-1}{b_u}}$,在 $(a_i, a_j)$ 与 $(a_j, b_j)$ 交换后,考虑 ${\color{Red} {b_i \sum\limits_{u=1}^{i-1}{b_u}}}$,${\color{Green} {\sum\limits_{k=i+1}^{j-1}{b_k \sum\limits_{u=1}^{k-1}{b_u}}}}$,${\color{Blue} {b_j \sum\limits_{u=1}^{i-1}{b_u}}}$ 这三部分发生的变化:

\begin{align*}
&{\color{Red} {b_j\sum_{u=1}^{i-1}{b_u}}} - {\color{Red} {b_i\sum_{u=1}^{i-1}{b_u}}} + {\color{Blue} {b_i\sum_{u=1}^{j-1}{b_u}}} - {\color{Blue} {b_i^2}} + {\color{Blue} {b_ib_j}} - {\color{Blue} {b_j\sum_{u=1}^{j-1}{b_u}}} + {\color{Green} {b_j\sum\limits_{k=i+1}^{j-1}{b_k}}} - {\color{Green} {b_i\sum\limits_{k=i+1}^{j-1}{b_k}}}  \\
=& (b_i - b_j)\sum_{u=1}^{j-1}{b_u} - (b_i - b_j)\sum_{u=1}^{i-1}{b_u} - (b_i - b_j)\sum\limits_{u=i+1}^{j-1}{b_u} - b_i(b_i - b_j) \\
=& (b_i - b_j)\sum_{u=i}^{j-1}{b_u} - (b_i - b_j)\sum\limits_{u=i+1}^{j-1}{b_u} - b_i(b_i - b_j) \\
=& (b_i - b_j)b_i - b_i(b_i - b_j) \\
=& 0
\end{align*}

  这意味着我们当我们确定购买的方案 $b_i$ 后,以任意顺序购买 $a_i$,得到的代价都不变。对 $a_i$ 进行升序排序,并构造方案 $b = [ \underbrace{m,m,\ldots,m}_{x-1}, k - (x-1)\cdot m, 0, 0, \ldots, 0]$。可以证明该购买方案得到的代价最小。

  其中 $\sum\limits_{i=1}^{n}{a_ib_i}$ 这部分根据排序不等式显然在 $0 \leq b_i \leq m$ 且 $\sum\limits_{i=1}^{n}{b_i}=k$ 的限制下,得到的结果最小。而另外一部分由于 $\left(\sum\limits_{i=1}^{n}{b_i}\right)^2 = \sum\limits_{i=1}^{n}{b_i^2} +  2\sum\limits_{i=1}^{n}{b_i \sum\limits_{j=1}^{i-1}{b_j}}$,从而推出 $\sum\limits_{i=1}^{n}{b_i \sum\limits_{j=1}^{i-1}{b_j}} = \frac{1}{2} k^2 - \frac{1}{2} \sum\limits_{i = 1}^{n}{b_i^2}$。从而只需证明 $\sum\limits_{i = 1}^{n}{b_i^2}$ 得到的结果最大。如果存在两个 $i$,$j$ 满足 $0 < b_i \leq b_j < m$,那么用 $(b_i-1, b_j+1)$ 取代 $(b_i,b_j)$,则 $(b_i-1)^2 + (b_j+1)^2 = b_i^2 + b_j^2 + 2(b_j - b_i + 1) > b_i^2 + b_j^2$ 即结果会变大。容易知道当 $b_i$ 为上述方案时,$\sum\limits_{i = 1}^{n}{b_i^2}$ 的结果最大,从而 $\frac{1}{2} k^2 - \frac{1}{2} \sum\limits_{i = 1}^{n}{b_i^2}$ 的结果最小。

  证毕。

  AC 代码如下,时间复杂度为 $O(n \log{n})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 3e5 + 5;

int a[N];

void solve() {
    int n, m, k;
    scanf("%d %d %d", &n, &m, &k);
    for (int i = 0; i < n; i++) {
        scanf("%d", a + i);
    }
    sort(a, a + n);
    LL ret = 0;
    for (int i = 0, s = 0; i < n; i++) {
        int t = min(k, m);
        ret += (LL)t * (a[i] + s);
        k -= t;
        s += t;
    }
    printf("%lld\n", ret);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Global Round 25 Editorial:https://codeforces.com/blog/entry/128116

posted @ 2024-04-09 16:42  onlyblues  阅读(14)  评论(0编辑  收藏  举报
Web Analytics