CF2239B.Decidophobia

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn people attending a round-table party, numbered 1,2,3,…,n1, 2, 3, \ldots, n in clockwise order. You have prepared some gifts to distribute among them.

Each person ii has a weight aia_i and a common field of view dd. The field of view for person ii consists of the dd people sitting clockwise and the dd people sitting counter-clockwise from them (a total of 2d2d people excluding person ii).

The happiness gained by person ii is determined by the following rules:

  • If person ii receives a gift, and there are xx people within their field of view who did not receive a gift, they gain x⋅aix \cdot a_i happiness.
  • If person ii does not receive a gift, and there are xx people within their field of view who received a gift, they incur −x⋅ai-x \cdot a_i happiness.

You want to maximize the total happiness of all nn people combined. Find this maximum value.

有 nn 个人参加一场圆桌宴会,按顺时针方向编号为 1,2,3,…,n1, 2, 3, \ldots, n。你已准备了一些礼物分发给他们。

每个人 ii 有一个权重 aia_i 和一个公共视野范围 dd。第 ii 个人的视野范围包括其顺时针方向的 dd 个人以及逆时针方向的 dd 个人(共 2d2d 人,不包含 ii 自身)。

第 ii 个人所获得的幸福感由以下规则决定:

  • 若第 ii 个人收到礼物,且其视野范围内有 xx 个人未收到礼物,则其获得 x⋅aix \cdot a_i 的幸福感;
  • 若第 ii 个人未收到礼物,且其视野范围内有 xx 个人收到了礼物,则其产生 −x⋅ai-x \cdot a_i 的幸福感(即损失 x⋅aix \cdot a_i 的幸福感)。

你希望最大化所有 nn 个人的总幸福感。求该最大值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and dd (3≤n≤2⋅1053 \le n \le 2 \cdot 10^5, 1≤d<n21 \le d \lt \frac{n}{2}).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1081 \le a_i \le 10^8), where aia_i is the weight of the ii-th person.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 dd(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5,1≤d<n21 \le d \lt \frac{n}{2})。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1081 \le a_i \le 10^8),其中 aia_i 表示第 ii 个人的体重。

保证所有测试用例中 nn 的总和不超过 10610^6。

输出格式

For each test case, output a single integer representing the maximum total happiness.

对于每个测试用例,输出一个整数,表示最大总幸福感。

输入输出样例

  • 输入#1

    5
    3 1
    1 2 3
    5 1
    1 4 5 2 6
    6 2
    1 1 4 5 1 4
    10 2
    230 24 3 42 432 234 934 2389 333 444
    3 1
    100000000 100000000 100000000

    输出#1

    3
    15
    26
    8590
    0

说明/提示

In the first test case, there are 3 people sitting in a circle. For each person ii, the field of view is d=1d=1, which includes the 1 person sitting clockwise and the 1 person sitting counter-clockwise from them. If person 2 receives the gift, they gain happiness from 2 neighbors who did not receive it, resulting in 2⋅a2=2⋅2=42 \cdot a_2 = 2 \cdot 2 = 4. However, person 1 and person 3 incur loss because they did not receive a gift but have a neighbor who did. After calculating all possibilities, the maximum total happiness is 3.

In the second test case, n=5,d=1n=5, d=1. The best solution is to give gifts to persons 2, 3, and 5, which can achieve the maximum total happiness value of 15.

在第一个测试用例中,有 3 个人围成一圈就坐。对每个人 ii,其视野范围为 d=1d=1,即包括其顺时针方向的 1 个人和逆时针方向的 1 个人。若将礼物送给第 2 个人,则其因获得礼物而从 2 个未收到礼物的邻居处获得幸福感,贡献值为 2⋅a2=2⋅2=42 \cdot a_2 = 2 \cdot 2 = 4。然而,第 1 个人和第 3 个人会遭受损失,因为他们未收到礼物,但各自有一个收到礼物的邻居。在计算所有可能情况后,总幸福感的最大值为 3。

在第二个测试用例中,n=5,d=1n=5, d=1。最优方案是将礼物送给第 2、第 3 和第 5 个人,此时可达到总幸福感的最大值 15。

输入解题思路,AI测评打分。不知道怎么写?

首页