#P6187. [NOI Online #1 提高组] 最小环
[NOI Online #1 提高组] 最小环
Problem Description
You are given a sequence of positive integers of length , indexed starting from . We view this sequence as a ring where the first and last elements are adjacent. More specifically, for two numbers and with indices , (), their distance is defined as .
Now you are also given integers . For each (), you need to rearrange the sequence so that the sum of products of every pair of numbers on the ring whose distance is is maximized.
Input Format
The first line contains two positive integers and , representing the sequence length and the number of queries.
The next line contains positive integers, representing .
The next lines each contain one non-negative integer, representing .
Output Format
Output 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 : the answer is the sum of squares of all numbers.
- When : one optimal arrangement is . The answer is $3 \times 1 + 1 \times 2 + 2 \times 4 + 4 \times 6 + 6 \times 5 + 5 \times 3 = 82$.
- When : one optimal arrangement is . The answer is $3 \times 1 + 1 \times 2 + 2 \times 3 + 6 \times 4 + 4 \times 5 + 5 \times 6 = 85$.
- When : one valid arrangement is , and the answer is . Note that the answer here is not .
Constraints and Hints
For all testdata: , , .
| Test Point ID | Special Property | |
|---|---|---|
| 1 | None | |
| 2 | ||
| 3 | is even and , | |
| 4,5 | , | |
| 6 | , | |
| 7,8 | None | |
| 9,10 |
Translated by ChatGPT 5