#P12330. Cyclic Stone Merging

    ID: 14429 Type: Default 1000ms 256MiB

Cyclic Stone Merging

Cyclic Stone Merging

There are N piles of stones arranged horizontally from left to right on a desk. Each pile is colored with one of three possible colors: \(0\), \(1\), or \(2\). Initially, every pile contains exactly one stone. You are allowed to merge two adjacent piles if and only if they have the same color. When merging two piles, the following rules apply:

  • The resulting pile is placed in the same relative position as the two merged piles.
  • The number of stones in the new pile is the sum of the stones in the two merged piles.
  • The color of the new pile changes cyclically: merging two piles of color \(0\) yields a pile of color \(1\); merging two piles of color \(1\) yields a pile of color \(2\); merging two piles of color \(2\) yields a pile of color \(0\). This can be written in \(\LaTeX\) as \( (c+1)\bmod 3 \) when merging two piles of color \(c\).
  • The cost of each merge is equal to the sum of the number of stones in the two piles that are merged.

Your goal is to merge the piles according to the rules in order to achieve the minimum possible number of piles. If there are several ways to achieve this, choose the one with the smallest total merge cost (i.e. the sum of the costs of every merge operation performed).

Compute and output two numbers: the minimum number of piles remaining after all possible merge operations, and the corresponding minimum total merge cost.

inputFormat

The input consists of two lines:

  1. The first line contains a single integer \(N\) \((1 \leq N \leq 200)\), representing the number of stone piles.
  2. The second line contains \(N\) space‐separated integers. Each integer is either \(0\), \(1\), or \(2\) and represents the color of the corresponding pile. Initially, each pile contains exactly one stone.

outputFormat

Output two space-separated integers on a single line:

  1. The minimum number of piles that can be obtained.
  2. The minimum total merge cost to achieve that number of piles.

sample

3
0 0 0
2 2