URAL 1152. False Mirrors

1. 题目

http://acm.timus.ru/problem.aspx?space=1&num=1152

1152. False Mirrors

Time limit: 2.0 second
Memory limit: 64 MB

Background

We wandered in the labyrinth for twenty minutes before finally entering the large hall. The walls were covered by mirrors here as well. Under the ceiling hung small balconies where monsters stood. I had never seen this kind before. They had big bulging eyes, long hands firmly holding riffles and scaly, human-like bodies. The guards fired at me from the balconies, I shot back using my BFG-9000. The shot shattered three mirrors filling the room with silvery smoke. Bullets drummed against my body-armor knocking me down to the floor. Falling down I let go a shot, and got up as fast as I fell down by rotating on my back, like I did in my youth while break dancing, all this while shooting three more times. Three mirrors, three mirrors, three mirrors…
Sergey Lukjanenko, “The Labyrinth of Reflections”

Problem

BFG-9000 destroys three adjacent balconies per one shoot. (N-th balcony is adjacent to the first one). After the shoot the survival monsters inflict damage to Leonid (main hero of the novel) — one unit per monster. Further follows new shoot and so on until all monsters will perish. It is required to define the minimum amount of damage, which can take Leonid.

Input

The first line contains integer N, аmount of balconies, on which monsters have taken a circular defense. 3 ≤ N ≤ 20. The second line contains N integers, amount of monsters on each balcony (not less than 1 and no more than 100 on each).

Output

Output minimum amount of damage.

Sample

input output
Problem Author: Eugene Bryzgalov
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round

2. 思路

N个台子首尾相连构成一个环,每个台子上有一些怪,使用BFG-9000射击一个台子,可以摧毁该台子和其左右相邻的台子。每回合都遵循“射击台子->剩余台子上的所有怪物攻击”这样的顺序,每只怪每回合造成1点伤害,求摧毁所有台子所受到的最小伤害。

最多只有20个台子,DFS暴力求解。也可以DP。

3. 代码