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 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.<p>A strange feature of the Knights is that they will not complain about not getting dessert unless they get support from both their neighbours.  So, you decide to pick the cheapest desserts to make, such that for every pair of adjacent Knights, at least one gets his dessert.</p>

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.

- Subtask 1 (30 marks)
- Subtask 2 (70 marks)

-----Sample Input-----
5
1 2 1 2 2

-----Sample Output-----
4

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '# cook your dish here\nimport sys\ninput=sys.stdin.readline\nn=int(input())\nl=input().split()\nli=[int(i) for i in l]\nif(n==1):\n    print(0)\n    return\nif(n==2):\n    print(min(li))\n    return\nif(n==3):\n    li.sort()\n    print(li[0]+li[1])\n    return\nz=list(li[1:])\nans=li[0]\nk=len(z)\ndp=[[0 for i in range(2)]for i in range(k)]\ndp[0][1]=z[0]\ndp[1][0]=z[0]\ndp[1][1]=z[1]\nfor i in range(2,k):\n    dp[i][0]=z[i-1]+min(dp[i-2][0],dp[i-2][1])\n    dp[i][1]=z[i]+min(dp[i-1][0],dp[i-1][1])\nans=ans+min(dp[-1][0],dp[-1][1])\nz=list(li[1:-1])\nok=li[1]+li[-1]\nk=len(z)\nif(k>=2):\n    dp=[[0 for i in range(2)]for i in range(k)]\n    dp[0][1]=z[0]\n    dp[1][0]=z[0]\n    dp[1][1]=z[1]\n    for i in range(2,k):\n        dp[i][0]=z[i-1]+min(dp[i-2][0],dp[i-2][1])\n        dp[i][1]=z[i]+min(dp[i-1][0],dp[i-1][1])\n    ok=ok+min(dp[-1][0],dp[-1][1])\nprint(min(ans,ok))\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: