luogu#P12074. [OOI 2025] The arithmetic exercise

[OOI 2025] The arithmetic exercise

题目描述

Oleg and Dasha participated in a team competition, but unfortunately, they were unable to solve any problems. Oleg immediately realized that their team wasn't training enough. Then, their mutual friend suggested an interesting exercise. The exercise was quite simple, and to solve it, they only needed to know the rules of addition and subtraction of integers.

You are given an array aa of length nn, where initially all values are zero. You are also given mm numbers x1, x2,  xmx_1,\ x_2,\ \ldots\ x_m. Then, for each ii from 11 to mm, you choose some index jij_i and make the change aji=xiajia_{j_i} = x_i - a_{j_i}.

Help Oleg and Dasha determine what the maximum possible sum of the elements of array aa can be after all the changes, if the choices are made optimally.

输入格式

Each test consists of several input data sets. The first line contains a single integer tt (1t100001 \le t \le 10\,000) --- the number of input data sets. Then follows the description of the data sets.

The first line of each data set contains two integers nn and mm (1n,m3000001 \le n, m \le 300\,000) --- the length of the array aa and the number of values xix_i, respectively.

The second line of each data set contains mm integers x1x_1, x2x_2, \ldots, xmx_m (109xi109-10^9 \le x_i \le 10^9) --- the description of the values.

Let NN be the sum of nn over all data sets, and MM be the sum of mm over all data sets.

It is guaranteed that NN and MM do not exceed 300000300\,000.

输出格式

For each data set, output a single number on a new line --- the maximum sum of the array aa that can be obtained.

4
1 4
1 2 3 4
2 7
10 3 7 1 4 6 3
4 10
103 354 1 227 179 189 142 201 165 140
5 3
-10 11 -4
2
18
1085
17

提示

Note

In the first data set, all operations are applied to the first element of the array aa. It sequentially becomes 10=11 - 0 = 1, 21=12 - 1 = 1, 31=23 - 1 = 2, 42=24 - 2 = 2, so the answer is 22.

In the second data set, the following sequence of changes can be performed:

  • Apply the change to the first element: a1=10a1=100=10a_1 = 10 - a_1 = 10 - 0 = 10, a=[10,0]a = [10, 0].
  • Apply the change to the first element: a1=3a1=310=7a_1 = 3 - a_1 = 3 - 10 = -7, a=[7,0]a = [-7, 0].
  • Apply the change to the first element: a1=7a1=7(7)=14a_1 = 7 - a_1 = 7 - (-7) = 14, a=[14,0]a = [14, 0].
  • Apply the change to the first element: a1=1a1=114=13a_1 = 1 - a_1 = 1 - 14 = -13, a=[13,0]a = [-13, 0].
  • Apply the change to the second element: a2=4a2=40=4a_2 = 4 - a_2 = 4 - 0 = 4, a=[13,4]a = [-13, 4].
  • Apply the change to the first element: a1=6a1=6(13)=19a_1 = 6 - a_1 = 6 - (-13) = 19, a=[19,4]a = [19, 4].
  • Apply the change to the second element: a2=3a2=34=1a_2 = 3 - a_2 = 3 - 4 = -1, a=[19,1]a = [19, -1].

At the end, we have a=[19,1]a = [19, -1], so the final sum is 1818.

It can be shown that a better result is not possible.

Scoring

The tests for this problem consist of ten groups. Points for each group are given only if all tests of the group and all tests of the required groups are passed. Please note that passing the example tests is not required for some groups. Offline-evaluation\textbf{Offline-evaluation} means that the results of testing your solution on this group will only be available after the end of the competition.

Group Points Additional constraints: n,Nn, N Additional constraints: m,Mm, M Additional constraints: xix_i Required Groups Comment
0 -- -- -- Examples.
1 4 0xi0 \le x_i All xix_i are same
2 8 n=2n=2 M30M \le 30, m18m \le 18 --
3 11 M50M \le 50 10xi10-10 \le x_i \le 10
4 9 M400M \le 400 400xi400-400 \le x_i \le 400 3
5 8 N30N \le 30, n18n \le 18 M30M \le 30, m18m \le 18 -- 0
6 10 N2000N \le 2000 M2000M \le 2000 0xi0 \le x_i --
7 12 -- 0, 2 -- 6
8 10 -- 0xi0 \le x_i 1 There are no more than two different values among xix_i
9 17 1, 6, 8
10 11 -- 0 -- 9 Offline-evaluation.