#P6187. [NOI Online #1 提高组] 最小环

[NOI Online #1 提高组] 最小环

Problem Description

You are given a sequence of positive integers aia_i of length nn, indexed starting from 11. We view this sequence as a ring where the first and last elements are adjacent. More specifically, for two numbers aia_i and aja_j with indices ii, jj (iji \leqslant j), their distance is defined as min(ji,i+nj)\min(j-i, i+n-j).

Now you are also given mm integers k1,k2,,kmk_1, k_2, \ldots, k_m. For each kik_i (i=1,2,,mi=1, 2, \ldots, m), you need to rearrange the sequence aia_i so that the sum of products of every pair of numbers on the ring whose distance is kik_i is maximized.

Input Format

The first line contains two positive integers nn and mm, representing the sequence length and the number of queries.

The next line contains nn positive integers, representing aia_i.

The next mm lines each contain one non-negative integer, representing kik_i.

Output Format

Output mm lines. Each line contains one integer, representing the answer.

6 4
1 2 3 4 5 6
0
1
2
3
91
82
85
88

Hint

Explanation for Sample Input/Output 1

  • When k=0k=0: the answer is the sum of squares of all numbers.
  • When k=1k=1: one optimal arrangement is {3,1,2,4,6,5}\{3,1,2,4,6,5\}. The answer is $3 \times 1 + 1 \times 2 + 2 \times 4 + 4 \times 6 + 6 \times 5 + 5 \times 3 = 82$.
  • When k=2k=2: one optimal arrangement is {3,6,1,4,2,5}\{3,6,1,4,2,5\}. The answer is $3 \times 1 + 1 \times 2 + 2 \times 3 + 6 \times 4 + 4 \times 5 + 5 \times 6 = 85$.
  • When k=3k=3: one valid arrangement is 1,5,3,2,6,41,5,3,2,6,4, and the answer is 8888. Note that the answer here is not 4444.

Constraints and Hints

For all testdata: 1mn2×1051 \leqslant m \leqslant n \leqslant 2 \times 10^5, 0kn/20 \leqslant k \leqslant \lfloor n/2\rfloor, 1ai1051 \leqslant a_i \leqslant 10^5.

Test Point ID nn \leqslant Special Property
1 1010 None
2 1818
3 3636 nn is even and m=1m=1, k=2k=2
4,5 10001000 m10m \leqslant 10, k=1k=1
6 5050 m10m \leqslant 10, k2k \leqslant 2
7,8 30003000 None
9,10 2×1052 \times 10^5

Translated by ChatGPT 5