D. Lonely Mountain Dungeons

D. Lonely Mountain Dungeons

Once, the people, elves, dwarves, and other inhabitants of Middle-earth gathered to reclaim the treasures stolen from them by Smaug. In the name of this great goal, they rallied around the powerful elf Timothy and began to plan the overthrow of the ruler of the Lonely Mountain.

The army of Middle-earth inhabitants will consist of several squads. It is known that each pair of creatures of the same race, which are in different squads, adds $b$ units to the total strength of the army. But since it will be difficult for Timothy to lead an army consisting of a large number of squads, the total strength of an army consisting of $k$ squads is reduced by $(k - 1) \cdot x$ units. Note that the army always consists of at least one squad.

It is known that there are $n$ races in Middle-earth, and the number of creatures of the $i$-th race is equal to $c_i$. Help the inhabitants of Middle-earth determine the maximum strength of the army they can assemble.

Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 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$, $b$, and $x$ ($1 \le n \le 2 \cdot 10^5$, $1 \le b \le 10^6, 0 \le x \le 10^9$) — the number of races and the constants $b$ and $x$ described above.

The second line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 2 \cdot 10^5$) — the number of creatures of each of the $n$ races.

It is guaranteed that the sum of the values $c_1 + c_2 + \ldots + c_n$ over all test cases does not exceed $2 \cdot 10^5$.

Output

For each test case, output a single integer — the maximum strength of the army that the inhabitants of Middle-earth can assemble.

Example

Input

5
3 1 0
1 2 3
3 5 10
2 5 3
4 3 3
3 2 1 2
4 1 0
4 1 4 2
4 1 10
4 1 4 2

Output

4
40
9
13
0

Note

In the first test case, the inhabitants of Middle-earth can form $3$ squads. Since $x = 0$, the army's strength will not decrease due to the number of squads. The inhabitants can be distributed among the squads as follows:

  • The single representative of the first species can be sent to the first squad.
  • The first representative of the second species can be sent to the first squad, the second representative of the second species can be sent to the second squad. Then the total strength of the army will increase by $b = 1$.
  • The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by $3 \cdot b = 3$, as they form three pairs in different squads.

Thus, the total strength of the army is $4$.

In the second test case, the inhabitants of Middle-earth can form $3$ squads. Since $x = 10$, the army's strength will decrease by $20$. The inhabitants can be distributed among the squads as follows:

  • The first representative of the first species can be sent to the first squad, the second representative of the first species can be sent to the second squad. Then the total strength of the army will increase by $b = 5$.
  • The first and second representatives of the second species can be sent to the first squad, the third and fourth representatives of the second species can be sent to the second squad, the fifth representative of the second species can be sent to the third squad. Then the total strength of the army will increase by $8 \cdot b = 40$.
  • The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by $3 \cdot b = 15$, as they form three pairs in different squads.

Thus, the total strength of the army is $5 + 40 + 15 - 20 = 40$.

 

解题思路

  对于固定的 $k$,容易知道每个 $c_i$ 的分配是相互独立的,因此可以依次单独考虑。当尽可能把 $c_i$ 平均分配成 $k$ 份时,能获得的数对数量最大。即有 $r = c_i \bmod k$ 条队被分配了 $\left\lceil \frac{c}{k} \right\rceil$ 个,剩余的 $k-r$ 条队被分配了 $\left\lfloor \frac{c}{k} \right\rfloor$ 个。数对有三种情况:两个都来自被分配了 $\left\lfloor \frac{c}{k} \right\rfloor$ 的队,数量为 $C_{k-r}^{2} \cdot \left\lfloor \frac{c}{k} \right\rfloor^2$。两个都来自被分配了 $\left\lceil \frac{c}{k} \right\rceil$ 的队,数量为 $C_{r}^{2} \cdot \left\lceil \frac{c}{k} \right\rceil^2$。两个来自被分配了不同个数的队,数量为 $(k-r) \cdot r \cdot \left\lfloor \frac{c}{k} \right\rfloor \cdot \left\lceil \frac{c}{k} \right\rceil$。

  对于每个 $c_i$,枚举 $j \in [1, c_i]$ 用上面的方法来计算当 $k = j$ 时 $c_i$ 的贡献(数对)是多少,并累加到 $s_j$ 中。

  记 $m = \max\limits_{1 \leq i \leq n}\{c_i\}$,枚举 $1 \sim m$ 表示 $k$ 所有可能的情况,并取最大值。其中对于 $c_i < k$ 的情况,显然每条队只分配 $1$ 个是最优的,数对数量为 $C_{c_i}^{2}$。用 $\text{sum}$ 来维护所有 $c_i < k$ 的 $C_{c_i}^{2}$ 的总和。那么当有 $k$ 条队时,总的数对数量就是 $s_k + \text{sum}$,对答案的贡献为 $(s_k + \text{sum}) \cdot b + (k-1) \cdot x$。

  AC 代码如下,时间复杂度为 $O\left(\sum{c_i}\right)$:

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

typedef long long LL;

const int N = 2e5 + 10;

int c[N], cnt[N];
LL s[N];

LL get(int c, int k) {
    int r = c % k;
    LL ret = 0;
    if (k - r >= 2) ret += (k - r) * (k - r - 1ll) / 2 * (c / k) * (c / k);
    if (r >= 2) ret += r * (r - 1ll) / 2 * ((c + k - 1) / k) * ((c + k - 1) / k);
    ret += 1ll * (k - r) * r * (c / k) * ((c + k - 1) / k);
    return ret;
}

void solve() {
    int n, b, x;
    scanf("%d %d %d", &n, &b, &x);
    for (int i = 0; i < n; i++) {
        scanf("%d", c + i);
        cnt[c[i]]++;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 1; j <= c[i]; j++) {
            s[j] += get(c[i], j);
        }
    }
    LL ret = 0, sum = 0;
    int m = *max_element(c, c + n);
    for (int i = 1; i <= m; i++) {
        ret = max(ret, (s[i] + sum) * b - (i - 1ll) * x);
        sum += get(i, i) * cnt[i];
    }
    printf("%lld\n", ret);
    for (int i = 0; i < n; i++) {
        cnt[c[i]] = 0;
    }
    for (int i = 1; i <= m; i++) {
        s[i] = 0;
    }
}

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

 

参考资料

  Codeforces Round 924 Editorial:https://codeforces.com/blog/entry/125740

posted @ 2024-02-13 20:33  onlyblues  阅读(35)  评论(0编辑  收藏  举报
Web Analytics