시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 512 MB 18 2 2 25.000%

문제

In an alternate reality Earth 616 young Stjepan lives a totally different life. Currently he is enrolled in a brick-crafting course at School of Arts and Design. As every child there, he is obsessed with patterns. For example, his homework requires him to build a brick wall using $N$ bricks. But he will not start building until he is satisfied with his two-dimensional sketch.

On the sketch, every brick can be represented as a rectangle with unit size height and width of size $d_i$. He chooses the order of bricks beforehand and starts sketching from the bottom-most row.

In the first row he will place some number of bricks going from left to right. In the second row he will be placing bricks from right to left and the first brick in the second row will align with the last brick in the first row (their right edges will align). Next, in the third row he will be placing the bricks again from left to right. The first brick in the third row will align with the last from the second row but this time the left edges. He continues this process until there are no bricks left. He may choose to build wall with arbitrary number of rows.

Stjepan uses super cement so a brick may be placed in the wall so that there is no other brick directly underneath. Beauty of the wall is a number of places where 4 bricks touch

For a given order of bricks and their respective sizes help find the largest possible beauty of the wall.

입력

First line contains an integer $N$ from the task description.

Second line contains $N$ integers $d_i$ from the task description.

출력

Print the largest possible beauty of any wall that can be built.

제한

Let M be width of the largest brick. 1 ≤ M ≤ 5 000 will hold unless otherwise specified.

서브태스크

번호 배점 제한
1 9

$1 \le N \le 20$

2 11

$1 \le N \le 80$

3 13

$1 \le N \le 500, 1 \le M \le 2$

4 15

$1 \le N \le 500$

5 52

$1 \le N \le 5~000$

예제 입력 1

6
2 2 2 1 1 2

예제 출력 1

2

예제 입력 2

13
9 5 2 8 8 2 5 9 9 7 8 5 10

예제 출력 2

5

예제 입력 3

12
5 5 2 3 2 1 1 5 5 2 5 1

예제 출력 3

4

힌트

Wall with beauty 4 for the third example.

채점 및 기타 정보

  • 예제는 채점하지 않는다.