Zonal Computing Olympiad 2014, 30 Nov 2013

In IPL 2025, the amount that each player is paid varies from match to match.  The match fee depends on the quality of opposition, the venue etc.

The match fees for each match in the new season have been announced in advance.  Each team has to enforce a mandatory rotation policy so that no player ever plays three matches in a row during the season.

Nikhil is the captain and chooses the team for each match. He wants to allocate a playing schedule for himself to maximize his earnings through match fees during the season.  

-----Input format-----
Line 1: A single integer N, the number of games in the IPL season.
Line 2: N non-negative integers, where the integer in
position i represents the fee for match i.

-----Output format-----
The output consists of a single non-negative integer, the
maximum amount of money that Nikhil can earn during this IPL
season. 

-----Sample Input 1-----
5 
10 3 5 7 3 

-----Sample Output 1-----
23

(Explanation: 10+3+7+3)

-----Sample Input 2-----
8
3 2 3 2 3 5 1 3

-----Sample Output 2-----
17

(Explanation: 3+3+3+5+3)

-----Test data-----
There is only one subtask worth 100 marks.  In all inputs:

• 1 ≤ N ≤ 2×105
• The fee for each match is between 0 and 104, inclusive.

-----Live evaluation data-----
There are 12 test inputs on the server during the exam.

```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 = '\n# def recursive(List, taken_previous, idx, r) : \n#     if idx >= N : return 0\n#     if taken_previous :\n#         # I am not sure if you should go 2 steps even futher to check. Probably extra recursion\n#         return List[idx] + max( recursive(List, False, idx + 2, r + List[idx]), recursive(List, False, idx + 3, r + List[idx]))\n#     else :\n#         return  List[idx] + max( recursive(List, True, idx + 1, r + List[idx]), recursive(List, False, idx + 2, r + List[idx]) )\n    \n# N = int(input())\n# fees = list(map(int, input().split()))\n# dp = [-1] * int(1e6)\n\n# def recursive(nums, n) : \n#     if n < 0  : return 0\n#     elif n == 0 : return nums[0]\n#     elif n == 1 : return nums[0] + nums[1]\n#     else : \n#         if dp[n] != -1 : return dp[n]\n        \n#         dp[n] = max( recursive(nums, n - 1), \n#                      recursive(nums, n - 2) + nums[n],\n#                      recursive(nums, n - 3) + nums[n] + nums[n-1]\n#                     )\n        \n#         return dp[n]\n        \n    \n# if N <= 2 :  print(sum(fees))\n# else :\n#     r = recursive(fees, N - 1)\n#     print(r)\n    \n    \ndp = [-1] * int(1e6)\n\n\ndef solve(a, d):\n\tif d < 0:\n\t\treturn 0\n\tif d == 0:\n\t\treturn a[0]\n\tif d == 1:\n\t\treturn a[0] + a[1]\n\n\tif dp[d] != -1:\n\t\treturn dp[d]\n\n\tdp[d] = max(\n\t\tsolve(a, d - 1),\n\t\tsolve(a, d - 2) + a[d],\n\t\tsolve(a, d - 3) + a[d] + a[d - 1]\n\t)\n\treturn dp[d]\n\n\ndef main():\n\tfrom sys import stdin, stdout, setrecursionlimit\n\tsetrecursionlimit(int(1e6))\n\trl = stdin.readline\n\n\tn = int(rl())\n\ta = [int(x) for x in rl().split()]\n\n\tif n < 3:\n\t\tstdout.write(str(sum(a)))\n\t\treturn\n\n\tstdout.write(str(solve(a, n - 1)))\n\n\nmain()\n    \n\n    \n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 8013935