Problem:


Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

2 \leq N \leq 2\times 10^5

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295



Sample input:
6
1 2 3 4 5 6


Sample output:
12

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
a= list(map(int, input().split()))

# aを偶数奇数の要素で振り分け
a1=[]
a2=[]

for i in range(n):
    if i%2==0:
        a1.append(a[i])
    else:
        a2.append(a[i])

# 累積和も作っておく
a3=[0]+list(itertools.accumulate(a1))
a4=[0]+list(itertools.accumulate(a2))

x1=sum(a1)
x2=sum(a2)


if n%2==0:
    print(max(x1,x2))

else:
    # 奇数番目だけの要素を選んだ時の最大値
    ans1=sum(a1)-min(a1)
    # 偶数番目だけを選んだ時
    ans2=sum(a2)
    # 奇数から偶数に一回だけ動く時
    ans3=-float('inf')
    for i in range(n//2-1):
        ans3=max(x2-a4[i+1]+a3[i+1],ans3)
    # 偶数から奇数へ1回だけ動く時
    ans4=-float('inf')
    for i in range(n//2-1):
        ans4=max(x1-a3[i+2]+a4[i+1],ans4)
    ans=max(ans1,ans2,ans3,ans4)
    if n<=5:
        print(ans)
    else:
        # 奇数→偶数→奇数のとき
        # dp1[i]=奇数偶数奇数と移動した時のi番目の要素でのMAX
        dp1 = [-float('inf')] * (n // 2 + 1)
        # dp2[i]=奇数偶数と移動した時のi番目の要素でのMAX
        dp2 = [-float('inf')] * (n // 2)

        for i in range(n // 2-1):
            dp2[i+1] = max(a3[i+1]+a2[i+1],dp2[i]+a2[i+1])
            if i>=1 and i<=n//2-1:
                dp1[i+2] = max(dp2[i]+a1[i+2],dp1[i+1]+a1[i+2])
        print(max(dp1[-1],dp2[-1],ans))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
a= list(map(int, input().split()))

# aを偶数奇数の要素で振り分け
a1=[]
a2=[]

for i in range(n):
    if i%2==0:
        a1.append(a[i])
    else:
        a2.append(a[i])

# 累積和も作っておく
a3=[0]+list(itertools.accumulate(a1))
a4=[0]+list(itertools.accumulate(a2))

x1=sum(a1)
x2=sum(a2)


if n%2==0:
    print(max(x1,x2))

else:
    # 奇数番目だけの要素を選んだ時の最大値
    ans1=sum(a1)-min(a1)
    # 偶数番目だけを選んだ時
    ans2=sum(a2)
    # 奇数から偶数に一回だけ動く時
    ans3=-float('inf')
    for i in range(n//2-1):
        ans3=max(x2-a4[i+1]+a3[i+1],ans3)
    # 偶数から奇数へ1回だけ動く時
    ans4=-float('inf')
    for i in range(n//2-1):
        ans4=max(x1-a3[i+1]+a4[i+1],ans4)
    ans=max(ans1,ans2,ans3,ans4)
    if n<=5:
        print(ans)
    else:
        # 奇数→偶数→奇数のとき
        # dp1[i]=奇数偶数奇数と移動した時のi番目の要素でのMAX
        dp1 = [-float('inf')] * (n // 2 + 1)
        # dp2[i]=奇数偶数と移動した時のi番目の要素でのMAX
        dp2 = [-float('inf')] * (n // 2)

        for i in range(n // 2-1):
            dp2[i+1] = max(a3[i+1]+a2[i+1],dp2[i]+a2[i+1])
            if i>=1 and i<=n//2-1:
                dp1[i+2] = max(dp2[i]+a1[i+2],dp1[i+1]+a1[i+2])
        print(max(dp1[-1],dp2[-1],ans))

Submission status: Wrong Answer

Solution code:
import itertools
n=int(input())
a= list(map(int, input().split()))

# aを偶数奇数の要素で振り分け
a1=[]
a2=[]

for i in range(n):
    if i%2==0:
        a1.append(a[i])
    else:
        a2.append(a[i])

# 累積和も作っておく
a3=[0]+list(itertools.accumulate(a1))
a4=[0]+list(itertools.accumulate(a2))

x1=sum(a1)
x2=sum(a2)


if n%2==0:
    print(max(x1,x2))

else:
    # 奇数番目だけの要素を選んだ時の最大値
    ans1=sum(a1)-min(a1)
    # 偶数番目だけを選んだ時
    ans2=sum(a2)
    # 奇数から偶数に一回だけ動く時
    ans3=0
    for i in range(n//2-1):
        ans3=max(x2-a4[i+1]+a3[i+1],ans3)
    # 偶数から奇数へ1回だけ動く時
    ans4=0
    for i in range(n//2-1):
        ans4=max(x1-a3[i+1]+a4[i+1],ans4)
    ans=max(ans1,ans2,ans3,ans4)
    if n<=5:
        print(ans)
    else:
        # 奇数→偶数→奇数のとき
        # dp1[i]=奇数偶数奇数と移動した時のi番目の要素でのMAX
        dp1 = [-float('inf')] * (n // 2 + 1)
        # dp2[i]=奇数偶数と移動した時のi番目の要素でのMAX
        dp2 = [-float('inf')] * (n // 2)

        for i in range(n // 2-1):
            dp2[i+1] = max(a3[i+1]+a2[i+1],dp2[i]+a2[i+1])
            if i>=1 and i<=n//2-1:
                dp1[i+2] = max(dp2[i]+a1[i+2],dp1[i+1]+a1[i+2])
        print(max(dp1[-1],dp2[-1],ans))

Submission status: Wrong Answer

Solution code:

#f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")
N = int(input())
A = list(map(int, input().split()))
A.insert(0, 0)

select_num = N//2
inf = -10e+10

Dp_f = [[inf] * (3) for _ in range(N+1)]
Dp_s = [[inf] * (3) for _ in range(N+1)]
# 3　次元はそれぞれStart~3つに該当

pre_margin = 0
for i in range(1,N+1):
    Start = (i-1)//2
    End = (i+1)//2
    
    cur_margin = Start
    
    for j in range(Start,End+1):
        Dp_f[i][j-cur_margin] = max(Dp_s[i-1][j-pre_margin], Dp_f[i-1][j-pre_margin])   
        # [0] in 3rd dimension  not chosen at last num
        # must be transfered from [i-1]
    for j in range(Start,End+1):
        if j-1 == 0:
            Dp_s[i][j-cur_margin] = 0 + A[i]
        else:
            Dp_s[i][j-cur_margin] = Dp_f[i-1][j-1-pre_margin]+A[i]
    
    pre_margin = cur_margin
print(max(Dp_f[-1][select_num - cur_margin], Dp_s[-1][select_num - cur_margin]))

Submission status: Wrong Answer

Solution code:

#f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")
N = int(input())
A = list(map(int, input().split()))
A.insert(0, 0)

select_num = N//2
inf = -10e+10

Dp_f = [[inf] * (3) for _ in range(N+1)]
Dp_s = [[inf] * (3) for _ in range(N+1)]
# 3　次元はそれぞれStart~3つに該当

pre_margin = 0
for i in range(1,N+1):
    Start = (i-1)//2
    End = (i+1)//2
    
    cur_margin = Start
    
    for j in range(Start,End+1):
        Dp_f[i][j-cur_margin] = max(Dp_s[i-1][j-pre_margin], Dp_f[i-1][j-pre_margin])   
        # [0] in 3rd dimension  not chosen at last num
        # must be transfered from [i-1]
    for j in range(Start,End+1):
        if Dp_f[i-1][j-1-pre_margin] == inf:
            Dp_s[i][j-cur_margin] = 0 + A[i]
        else:
            Dp_s[i][j-cur_margin] = Dp_f[i-1][j-1-pre_margin]+A[i]
            
    pre_margin = cur_margin
print(max(Dp_f[-1][select_num - cur_margin], Dp_s[-1][select_num - cur_margin]))

Submission status: Wrong Answer

Solution code:

#f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")
N = int(input())
A = list(map(int, input().split()))
A.insert(0, 0)

select_num = N//2
inf = -10e+10

Dp_f = [[inf] * (3) for _ in range(N+1)]
Dp_s = [[inf] * (3) for _ in range(N+1)]
# 3　次元はそれぞれStart~3つに該当

pre_margin = 0
for i in range(1,N+1):
    Start = (i-1)//2
    End = (i+1)//2
    
    cur_margin = Start
    
    for j in range(Start,End+1):
        Dp_f[i][j-cur_margin] = max(Dp_s[i-1][j-pre_margin], Dp_f[i-1][j-pre_margin])   
        # [0] in 3rd dimension  not chosen at last num
        # must be transfered from [i-1]
    for j in range(Start,End+1):
        if Dp_f[i-1][j-1-pre_margin] == inf and j == 1:
            Dp_s[i][j-cur_margin] = 0 + A[i]
        else:
            Dp_s[i][j-cur_margin] = Dp_f[i-1][j-1-pre_margin]+A[i]
            
    pre_margin = cur_margin
print(max(Dp_f[-1][select_num - cur_margin], Dp_s[-1][select_num - cur_margin]))

Submission status: Wrong Answer

Solution code:


N = int(input())
A = list(map(int, input().split()))
A.insert(0, 0)

select_num = N//2


Dp_f = [[-9999] * (5) for _ in range(N+1)]
Dp_s = [[-9999] * (5) for _ in range(N+1)]

pre_margin = 0

for i in range(1,N+1):
    Start = (i-1)//2
    End = (i+1)//2
    
    cur_margin = Start
    
    for j in range(Start,End+1):
        Dp_f[i][j-cur_margin] = max(Dp_s[i-1][j-pre_margin], Dp_f[i-1][j-pre_margin])   
    for j in range(Start,End+1):
        if Dp_f[i-1][j-1-pre_margin] == -9999 and j == 1:
            Dp_s[i][j-cur_margin] = 0 + A[i]
        else:
            Dp_s[i][j-cur_margin] = Dp_f[i-1][j-1-pre_margin]+A[i]
            
    pre_margin = cur_margin
print(max(Dp_f[-1][select_num - cur_margin], Dp_s[-1][select_num - cur_margin]))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int, input().split()))
if N==2 or N==3:
  print(max(A))
  exit() 
  
dp=[[0,0,0] for i in range(N)]
dp[0][0]=A[0]
dp[1][1]=A[1]
dp[2][2]=A[2]
for i in range(N):
  if i>1:
    dp[i][0]=dp[i-2][0]+A[i]
  if i>2:
    dp[i][1]=max(dp[i-3][0],dp[i-2][1])+A[i]
  if i>3 :
    dp[i][2]=max(dp[i-4][0],dp[i-3][1],dp[i-2][2])+A[i]
if N%2==1:
  ans=max(dp[-1][2],dp[-2][1],dp[-3][0])
else:
  ans=max(dp[-1][2],dp[-2][1])
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int, input().split()))
if N%2==0:
  a,b=0,0
  for i in range(N):
    if i%2==0:
      a+=A[i]
    else:
      b+=A[i]
  print(max(a,b))
else:
  dp=[[0,0,0] for i in range(N)]
  dp[0][0]=A[0]
  dp[1][1]=A[1]
  dp[2][2]=A[2]
  for i in range(N):
    if i>1:
      dp[i][0]=dp[i-2][0]+A[i]
    if i>2:
      dp[i][1]=max(dp[i-3][0],dp[i-2][1])+A[i]
    if i>3 :
      dp[i][2]=max(dp[i-4][0],dp[i-3][1],dp[i-2][2])+A[i]
  ans=max(dp[-1][2],dp[-2][1],dp[-3][0])
  print(ans)

Submission status: Wrong Answer

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

import copy
def competition(a):
  n=len(a)
  count1=0
  count2=0
  for i in range(0,n//2):
    count1=count1+a[2*i]
    count2=count2+a[2*i+1]
  if count1>=count2:
    return(count1)
  else:
    return(count2)


if n%2==0:
  print(competition(a))

else:
  result=0
  for j in range(0,n):
    aa=copy.deepcopy(a)
    aa.pop(j)
    comp=competition(aa)
    if comp>=result:
      result=comp
  print(result,comp)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def make4(initial, L, M, N, O):
    return [[[[initial for i in range(O)]
                for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def make3(initial, L, M, N):
    return [[[initial for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    N = I()
    A = LI()
    dp_use = [-IINF] * 5
    dp_not_use = [-IINF] * 5
    dp_not_use[0] = 0

    for a in A:
        tmp_use = [-IINF] * 5
        tmp_not_use = [-IINF] * 5
        for current_dif in range(-3,2):
            if current_dif-1 >= -3:
                tmp_use[current_dif] = dp_not_use[current_dif - 1] + a
            if current_dif+1 < 2:
                tmp_not_use[current_dif] = max(dp_use[current_dif+1], dp_not_use[current_dif+1])
        dp_use = tmp_use
        dp_not_use = tmp_not_use

    dif = N//2 - (N-N//2)
    print(max(dp_use[dif], dp_not_use[dif]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def make4(initial, L, M, N, O):
    return [[[[initial for i in range(O)]
                for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def make3(initial, L, M, N):
    return [[[initial for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    N = I()
    A = LI()
    dp_use = [-IINF] * 5
    dp_not_use = [-IINF] * 5
    dp_not_use[0] = 0

    for a in A:
        dp_use_next = [-IINF] * 5
        dp_not_use_next = [-IINF] * 5
        for current_dif in range(-3,2):
            if current_dif-1 >= -3:
                dp_use_next[current_dif] = dp_not_use[current_dif - 1] + a
            if current_dif+1 < 2:
                dp_not_use_next[current_dif] = max(dp_use[current_dif+1], dp_not_use[current_dif+1])
        dp_use = dp_use_next
        dp_not_use = dp_not_use_next

    dif = N//2 - (N-N//2)
    print(max(dp_use[dif], dp_not_use[dif]))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def make4(initial, L, M, N, O):
    return [[[[initial for i in range(O)]
                for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def make3(initial, L, M, N):
    return [[[initial for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    N = I()
    A = LI()
    dp_use = [-IINF] * 5
    dp_not_use = [-IINF] * 5
    dp_not_use[0] = 0

    for k in range(N):
        dp_use_next = [-IINF] * 5
        dp_not_use_next = [-IINF] * 5
        for current_dif in range(-3,2):
            if current_dif-1 >= -3:
                dp_use_next[current_dif] = dp_not_use[current_dif - 1] + A[k]
            if current_dif+1 < 2:
                dp_not_use_next[current_dif] = max(dp_use[current_dif+1], dp_not_use[current_dif+1])
        dp_use = dp_use_next
        dp_not_use = dp_not_use_next

    dif = N//2 - (N-N//2)
    print(max(dp_use[dif], dp_not_use[dif]))

    # print('===============================')
    # dp = [[-IINF] * (N//2+1) for i in range(N)]
    # dp[0][1] = A[0]

    # for i in range(N):
    #     for j in range(1, N//2+1):
    #         if i - 4 >= 0 and j - 1 > 0:
    #             dp[i][j] = max(dp[i][j], A[i] + max(dp[i-4][j-1], dp[i-3][j-1], dp[i-2][j-1]))
    #         if i - 3 >= 0 and j - 1 > 0:
    #             dp[i][j] = max(dp[i][j], A[i] + max(dp[i-3][j-1], dp[i-2][j-1]))
    #         if i - 2 >= 0 and j - 1 > 0:
    #             dp[i][j] = max(dp[i][j], A[i] + dp[i-2][j-1])
    #             # print(dp[i])
    #             # print(i, j, A[i])
    #             # return
    #         if i <= 2 and j == 1:
    #             dp[i][j] = A[i]
    #     print(dp[i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9+7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
if n == 3:
    print(max(a))
    quit()
dp1 = [-inf]*(n+1) 
dp2 = [-inf]*(n+1)
dp3 = [-inf]*(n+1)
dp1[0] = 0
dp2[0] = 0
dp3[0] = 0
dp1[1] = a[0]
dp2[2] = a[1]
for i in range(3,n+1):
    dp1[i] = dp1[i-2] + a[i-1]
    dp2[i] = max(dp2[i-2],dp1[i-3]) + a[i-1]
    dp3[i] = max(dp3[i-2],dp2[i-3],dp1[i-4]) + a[i-1]
if n % 2:
    print(max(dp1[n-2], dp2[n], dp3[n]))
else:
    print(max(dp1[n-1], dp2[n], dp3[n]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9+7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
if n == 3:
    print(max(a))
    quit()
dp1 = [-inf]*(n+1) 
dp2 = [-inf]*(n+1)
dp3 = [-inf]*(n+1)
dp1[0] = 0
dp2[0] = 0
dp3[0] = 0
dp1[1] = a[0]
dp2[2] = a[1]
for i in range(3,n+1):
    dp1[i] = dp1[i-2] + a[i-1]
    dp2[i] = max(dp2[i-2],dp1[i-3]) + a[i-1]
    dp3[i] = max(dp3[i-2],dp2[i-3],dp1[i-4]) + a[i-1]
if n % 2:
    print(max(dp1[n-2], dp2[n]))
else:
    print(max(dp1[n-1],dp2[n]))

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def make4(initial, L, M, N, O):
    return [[[[initial for i in range(O)]
                for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def make3(initial, L, M, N):
    return [[[initial for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    N = I()
    A = LI()
    dp_use = [-IINF] * 5
    dp_not_use = [-IINF] * 5
    dp_not_use[0] = 0

    for k in range(N):
        dp_use_next = [-IINF] * 5
        dp_not_use_next = [-IINF] * 5
        for j in range(-3, 2):
            if j-1 >= -3:
                dp_use_next[j] = dp_not_use[j-1] + A[k]
            if j+1 < 2:
                dp_not_use_next[j] = max(dp_use[j+1], dp_not_use[j+1])
        dp_use = dp_use_next
        dp_not_use = dp_not_use_next

    dif = N//2 - (N-N//2)
    print(max(dp_use[dif], dp_not_use[dif]))

    # print('===============================')
    # dp = [[-IINF] * (N//2+1) for i in range(N)]
    # dp[0][1] = A[0]

    # for i in range(N):
    #     for j in range(1, N//2+1):
    #         if i - 4 >= 0 and j - 1 > 0:
    #             dp[i][j] = max(dp[i][j], A[i] + max(dp[i-4][j-1], dp[i-3][j-1], dp[i-2][j-1]))
    #         if i - 3 >= 0 and j - 1 > 0:
    #             dp[i][j] = max(dp[i][j], A[i] + max(dp[i-3][j-1], dp[i-2][j-1]))
    #         if i - 2 >= 0 and j - 1 > 0:
    #             dp[i][j] = max(dp[i][j], A[i] + dp[i-2][j-1])
    #             # print(dp[i])
    #             # print(i, j, A[i])
    #             # return
    #         if i <= 2 and j == 1:
    #             dp[i][j] = A[i]
    #     print(dp[i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9+7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
if n == 3:
    print(max(a))
    quit()
dp1 = [-inf]*(n+1) 
dp2 = [-inf]*(n+1)
dp3 = [-inf]*(n+1)
dp1[0] = 0
dp2[0] = 0
dp3[0] = 0
dp1[1] = a[0]
dp2[2] = a[1]
for i in range(3,n+1):
    dp1[i] = dp1[i-2] + a[i-1]
    dp2[i] = max(dp2[i-2],dp1[i-3]) + a[i-1]
    dp3[i] = max(dp3[i-2],dp2[i-3],dp1[i-4]) + a[i-1]
if n % 2:
    print(max(dp1[n-2], dp2[n], dp3[n]))
else:
    print(max(dp1[n],dp2[n]))

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def make4(initial, L, M, N, O):
    return [[[[initial for i in range(O)]
                for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def make3(initial, L, M, N):
    return [[[initial for n in range(N)]
                for m in range(M)]
                for l in range(L)]


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    N = I()
    A = LI()
    dp_use = [-IINF] * 5
    dp_not_use = [-IINF] * 5
    dp_not_use[0] = 0

    for k in range(N):
        dp_use_next = [-IINF] * 5
        dp_not_use_next = [-IINF] * 5
        for j in range(-3, 2):
            if j > -3:
                dp_use_next[j] = dp_not_use[j-1] + A[k]
            if j < 1:
                dp_not_use_next[j] = max(dp_use[j+1], dp_not_use[j+1])
        dp_use = dp_use_next
        dp_not_use = dp_not_use_next

    dif = N//2 - (N-N//2)
    print(max(dp_use[dif], dp_not_use[dif]))

    # print('===============================')
    # dp = [[-IINF] * (N//2+1) for i in range(N)]
    # dp[0][1] = A[0]

    # for i in range(N):
    #     for j in range(1, N//2+1):
    #         if i - 4 >= 0 and j - 1 > 0:
    #             dp[i][j] = max(dp[i][j], A[i] + max(dp[i-4][j-1], dp[i-3][j-1], dp[i-2][j-1]))
    #         if i - 3 >= 0 and j - 1 > 0:
    #             dp[i][j] = max(dp[i][j], A[i] + max(dp[i-3][j-1], dp[i-2][j-1]))
    #         if i - 2 >= 0 and j - 1 > 0:
    #             dp[i][j] = max(dp[i][j], A[i] + dp[i-2][j-1])
    #             # print(dp[i])
    #             # print(i, j, A[i])
    #             # return
    #         if i <= 2 and j == 1:
    #             dp[i][j] = A[i]
    #     print(dp[i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import*
n,*a=map(int,open(0).read().split())
dp=[defaultdict(lambda:-10**12)for _ in range(n)]
for i in range(n):dp[i][0]=0
for i,a in enumerate(a):
    for j in range(n//2-(n-i)//2,i//2+2):
        dp[i][j]=max(dp[i-2][j],dp[i-2][j-1]+a,dp[i-1][j])
print(dp[-1][n//2])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9+7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
if n == 3:
    print(max(a))
    quit()
dp1 = [-inf]*(n+1) 
dp2 = [-inf]*(n+1)
dp3 = [-inf]*(n+1)
dp1[0] = 0
dp2[0] = 0
dp3[0] = 0
dp1[1] = a[0]
dp2[2] = a[1]
for i in range(3,n+1):
    dp1[i] = dp1[i-2] + a[i-1]
    dp2[i] = max(dp2[i-2],dp1[i-3]) + a[i-1]
    dp3[i] = max(dp3[i-2],dp2[i-3],dp1[i-4]) + a[i-1]
print(max(dp1[n],dp2[n],dp3[n]))

Submission status: Wrong Answer

Solution code:



def resolve():
    N = int(input())
    A= list(map(int,input().split()))
    if N % 2 ==0:
        x1 =sum(A[::2])
        x2 =sum(A[1::2])
        print(max(x1,x2))
        return

    inf = -float('inf')
    table = [dict() for i in range(N)]

    table[0][0] = [0,inf]

    table[0][1] =[inf,A[0]]

    for i in range(1,N):
        v=A[i]
        i1 = (i-1) // 2
        i2 = (i+1)//2

        if i % 2 ==1:
            i1 = (i-1) // 2
            i2 = (i+1)//2
            
            table[i][i1] = max(table[i-1][i1])
            table[i][i2] = [max(table[i-1][i2]),table[i-1][i1][0]+v]

        else:
            
            i1 = i // 2
            i0 = i1 - 1
            i2 = (i+2)//2
            
            table[i][i1] = [max(table[i-1][i1]),table[i-1][i0]+v]
            table[i][i2] = [inf,table[i-1][i1][0]+v]


    print(max(table[-1][N//2]))





if __name__ == "__main__":
    resolve()
    #unittest.main()

Submission status: Wrong Answer

Solution code:



def resolve():
    N = int(input())
    A= list(map(int,input().split()))
    if N % 2 ==0:
        x1 =sum(A[::2])
        x2 =sum(A[1::2])
        print(max(x1,x2))
        return

    inf = -float('inf')
    table = [dict() for i in range(N)]

    table[0][0] = [0,inf]

    table[0][1] =[inf,A[0]]
    print(table[0])
    for i in range(1,N):
        v=A[i]
        i1 = (i-1) // 2
        i2 = (i+1)//2
        print(i1,i2)
        if i % 2 ==1:
            i1 = (i-1) // 2
            i2 = (i+1)//2
            
            table[i][i1] = max(table[i-1][i1])
            table[i][i2] = [max(table[i-1][i2]),table[i-1][i1][0]+v]

        else:
            
            i1 = i // 2
            i0 = i1 - 1
            i2 = (i+2)//2
            
            table[i][i1] = [max(table[i-1][i1]),table[i-1][i0]+v]
            table[i][i2] = [inf,table[i-1][i1][0]+v]


    print(max(table[-1][N//2]))





if __name__ == "__main__":
    resolve()
    #unittest.main()

Submission status: Wrong Answer

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

dp = [[0]*2 for _ in range(n+1)]
for i in range(1,n+1):
    if i%2 == 0:
        dp[i][0] = max(dp[i-1][0],dp[i-2][0]+a[i-1])
        dp[i][1] = max(dp[i-1][1],dp[i-2][1]+a[i-1])
    else:
        dp[i][0] = max(dp[i-1][1],dp[i-2][0]+a[i-1])
        dp[i][1] = dp[i-2][1]+a[i-1]
        
if n%2 == 0:
    print(dp[n][1])
else:
    print(dp[n][0])

Submission status: Wrong Answer

Solution code:
d=[0,0]
p=d[:]
for a in[*open(0)][1].split():
  a=int(a)
  d+=[t:=max(a+d[-2],(d,p)[len(d)&1][-1])]
  p+=a+p[-2],
print(t)