#P17193. [KOI 2026 #2] 搭骰子塔

[KOI 2026 #2] 搭骰子塔

Problem Description

Sanghun has NN standard six-faced dice. Call them the 11st die, the 22nd die, …\ldots, the NNth die, in order.

Each die has an integer from 11 to 66 written on each of its six faces. On the same die, any two different faces have different numbers, and the sum of the numbers on any two opposite faces is always 77.

Sanghun wants to use these NN dice to build dice towers. The process is as follows:

  1. Throw all NN dice onto the ground.
  2. Record the number on the top face of each die. In the order from the 11st die to the NNth die, denote these top-face numbers by A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N.
  3. Choose at least one die from those still on the ground and build a new tower on the table. You may pick up dice from the ground, but you may not change their orientation. You may choose any ordering of the selected dice, and you must stack all of them vertically into a single column. At this time, the numbers written on any two faces that touch must be the same.
  4. Repeat step 3 until no dice remain on the ground.

For example, the process can be as follows:

  1. Sanghun throws 44 dice on the ground, and the top-face numbers of the 11st through 44th dice are 3,3,5,43, 3, 5, 4, respectively.
  2. Sanghun selects the 11st, 22nd, and 44th dice from the ground, and builds a tower in the order (from bottom to top) of the 11st, 44th, and 22nd dice. The top face of the 11st die and the bottom face of the 44th die both have 33, so these faces can touch. Also, the top face of the 44th die and the bottom face of the 22nd die both have 44, so these faces can touch as well.
  3. Sanghun selects the remaining 33rd die on the ground and builds a tower consisting of only one die.
  4. No dice remain on the ground, so the process ends. In total, Sanghun built 22 towers.

Sanghun wants to decide how to build the towers so that the final number of towers is minimized. Given the numbers on the top faces after throwing the NN dice, find the minimum possible number of towers in the end.

Input Format

The first line contains an integer NN.

The second line contains NN integers A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, separated by spaces.

Output Format

Print the minimum possible number of towers that can be built when Sanghun chooses an optimal way to build them.

4
3 3 5 4
2
2
3 4
1
5
1 1 6 1 1
3

Hint

Constraints

  • All given numbers are integers.
  • 2≤N≤200 0002 \le N \le 200\,000
  • For each integer ii (1≤i≤N1 \le i \le N), 1≤Ai≤61 \le A_i \le 6

Subtasks

  1. (88 points) N=2N = 2.
  2. (2828 points) The number on the top face is 33 or 44. That is, for each integer ii (1≤i≤N1 \le i \le N), Ai=3A_i = 3 or Ai=4A_i = 4.
  3. (3131 points) For any two distinct integers x,yx, y (1≤x,y≤61 \le x, y \le 6), the number of dice whose top face is xx is different from the number of dice whose top face is yy.
  4. (3333 points) No additional constraints.

Translated by ChatGPT-5.6.

Translated by ChatGPT 5