Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))

a_cs = [0] * (n + 1)
a_cs[-2] = a[-1]
for i in reversed(range(n - 1)):
    a_cs[i] = a[i] + a_cs[i + 2]
# print(a_cs)


def func(i):
    if i == n:
        return 0
    if i == n - 1:
        return a[-1]

    m = n - i
    if m % 2 == 0:
        ans0 = a[i] + func(i + 2)
        ans1 = a_cs[i + 1]
        ans = max(ans0, ans1)
    else:
        ans0 = a[i + 0] + func(i + 2)
        ans1 = a[i + 1] + func(i + 3)
        ans2 = a_cs[i + 2]
        ans = max(ans0, ans1, ans2)

    return ans


ans = func(0)

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))

a_cs = [0] * (n + 1)
a_cs[-2] = a[-1]
for i in reversed(range(n - 1)):
    a_cs[i] = a[i] + a_cs[i + 2]
# print(a_cs)

d = {}


def func(i):
    if i == n:
        return 0
    if i == n - 1:
        return a[-1]
    if i in d:
        return d[i]

    m = n - i
    if m % 2 == 0:
        ans0 = a[i] + func(i + 2)
        ans1 = a_cs[i + 1]
        ans = max(ans0, ans1)
    else:
        ans0 = a[i + 0] + func(i + 2)
        ans1 = a[i + 1] + func(i + 3)
        ans2 = a_cs[i + 2]
        ans = max(ans0, ans1, ans2)

    d[i] = ans
    return ans


ans = func(0)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))

d = {}


def func(b, m):
    if m == 0:
        return 0
    if m == 1:
        return b[0]
    if m == 2:
        return max(b[0], b[1])
    if m in d:
        return d[m]

    if m % 2 == 0:
        ans0 = b[0] + func(b[2:], m - 2)
        ans1 = sum(b[i] for i in range(1, m, 2))
        ans = max(ans0, ans1)
    else:
        ans0 = b[0] + func(b[2:], m - 2)
        ans1 = b[1] + func(b[3:], m - 3)
        ans2 = sum(b[i] for i in range(2, m, 2))
        ans = max(ans0, ans1, ans2)

    d[m] = ans
    return ans


ans = func(a, n)

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))

a_cs = [0] * (n + 1)
a_cs[-2] = a[-1]
for i in reversed(range(n - 1)):
    a_cs[i] = a[i] + a_cs[i + 2]
# print(a_cs)

d = {}


def func(i):
    if i == n:
        return 0
    if i == n - 1:
        return a[-1]
    if i in d:
        return d[i]

    m = n - i
    if m % 2 == 0:
        ans0 = a[i] + func(i + 2)
        ans1 = a_cs[i + 1]
        ans = max(ans0, ans1)
    else:
        ans0 = a[i + 0] + func(i + 2)
        ans1 = a[i + 1] + func(i + 3)
        ans2 = a_cs[i + 2]
        ans = max(ans0, ans1, ans2)

    d[i] = ans
    return ans


ans = func(0)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))


def func(b, m):
    if m == 0:
        return 0
    if m == 1:
        return b[0]

    if m % 2 == 0:
        ans0 = b[0] + func(b[2:], m - 2)
        ans1 = sum(b[i] for i in range(1, m, 2))
        ans = max(ans0, ans1)
    else:
        ans0 = b[0] + func(b[2:], m - 2)
        ans1 = b[1] + func(b[3:], m - 3)
        ans2 = sum(b[i] for i in range(2, m, 2))
        ans = max(ans0, ans1, ans2)

    return ans


ans = func(a, n)

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))

a_cs = [0] * (n + 1)
a_cs[-2] = a[-1]
for i in reversed(range(n - 1)):
    a_cs[i] = a[i] + a_cs[i + 2]
# print(a_cs)

d = {}


def func(i):
    if i == n:
        return 0
    if i == n - 1:
        return a[-1]
    if i in d:
        return d[i]

    m = n - i
    if m % 2 == 0:
        ans0 = a[i] + func(i + 2)
        ans1 = a_cs[i + 1]
        ans = max(ans0, ans1)
    else:
        ans0 = a[i + 0] + func(i + 2)
        ans1 = a[i + 1] + func(i + 3)
        ans2 = a_cs[i + 2]
        ans = max(ans0, ans1, ans2)

    d[i] = ans
    return ans


ans = func(0)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))

d = {}


def func(b, m):
    if m == 0:
        return 0
    if m == 1:
        return b[0]
    if m in d:
        return d[m]

    if m % 2 == 0:
        ans0 = b[0] + func(b[2:], m - 2)
        ans1 = sum(b[i] for i in range(1, m, 2))
        ans = max(ans0, ans1)
    else:
        ans0 = b[0] + func(b[2:], m - 2)
        ans1 = b[1] + func(b[3:], m - 3)
        ans2 = sum(b[i] for i in range(2, m, 2))
        ans = max(ans0, ans1, ans2)

    d[m] = ans
    return ans


ans = func(a, n)

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))

a_cs = [0] * (n + 1)
a_cs[-2] = a[-1]
for i in reversed(range(n - 1)):
    a_cs[i] = a[i] + a_cs[i + 2]
# print(a_cs)

d = {}


def func(i):
    if i == n:
        return 0
    if i == n - 1:
        return a[-1]
    if i in d:
        return d[i]

    m = n - i
    if m % 2 == 0:
        ans0 = a[i] + func(i + 2)
        ans1 = a_cs[i + 1]
        ans = max(ans0, ans1)
    else:
        ans0 = a[i + 0] + func(i + 2)
        ans1 = a[i + 1] + func(i + 3)
        ans2 = a_cs[i + 2]
        ans = max(ans0, ans1, ans2)

    d[i] = ans
    return ans


ans = func(0)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))


def func(b, m):
    if m == 0:
        return 0
    if m == 1:
        return b[0]

    if m % 2 == 0:
        ans0 = b[0] + func(b[2:], m - 2)
        ans1 = sum(b[i] for i in range(1, m, 2))
        ans = max(ans0, ans1)
    else:
        ans0 = b[0] + func(b[2:], m - 2)
        ans1 = b[1] + func(b[3:], m - 3)
        ans2 = sum(b[i] for i in range(2, m, 2))
        ans = max(ans0, ans1, ans2)

    return ans


ans = func(a, n)

print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**8)

n = int(input())
a = list(map(int, input().split()))

a_cs = [0] * (n + 1)
a_cs[-2] = a[-1]
for i in reversed(range(n - 1)):
    a_cs[i] = a[i] + a_cs[i + 2]
# print(a_cs)

d = {}


def func(i):
    if i == n:
        return 0
    if i == n - 1:
        return a[-1]
    if i in d:
        return d[i]

    m = n - i
    if m % 2 == 0:
        ans0 = a[i] + func(i + 2)
        ans1 = a_cs[i + 1]
        ans = max(ans0, ans1)
    else:
        ans0 = a[i + 0] + func(i + 2)
        ans1 = a[i + 1] + func(i + 3)
        ans2 = a_cs[i + 2]
        ans = max(ans0, ans1, ans2)

    d[i] = ans
    return ans


ans = func(0)

print(ans)