tetris
문제
Johnny plays a very interesting game, very similar to the popular Tetris.
Game field is rectangular, 3 columns wide and 1000 rows high. Tiles fall from the top and have to be arranged keeping the total height of all the tiles minimal.
There are 7 possible tiles, marked with numbers 1 through 7 (see picture below).
[이미지 1]
Johnny can rotate each tile that appears. Rotation are made by angles that are multiples of 90 degrees. The tile then falls downwards until stopped by some tile inserted previously or by the bottom of the game field.
Johnny can choose any horizontal poisition of the tile, keeping in mind that the whole tile remains within the game filed. I.e. tile no. 1 cannot be rotated (because it is to wide for the game field), but can be inserted in any of the three columns. If Johnny decides to rotate tile no. 5 then it can be inserted in two ways: into columns 1 and 2 or into columns 2 and 3.
After the tile is inserted into the game field and starts to decend, additional moves or rotations are not allowed. Unlike the original tetris, filled rows are not removed from the field.
A sequence of tiles to be inserted into the field is given.
Write a program that will calculate the minimal possible height of all the tiles inserted. In other words, find the least integer K such that, by arranging the tiles in a specific manner, they can all fit in the bottom K rows of the game field.
입력
In first line, there is an integer N, 1 ≤ N ≤ 100, number of tiles in the game.
In each of the next N lines, there is a number which denote mark label of that tail, from the first to the last tail.
출력
In first and only line you should write the minimal possible height as described in the text.
예제
예제 1
3 4 5 1
5
예제 2
4 5 3 1 2
6
예제 3
5 1 6 3 4 2
8