Round Table

Zonal Computing Olympiad 2012, 26 Nov 2011
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 counterclockwise.
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 ≤ 10^{6}. In 30% of the test cases, 1 ≤ N ≤ 10^{3}.
 Subtask 1 (30 marks)
 Subtask 2 (70 marks)
Sample Input
5 1 2 1 2 2
Sample Output
4
Author:  admin3 
Date Added:  2112015 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, C99 strict, CPP 4.3.2, CPP 4.9.2, CPP14, JAVA, PYTH, PYTH 3.4 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions