codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2239B.md (2549B)


      1 Source: https://codeforces.com/problemset/problem/2239/B
      2 
      3 B. Decidophobia
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 There are $n$ people attending a round-table party, numbered $1, 2, 3, \ldots, n$ in clockwise order. You have prepared some gifts to distribute among them.
     22 
     23 Each person $i$ has a weight $a_i$ and a common field of view $d$. The field of view for person $i$ consists of the $d$ people sitting clockwise and the $d$ people sitting counter-clockwise from them (a total of $2d$ people excluding person $i$).
     24 
     25 The happiness gained by person $i$ is determined by the following rules:
     26 
     27 * If person $i$ receives a gift, and there are $x$ people within their field of view who did not receive a gift, they gain $x \cdot a_i$ happiness.
     28 * If person $i$ does not receive a gift, and there are $x$ people within their field of view who received a gift, they incur $-x \cdot a_i$ happiness.
     29 
     30 You want to maximize the total happiness of all $n$ people combined. Find this maximum value.
     31 
     32 Input
     33 
     34 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
     35 
     36 The first line of each test case contains two integers $n$ and $d$ ($3 \le n \le 2 \cdot 10^5$, $1 \le d \lt \frac{n}{2}$).
     37 
     38 The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^8$), where $a_i$ is the weight of the $i$-th person.
     39 
     40 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
     41 
     42 Output
     43 
     44 For each test case, output a single integer representing the maximum total happiness.
     45 
     46 Example
     47 
     48 Input
     49 
     50 ```
     51 5
     52 
     53 3 1
     54 
     55 1 2 3
     56 
     57 5 1
     58 
     59 1 4 5 2 6
     60 
     61 6 2
     62 
     63 1 1 4 5 1 4
     64 
     65 10 2
     66 
     67 230 24 3 42 432 234 934 2389 333 444
     68 
     69 3 1
     70 
     71 100000000 100000000 100000000
     72 ```
     73 
     74 Output
     75 
     76 ```
     77 3
     78 
     79 15
     80 
     81 26
     82 
     83 8590
     84 
     85 0
     86 ```
     87 
     88 Note
     89 
     90 In the first test case, there are 3 people sitting in a circle. For each person $i$, the field of view is $d=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 \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.
     91 
     92 In the second test case, $n=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.