luogu#P5101. [JOI 2017 Final] 绳 / Rope

[JOI 2017 Final] 绳 / Rope

Problem Description

This problem is translated from JOI 2017 Final T5 「 / Rope」.

JOI is playing with a rope. The rope can be regarded as a line segment extending left and right with length NN. The rope is made by connecting NN pieces of string. Each piece has length 11 and thickness 11. There are MM colors on the rope. The color of the ii-th piece from the left (1iN)(1\leqslant i\leqslant N) is Ci(1CiM)C_i(1\leqslant C_i\leqslant M). The left endpoint of the rope means the left endpoint of the 11-st piece from the left, and the right endpoint of the rope means the right endpoint of the 11-st piece from the right. Clearly, the distance from the right endpoint of the ii-th piece from the left (1iN)(1\leqslant i\leqslant N) to the left endpoint of the rope is ii.

JOI shortened the rope. More precisely, JOI repeatedly performs the following process until the rope length becomes 22.

  • Suppose the current rope length is LL. Choose an integer j(1j<L)j(1\leqslant j<L), make the jj-th piece from the left become the left endpoint of the rope (the leftmost piece), and fold the rope. That is,
    • If jL2j\leqslant \Large\frac{L}{2}, then twist the ii-th piece from the left (1ij)(1\leqslant i\leqslant j) together with the (2ji+1)(2j-i+1)-th piece from the left. In this case, the original right endpoint of the rope remains the right endpoint, and the rope length becomes LjL-j.
    • If j>L2j> \Large\frac{L}{2}, then twist the ii-th piece from the left (2jL+1ij)(2j-L+1\leqslant i\leqslant j) together with the (2ji+1)(2j-i+1)-th piece from the left. In this case, the original left endpoint of the rope becomes the right endpoint, and the rope length becomes jj.
  • Two pieces can be twisted together only if they have the same color. Before twisting two pieces together, you may change their colors arbitrarily. The cost to dye a piece into another color is equal to the thickness of that piece. After the colors match, the two pieces are twisted into one, and the thickness of the new piece becomes the sum of the thicknesses of the two pieces.

We call the rope of length 22 obtained after shortening the final rope. JOI wants to minimize the total cost required to shorten the rope to length 22. For each color, JOI wants to know the minimum cost required to shorten the rope to length 22 under the condition that the final rope contains this color.

Your task is to help JOI solve this problem.

Input Format

The first line contains two integers N,MN, M, separated by spaces.
The second line contains NN integers C1,C2,,CNC_1, C_2, \ldots, C_N, separated by spaces.
The meanings of all numbers are given in the statement.

Output Format

Output MM lines. The cc-th line (1cM)(1\leqslant c\leqslant M) contains one integer, representing the minimum cost required to shorten the rope to length 22 under the condition that the final rope contains color cc.

5 3
1 2 3 3 2
2
1
1
7 3
1 2 2 1 3 3 3
2
2
2
10 3
2 2 1 1 3 3 2 1 1 2
3
3
4

Hint

Sample Explanation 1

With the following steps, you can make the final rope contain color 11 with total cost 22.

  • Dye the 22-nd piece from the left into color 11. Fold the rope so that the endpoint that was originally at distance 11 from the left endpoint becomes the new left endpoint. Now, from left to right, the colors are 1,1, 3,3, 3,3, 22, and the thicknesses are 2,2, 1,1, 1,1, 11.
  • Dye the 44-th piece from the left into color 11. Fold the rope so that the endpoint that was originally at distance 22 from the left endpoint becomes the new left endpoint. Now, from left to right, the colors are 3,13, 1, and the thicknesses are 2,32, 3.

With the following steps, you can make the final rope contain colors 22 and 33 with total cost 11.

  • Fold the rope so that the endpoint that was originally at distance 33 from the left endpoint becomes the new left endpoint. Now, from left to right, the colors are 3,3, 2,2, 11, and the thicknesses are 2,2, 2,2, 11.
  • Dye the 33-rd piece from the left into color 22. Fold the rope so that the endpoint that was originally at distance 22 from the left endpoint becomes the new left endpoint. Now, from left to right, the colors are 2,32, 3, and the thicknesses are 3,23, 2.

Sample Explanation 2

With the following steps, you can make the final rope contain color 11 with total cost 22.

  • Fold the rope so that the endpoint that was originally at distance 22 from the left endpoint becomes the new left endpoint.
  • Dye the 11-st piece from the left into color 11. Fold the rope so that the endpoint that was originally at distance 11 from the left endpoint becomes the new left endpoint. Note that the cost of this dyeing is 22, because at this time the thickness of the 11-st piece from the left is 22.
  • Fold the rope so that the endpoint that was originally at distance 33 from the left endpoint becomes the new left endpoint.
  • Fold the rope so that the endpoint that was originally at distance 11 from the left endpoint becomes the new left endpoint.

Constraints and Hints

For 15%15\% of the testdata, N15,M10N\leqslant 15, M\leqslant 10.
For another 30%30\% of the testdata, N105,M10N\leqslant 10^5, M\leqslant 10.
For another 10%10\% of the testdata, N105,M500N\leqslant 10^5, M\leqslant 500.
For another 20%20\% of the testdata, N105,M5000N\leqslant 10^5, M\leqslant 5000.
For all testdata, $2\leqslant N\leqslant 10^6, 1\leqslant M\leqslant N, 1\leqslant C_i\leqslant M(1\leqslant i\leqslant N)$, and in the initial rope, each color 1,2,,M1, 2, \ldots, M appears at least once.

Translated by ChatGPT 5