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 (k1)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 ci. 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 (1t2104) — 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 (1n2105, 1b106,0x109) — the number of races and the constants b and x described above.

The second line of each test case contains n integers c1,c2,,cn (1ci2105) — the number of creatures of each of the n races.

It is guaranteed that the sum of the values c1+c2++cn over all test cases does not exceed 2105.

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 3b=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 8b=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 3b=15, as they form three pairs in different squads.

Thus, the total strength of the army is 5+40+1520=40.

 

解题思路

  对于固定的 k,容易知道每个 ci 的分配是相互独立的,因此可以依次单独考虑。当尽可能把 ci 平均分配成 k 份时,能获得的数对数量最大。即有 r=cimodk 条队被分配了 ck 个,剩余的 kr 条队被分配了 ck 个。数对有三种情况:两个都来自被分配了 ck 的队,数量为 Ckr2ck2。两个都来自被分配了 ck 的队,数量为 Cr2ck2。两个来自被分配了不同个数的队,数量为 (kr)rckck

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

  记 m=max1in{ci},枚举 1m 表示 k 所有可能的情况,并取最大值。其中对于 ci<k 的情况,显然每条队只分配 1 个是最优的,数对数量为 Cci2。用 sum 来维护所有 ci<kCci2 的总和。那么当有 k 条队时,总的数对数量就是 sk+sum,对答案的贡献为 (sk+sum)b+(k1)x

  AC 代码如下,时间复杂度为 O(ci)

#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 @   onlyblues  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2023-02-13 D. Moscow Gorillas
2023-02-13 B. Fedya and Array
2022-02-13 飞行员兄弟
Web Analytics
点击右上角即可分享
微信分享提示