#CF2240D. 选择恐惧症 / D. Decidophobia
选择恐惧症 / D. Decidophobia
D. Decidophobia
Source: Codeforces 2240D
Contest: Codeforces Round 1105 (Div. 2)
Time limit: 2 seconds
Memory limit: 256 megabytes
Statement
There are people attending a round-table party, numbered in clockwise order. You have prepared some gifts to distribute among them.
Each person has a weight and a common field of view . The field of view for person consists of the people sitting clockwise and the people sitting counter-clockwise from them (a total of people excluding person ).
The happiness gained by person is determined by the following rules:
- If person receives a gift, and there are people within their field of view who did not receive a gift, they gain happiness.
- If person does not receive a gift, and there are people within their field of view who received a gift, they incur happiness.
You want to maximize the total happiness of all people combined. Find this maximum value.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains two integers and (, ).
The second line contains integers (), where is the weight of the -th person.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer representing the maximum total happiness.
Note
In the first test case, there are 3 people sitting in a circle. For each person , the field of view is , 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 . 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, . The best solution is to give gifts to persons 2, 3, and 5, which can achieve the maximum total happiness value of 15.
Examples
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
3
15
26
8590
0