Zonal Computing Olympiad 2012, 26 Nov 2011

2:00 pm-5:00 pm IST

Problem 2 : Round Table

It's dinner time in Castle Camelot, and the fearsome Knights of the Round Table are clamouring for dessert. You, the chef, are in a soup. There are N knights, including King Arthur, each with a different preference for dessert, but you cannot afford to make desserts for all of them.

You are given the cost of manufacturing each Knight's preferred dessert–since it is a round table, the list starts with the cost of King Arthur's dessert, and goes counter-clockwise.

You decide to pick the cheapest desserts to make, such that for every pair of adjacent Knights, at least one gets his dessert. This will ensure that the Knights do not protest.

What is the minimum cost of tonight's dinner, given this condition?

For instance, suppose there are 5 Knights and their desserts cost 1, 2, 1, 2 and 2. In this case, the minimum cost is 4, which you can achieve by feeding the first, third and fourth (or fifth) Knights.

Input format

There are 2 lines of input. The first line contains a single integer N, the number of seats at the table. The next line contains N space separated integers, each being the cost of the dessert of a Knight, listed in counterclockwise order around the table, starting with King Arthur.

Output format

The output should be a single line containing a single integer, the minimum possible cost for you, the chef.

Testdata

Each Knight's dessert costs strictly more than 0 and strictly less than 1000. You may assume that 1 ≤ N ≤ 106. In 30% of the test cases, 1 ≤ N ≤ 103.

Sample Input

5
1 2 1 2 2

Sample Output

4

Time and memory limits

The time limit for this task is 1 second. The memory limit is 32MB.




Copyright (c) IARCS 2003-2024;   Last Updated: 23 Sep 2012