Problem:


Score : 600 points

Problem Statement

Snuke stands on a number line. He has L ears, and he will walk along the line continuously under the following conditions:

He never visits a point with coordinate less than 0, or a point with coordinate greater than L.

He starts walking at a point with integer coordinate, and also finishes walking at a point with integer coordinate.

He only changes direction at a point with integer coordinate.

Each time when Snuke passes a point with coordinate i-0.5, where i is an integer, he put a stone in his i-th ear.

After Snuke finishes walking, Ringo will repeat the following operations in some order so that, for each i, Snuke's i-th ear contains A_i stones:

Put a stone in one of Snuke's ears.

Remove a stone from one of Snuke's ears.

Find the minimum number of operations required when Ringo can freely decide how Snuke walks.

Constraints

1 \leq L \leq 2\times 10^5

0 \leq A_i \leq 10^9(1\leq i\leq L)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L
A_1
:
A_L

Output

Print the minimum number of operations required when Ringo can freely decide how Snuke walks.

Sample Input 1

4
1
0
2
3

Sample Output 1

1

Assume that Snuke walks as follows:

He starts walking at coordinate 3 and finishes walking at coordinate 4, visiting coordinates 3,4,3,2,3,4 in this order.

Then, Snuke's four ears will contain 0,0,2,3 stones, respectively.
Ringo can satisfy the requirement by putting one stone in the first ear.

Sample Input 2

8
2
0
0
2
1
3
4
1

Sample Output 2

3

Sample Input 3

7
314159265
358979323
846264338
327950288
419716939
937510582
0

Sample Output 3

1



Sample input:
4
1
0
2
3


Sample output:
1

Submission status: Wrong Answer

Solution code:
import sys
import math
input = sys.stdin.readline
MOD = 10**9 + 7

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

# dp[i][j], i:processed i-th item, j:status
dp = [[n] * (n + 1) for _ in range(5)]
for i in range(5):
    dp[i][0] = 0
for i, item in enumerate(a):
    if item != 0:
        odd_cost = (item % 2 == 1)
        even_cost = (item % 2 == 0)
        # from 0 
        dp[0][i+1] = min(dp[0][i+1], dp[0][i] + item)
        dp[1][i+1] = min(dp[1][i+1], dp[0][i] + even_cost)
        dp[2][i+1] = min(dp[2][i+1], dp[0][i] + odd_cost)
        dp[3][i+1] = min(dp[3][i+1], dp[0][i] + even_cost)
        dp[4][i+1] = min(dp[4][i+1], dp[0][i] + item)
        # from 1
        dp[1][i+1] = min(dp[1][i+1], dp[1][i] + even_cost)
        dp[2][i+1] = min(dp[2][i+1], dp[1][i] + odd_cost)
        dp[3][i+1] = min(dp[3][i+1], dp[1][i] + even_cost)
        dp[4][i+1] = min(dp[4][i+1], dp[1][i] + item)
        # from 2
        dp[2][i+1] = min(dp[2][i+1], dp[2][i] + even_cost)
        dp[3][i+1] = min(dp[3][i+1], dp[2][i] + odd_cost)
        dp[4][i+1] = min(dp[4][i+1], dp[2][i] + item)
        # from 3
        dp[3][i+1] = min(dp[3][i+1], dp[3][i] + odd_cost)
        dp[4][i+1] = min(dp[4][i+1], dp[3][i] + item)
        # from 4
        dp[4][i+1] = min(dp[4][i+1], dp[4][i] + item)
    else:
        # from 0 
        dp[0][i+1] = min(dp[0][i+1], dp[0][i])
        dp[1][i+1] = min(dp[1][i+1], dp[0][i] + 2)
        dp[2][i+1] = min(dp[2][i+1], dp[0][i] + 1)
        dp[3][i+1] = min(dp[3][i+1], dp[0][i] + 2)
        dp[4][i+1] = min(dp[4][i+1], dp[0][i])
        # from 1
        dp[1][i+1] = min(dp[1][i+1], dp[1][i] + 2)
        dp[2][i+1] = min(dp[2][i+1], dp[1][i] + 1)
        dp[3][i+1] = min(dp[3][i+1], dp[1][i] + 2)
        dp[4][i+1] = min(dp[4][i+1], dp[1][i])
        # from 2
        dp[2][i+1] = min(dp[2][i+1], dp[2][i] + 1)
        dp[3][i+1] = min(dp[3][i+1], dp[2][i] + 2)
        dp[4][i+1] = min(dp[4][i+1], dp[2][i])
        # from 3
        dp[3][i+1] = min(dp[3][i+1], dp[3][i] + 2)
        dp[4][i+1] = min(dp[4][i+1], dp[3][i])
        # from 4
        dp[4][i+1] = min(dp[4][i+1], dp[4][i])

ans = n
for line in dp:
    ans = min(ans, line[n])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
input = sys.stdin.readline
MOD = 10**9 + 7

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

# dp[i][j], i:processed i-th item, j:status
dp = [[n] * (n + 1) for _ in range(5)]
for i in range(5):
    dp[i][0] = 0
for i, item in enumerate(a):
    if item != 0:
        odd_cost = (item % 2 == 1)
        even_cost = (item % 2 == 0)
    else:
        odd_cost = 1
        even_cost = 1
    # from 0 
    dp[0][i+1] = min(dp[0][i+1], dp[0][i] + item)
    dp[1][i+1] = min(dp[1][i+1], dp[0][i] + even_cost)
    dp[2][i+1] = min(dp[2][i+1], dp[0][i] + odd_cost)
    dp[3][i+1] = min(dp[3][i+1], dp[0][i] + even_cost)
    dp[4][i+1] = min(dp[4][i+1], dp[0][i] + item)
    # from 1
    dp[1][i+1] = min(dp[1][i+1], dp[1][i] + even_cost)
    dp[2][i+1] = min(dp[2][i+1], dp[1][i] + odd_cost)
    dp[3][i+1] = min(dp[3][i+1], dp[1][i] + even_cost)
    dp[4][i+1] = min(dp[4][i+1], dp[1][i] + item)
    # from 2
    dp[2][i+1] = min(dp[2][i+1], dp[2][i] + even_cost)
    dp[3][i+1] = min(dp[3][i+1], dp[2][i] + odd_cost)
    dp[4][i+1] = min(dp[4][i+1], dp[2][i] + item)
    # from 3
    dp[3][i+1] = min(dp[3][i+1], dp[3][i] + odd_cost)
    dp[4][i+1] = min(dp[4][i+1], dp[3][i] + item)
    # from 4
    dp[4][i+1] = min(dp[4][i+1], dp[4][i] + item)

ans = n
for line in dp:
    ans = min(ans, line[n])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
input = sys.stdin.readline
MOD = 10**9 + 7

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

# dp[i][j], i:processed i-th item, j:status
dp = [[n] * (n + 1) for _ in range(5)]
for i in range(5):
    dp[i][0] = 0
for i, item in enumerate(a):
    if item != 0:
        even_cost = (item % 2 == 0)
        odd_cost = (item % 2 == 1)
    else:
        even_cost = 1
        odd_cost = 1
    # from 0 
    dp[0][i+1] = min(dp[0][i+1], dp[0][i] + item)
    dp[1][i+1] = min(dp[1][i+1], dp[0][i] + odd_cost)
    dp[2][i+1] = min(dp[2][i+1], dp[0][i] + even_cost)
    dp[3][i+1] = min(dp[3][i+1], dp[0][i] + odd_cost)
    dp[4][i+1] = min(dp[4][i+1], dp[0][i] + item)
    # from 1
    dp[1][i+1] = min(dp[1][i+1], dp[1][i] + odd_cost)
    dp[2][i+1] = min(dp[2][i+1], dp[1][i] + even_cost)
    dp[3][i+1] = min(dp[3][i+1], dp[1][i] + odd_cost)
    dp[4][i+1] = min(dp[4][i+1], dp[1][i] + item)
    # from 2
    dp[2][i+1] = min(dp[2][i+1], dp[2][i] + odd_cost)
    dp[3][i+1] = min(dp[3][i+1], dp[2][i] + even_cost)
    dp[4][i+1] = min(dp[4][i+1], dp[2][i] + item)
    # from 3
    dp[3][i+1] = min(dp[3][i+1], dp[3][i] + odd_cost)
    dp[4][i+1] = min(dp[4][i+1], dp[3][i] + item)
    # from 4
    dp[4][i+1] = min(dp[4][i+1], dp[4][i] + item)

ans = n
for line in dp:
    ans = min(ans, line[n])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

"""
0,0,0,...,0,ev,ev,ev,,,ev,od,od,,,od,ev,ev,,,,ev,0,0,0,0,0
"""

L = int(input())
A = (int(x) for x in sys.stdin.read().split())

# どの状態まで進んだかごとに、最小スコア
a,b,c,d,e = 0,0,0,0,0
for x in A:
    ev_score = 1 if x&1 else (0 if x > 0 else 2)
    od_score = 1-(x&1)
    a1 = a + x
    b1 = min(a,b) + ev_score
    c1 = min(a,b,c) + od_score
    d1 = min(c,d) + ev_score
    e1 = min(c,d,e) + x
    a,b,c,d,e = a1,b1,c1,d1,e1

answer = min(a,b,c,d,e)
print(answer)

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [int(input()) for _ in range(L)]
while True:
  if len(A) == 0:
    break
  if A[0] == 0:
    del A[0]
  else:
    break

A_sub_list = []
zero_num = []
flag = 0
for A_i in A:
  if flag == 0:
    if A_i != 0:
      flag = 1
      A_sub_list.append([A_i])
    else:
      zero_num[-1] = zero_num[-1] + 1
  else:
    if A_i == 0:
      flag = 0
      zero_num.append(1)
    else:
      A_sub_list[-1].append(A_i)

def calc_reward(A_sub):
  ans = sum(A_sub)
  for A_i in A_sub:
    if A_i % 2 == 0:
      ans -= 1
  if A_sub[0] % 2 == 0:
    ans += 1
  if A_sub[-1] % 2 == 0:
    ans += 1
  if len(A_sub) == 1 and A_sub[0] % 2 == 0:
    ans -= 1
  return ans

stone = 0
num_sub = 0
for i in range(len(A_sub_list)):
  reward = calc_reward(A_sub_list[i])
  cost = zero_num[i] if i <= len(zero_num) - 1 else 0
  if reward > cost:
    stone += reward - cost
    num_sub += 1
ans = sum(A) - stone if num_sub != 1 else sum(A) - stone - 2
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

L=int(input())
A=[int(input()) for i in range(L)]
            
SUM=[0]
for a in A:
    SUM.append(SUM[-1]+a)

def div2(x):
    if x==0:
        return 2
    else:
        return x%2
    
DP0=[0]*(L+1)
DP1=[0]*(L+1)
DP2=[0]*(L+1)
DP3=[0]*(L+1)


for i in range(L):
    DP0[i]=DP0[i-1]+A[i]
    DP1[i]=min(DP0[i-1]+div2(A[i]),DP1[i-1]+div2(A[i]))
    DP2[i]=min(DP2[i-1]+(A[i]+1)%2,DP1[i-1]+(A[i]+1)%2)
    DP3[i]=min(DP3[i-1]+A[i],DP2[i-1]+A[i])

ANS=min(DP0[L-1],DP1[L-1],DP2[L-1],DP3[L-1])

DP0=[0]*(L+1)
DP1=[0]*(L+1)
DP2=[0]*(L+1)
DP3=[0]*(L+1)


for i in range(L):
    DP0[i]=DP0[i-1]+A[i]
    DP1[i]=min(DP0[i-1]+(A[i]+1)%2,DP1[i-1]+(A[i]+1)%2)
    DP2[i]=min(DP2[i-1]+div2(A[i]),DP1[i-1]+div2(A[i]))
    DP3[i]=min(DP3[i-1]+A[i],DP2[i-1]+A[i])

ANS=min(DP0[L-1],DP1[L-1],DP2[L-1],DP3[L-1],ANS)

print(ANS)

Submission status: Wrong Answer

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

INF = float('inf')

def solve(n: int, A: "List[int]"):
    dp = [[INF] * 5  for _ in range(n + 1)]
    dp[0][0] = 0
    dp[0][1] = 0
    dp[0][2] = 0
    for i in range(n):
        for j in range(5):
            if j == 0 or j == 4:
                extra = A[i]
            elif j == 1 or j == 3:
                extra = A[i] % 2 if A[i] > 1 else 2
            else:
                extra = (A[i] + 1) % 2
            dp[i + 1][j] = min(dp[i][:j + 1]) + extra
            #dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + extra)
            #for k in range(j + 1, 5):
            #    dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + extra)
        #print(dp[i + 1])
    print(min(dp[n]))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    L = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(L) ]  # type: "List[int]"
    solve(L, A)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

INF = float('inf')

def solve(n: int, A: "List[int]"):
    dp = [[INF] * 5  for _ in range(n + 1)]
    dp[0][0] = 0
    dp[0][1] = 0
    dp[0][2] = 0
    for i in range(n):
        for j in range(5):
            if j == 0 or j == 4:
                extra = A[i]
            elif j == 1 or j == 3:
                extra = A[i] % 2 if A[i] > 1 else 2
            else:
                extra = (A[i] + 1) % 2
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + extra)
            for k in range(j + 1, 5):
                dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + extra)
        #print(dp[i + 1])
    print(min(dp[n]))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    L = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(L) ]  # type: "List[int]"
    solve(L, A)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

INF = float('inf')

def solve(n: int, A: "List[int]"):
    dp = [[INF] * 5  for _ in range(n + 1)]
    dp[0][0] = 0
    dp[0][1] = 0
    dp[0][2] = 0
    for i in range(n):
        for j in range(5):
            if j == 0 or j == 4:
                extra = A[i]
            elif j == 1 or j == 3:
                extra = A[i] % 2 if A[i] > 1 else 2
            else:
                extra = (A[i] + 1) % 2
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + extra)
            if j < 4:
                dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + extra)
        #print(dp[i + 1])
    print(min(dp[n]))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    L = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(L) ]  # type: "List[int]"
    solve(L, A)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

L = input()
A = [input() for i in range(L)]

DP = [[0] * 5 for i in range(L)]

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
dp = [[1<<60]*5 for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
        dp[i+1][0] = dp[i][0]+a[i]
        dp[i+1][1] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2)
        if a[i] == 0:
                pass
        else:
                a[i]+=1
        dp[i+1][2] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2,dp[i][2]+a[i]%2)
        if a[i] == 0:
                pass
        else:
                a[i]-=1
        dp[i+1][3] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2,dp[i][2]+a[i]%2,dp[i][3]+a[i]%2)
        dp[i+1][4] = min(dp[i][0],dp[i][1],dp[i][2],dp[i][3],dp[i][4])+a[i]
print(min(dp[n]))

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [0] * L

for i in range(L):
  A[i] = int(input())
  
dp = [[0 for i in range(5)] for j in range(L+1)]

for i in range(1,L+1):
  a = A[i-1]
  dp[i][0] = dp[i-1][0] + a
  dp[i][1] = min(dp[i-1][:2]) +  a % 2 if a > 0 else 2
  dp[i][2] = min(dp[i-1][:3]) + (1 - a%2)
  dp[i][3] = min(dp[i-1][:4]) +  a % 2 if a > 0 else 2
  dp[i][4] = min(dp[i-1][:]) + a
  
print(min(dp[L]))
#print(dp)

Submission status: Wrong Answer

Solution code:
import math


def round(n):
    if n == 0:
        return 0
    if n % 2 == 0:
        return 2
    return 1


def aggregate(arr):
    aggregated = [[-1, 0, 0]]
    for i in arr:
        rounded = round(i)
        if aggregated[-1][0] != rounded:
            aggregated.append([rounded, 1, i])
        else:
            aggregated[-1][1] += 1
            aggregated[-1][2] += i
    return aggregated[1:]


def calc_pass_cost(n):
    if n % 2 == 0:
        return 1
    return 0


def trim_edges_if_zero(aggregated):
    result = aggregated
    if aggregated[0][0] == 0:
        result = result[1:]
    if aggregated[-1][0] == 0:
        result = result[:-1]
    return result


def trim_edge_by_cost(aggregated):
    pass_costs = 0
    ignored_stones = 0
    for i, v in enumerate(aggregated):
        if i > 0:
            pass_costs += calc_pass_cost(v[0]) * v[1]
        ignored_stones += v[2]
        if pass_costs >= ignored_stones:
            return i + 1, ignored_stones
    return 0, 0


def trim_edges(aggregated):
    return aggregated[1:-1]


def calc_cost(aggregated):
    cost = 0
    for v in aggregated:
        cost += calc_pass_cost(v[0]) * v[1]
    return cost


if __name__ == "__main__":
    L = int(input())
    A = [0] * L
    for i in range(L):
        A[i] = int(input())

    agg = trim_edges_if_zero(aggregate(A))
    left_trim_point, left_ignored_stone = trim_edge_by_cost(agg)
    right_trim_point, right_ignored_stone = trim_edge_by_cost(agg[::-1])
    agg_trimmed = trim_edges(agg[left_trim_point:len(agg)-right_trim_point])

    cost = calc_cost(agg_trimmed)
    print(cost + left_ignored_stone + right_ignored_stone)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
def tqdm(iterable): return iterable
def inpl(): return list(map(int, input().split()))
MOD = 998244353

S = input()
N = len(S)
B = [0]*N
R = [0]*N

for i in range(N):
    b = int(S[i])
    B[i] = b
    R[i] = 2 - b

DP = defaultdict(int)
DP[(B[0], R[0])] = 1

for i in tqdm(range(1, N)):
    DP2 = defaultdict(int)
    b1, r1 = B[i], R[i]
    for (b0, r0), v in DP.items():
        b2, r2 = b0+b1, r0+r1
        if b0:
            DP2[(b2-1, r2)] = (DP2[(b2-1, r2)] + v)%MOD
        if r0:
            DP2[(b2, r2-1)] = (DP2[(b2, r2-1)] + v)%MOD
    DP = DP2

for _ in tqdm(range(N+1)):
    DP2 = defaultdict(int)
    for (b, r), v in DP.items():
        if b:
            DP2[(b-1, r)] = (DP2[(b-1, r)] + v)%MOD
        if r:
            DP2[(b, r-1)] = (DP2[(b, r-1)] + v)%MOD
    DP = DP2

print(sum(DP.values()))

Submission status: Wrong Answer

Solution code:
l = int(input())
a = [int(input()) for _ in range(l)]

dp = [[0 for i in range(5)] for j in range(l+1)]

def cost(s, a):
    if s == 0 or s == 4:
        return a
    elif s == 1 or s == 3:
        if a > 0:
            return a%2
        else:
            return 0
    else:
        return (a+1)%2

for i in range(l):
    dp[i+1][0] = min(dp[i][:1]) + cost(0, a[i])
    dp[i+1][1] = min(dp[i][:2]) + cost(1, a[i])
    dp[i+1][2] = min(dp[i][:3]) + cost(2, a[i])
    dp[i+1][3] = min(dp[i][:4]) + cost(3, a[i])
    dp[i+1][4] = min(dp[i][:5]) + cost(4, a[i])

print(min(dp[l]))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**23]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(k+1):
            dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k]-B[i-1]))
ans=10**23
print(B)
print(dp)
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**9]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(k+1):
            dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k]-B[i-1]))
ans=10**9
#print(B)
#print(dp)
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**9]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(k+1):
            dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k1]-B[i-1]))
ans=10**9
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**9]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(max(0,k-1),k+1):
            dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k1]-B[i-1]))
ans=10**9
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(L, A):

    dp = [[sys.maxsize] * (L+1) for _ in range(5)]
    for i in range(5):
        dp[i][0] = 0

    for i, a in enumerate(A):
        dp[0][i+1] = dp[0][i] + a
        dp[1][i+1] = min(dp[1][i], dp[0][i]) + ((a%2) if a != 0 else 2)
        dp[2][i+1] = min(dp[2][i], dp[1][i]) + ((a+1)%2)
        dp[3][i+1] = min(dp[3][i], dp[2][i]) + ((a%2) if a != 0 else 2)
        dp[4][i+1] = min(dp[4][i], dp[3][i]) + a
    

    return min([dp[i][L] for i in range(5)])

def main():
    L = read_int()
    A = [read_int() for _ in range(L)]
    print(slv(L, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(L, A):

    dp = [[sys.maxsize] * (L+1) for _ in range(5)]
    for i in range(5):
        dp[i][0] = 0

    for i, a in enumerate(A):
        dp[0][i+1] = dp[0][i] + a
        dp[1][i+1] = min(dp[1][i], dp[0][i]) + ((a%2) if a != 0 else 2)
        dp[2][i+1] = min(dp[2][i], dp[1][i]) + ((a+1)%2)
        dp[3][i+1] = min(dp[3][i], dp[2][i]) + ((a%2) if a != 0 else 2)
        dp[4][i+1] = min(dp[4][i], dp[3][i]) + a
    
    for r in dp:
        print(r)

    return min([dp[i][L] for i in range(5)])

def main():
    L = read_int()
    A = [read_int() for _ in range(L)]
    print(slv(L, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(L, A):

    dp = [[0] * (L+1) for _ in range(5)]

    for i, a in enumerate(A):
        dp[0][i+1] = dp[0][i] + a
        dp[1][i+1] = min(dp[1][i], dp[0][i]) + ((a%2) if a != 0 else 2)
        dp[2][i+1] = min(dp[2][i], dp[1][i]) + ((a+1)%2)
        dp[3][i+1] = min(dp[3][i], dp[2][i]) + ((a%2) if a != 0 else 2)
        dp[4][i+1] = min(dp[4][i], dp[3][i]) + a
    
    return min([dp[i][L] for i in range(5)])

def main():
    L = read_int()
    A = [read_int() for _ in range(L)]
    print(slv(L, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(L, A):

    dp = [[0] * (L+1) for _ in range(5)]
    for i, a in enumerate(A):
        dp[0][i+1] = dp[0][i] + a
    for i, a in enumerate(A):
        dp[1][i+1] = min(dp[1][i], dp[0][i]) + (a%2 if a != 0 else 2)
        dp[2][i+1] = min(dp[2][i], dp[1][i]) + ((a+1)%2 if a != 0 else 1)
        dp[3][i+1] = min(dp[3][i], dp[2][i]) + (a%2 if a != 0 else 2)
        dp[4][i+1] = min(dp[4][i], dp[3][i]) + a
    
    return min([dp[i][L] for i in range(5)])

def main():
    L = read_int()
    A = [read_int() for _ in range(L)]
    print(slv(L, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(L, A):

    dp = [[0] * (L+1) for _ in range(5)]
    for i, a in enumerate(A):
        dp[0][i+1] = dp[0][i] + a
    for i, a in enumerate(A):
        dp[1][i+1] = min(dp[1][i], dp[0][i]) + (a%2 if a != 0 else 1)
        dp[2][i+1] = min(dp[2][i], dp[1][i]) + ((a+1)%2 if a != 0 else 1)
        dp[3][i+1] = min(dp[3][i], dp[2][i]) + (a%2 if a != 0 else 1)
        dp[4][i+1] = min(dp[4][i], dp[3][i]) + a
    
    return min([dp[i][L] for i in range(5)])

def main():
    L = read_int()
    A = [read_int() for _ in range(L)]
    print(slv(L, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
L = int(input())
As = [int(input()) for _ in range(L)]

for i, A in enumerate(As):
    if A > 2:
        As[i] = 2 - A%2

Xs = [0, 2, 1, 2, 0]
dp = [[float('inf')]*(5) for _ in range(L+1)]
dp[0][0] = 0
for i, A in enumerate(As, 1):
    for t, X in enumerate(Xs):
        dp[i][t] = min(dp[i-1][:t+1]) + abs(A-X)
print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/yahoo-procon2019-qual/tasks/yahoo_procon2019_qual_d
#2019-02-10

#DP
import numpy as np
L = int(input())
A = [int(input()) for i in range(L)]

DP = np.zeros(5, dtype = int)

def cost(a):
    return np.array([a,a%2,(a+1)%2,a%2,a])

for i in range(L):
    n  = np.array([min(DP[:i+1]) for i in range(5)])
    a  = A[i]
    DP = n + cost(a)



print(int(DP[4]))

Submission status: Wrong Answer

Solution code:
l = int(input())
m = []
mlist = []
for i in range(l):
  x = int(input())
  if x==0 and len(m)>0:
    mlist.append(m)
    m = []
  elif x!=0:
    m.append(x)
  if i==l-1 and len(m)>0:
    mlist.append(m)
msum = []
for i in range(len(mlist)):
  msum.append(sum(mlist[i]))
mcount = 0
for i in range(len(msum)):
  if max(msum)==msum[i]:
    mcount+=1
  else:
    mcount+=msum[i]
print(mcount)

Submission status: Wrong Answer

Solution code:
n=int(input())
zot=lambda x:0 if x==0 else (x+1)%2+1
A=[zot(int(input())) for i in range(n)]
D=[0,0,0,0,0]

for a in A:
  d0 = D[0] + a
  d1 = min(D[:2])+ [2,1,0][a]
  d2 = min(D[:3])+ [1,0,1][a]
  d3 = min(D[:4])+ [2,1,0][a]
  d4 = min(D) + a
  D=[d0,d1,d2,d3,d4]
print(min(D))

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
D=[0,0,0,0,0]
def zot(x):
  if x==0:
    return 0
  elif x%2==1:
    return 1
  else:
    return 2
  
for a in A:
  d0 = D[0] + zot(a)
  d1 = min(D[:2])+[2,1,0][zot(a)]
  d2 = min(D[:3])+[1,0,1][zot(a)]
  d3 = min(D[:4])+[2,1,0][zot(a)]
  d4 = min(D) + zot(a)
  D=[d0,d1,d2,d3,d4]
print(min(D))

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
D=[[0,0,0,0,0]]
def zot(x):
  if x==0:
    return 0
  elif x%2==1:
    return 1
  else:
    return 2
  
for a in A:
  d0 = D[-1][0] + zot(a)
  d1 = min(D[-1][:2]) + [2,1,0][zot(a)]
  d2 = min(D[-1][:3])+ [1,0,1][zot(a)]
  d3 = min(D[-1][:4])+ [2,1,0][zot(a)]
  d4 = min(D[-1]) + zot(a)
  D.append([d0,d1,d2,d3,d4])
print(min(D[-1]))

Submission status: Wrong Answer

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
D=[[0,0,0,0,0]]
def zot(x):
  if x==0:
    return 0
  elif x%2==1:
    return 1
  else:
    return 2
  
for a in A:
  d0 = D[-1][0] + zot(a)
  d1 = min(D[-1][:2]) + [2,1,0][zot(a)]
  d2 = min(D[-1][1:3])+ [1,0,1][zot(a)]
  d3 = min(D[-1][2:4])+ [2,1,0][zot(a)]
  d4 = min(D[-1][3:]) + zot(a)
  D.append([d0,d1,d2,d3,d4])
print(min(D[-1]))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(input()) for i in range(n)]

INF = int(1e15)
DP = [[INF] * (n+1) for i in range(5)]
for i in range(4):
    DP[i][0] = 0

for i in range(n):
    DP[0][i+1] = DP[0][i] + A[i]
    DP[1][i+1] = min(DP[0][i], DP[1][i]) + (A[i] % 2 if A[i] != 0 else 2)
    DP[2][i+1] = min(DP[0][i], DP[1][i], DP[2][i]) + (A[i] + 1) % 2
    DP[3][i+1] = min(DP[0][i], DP[2][i], DP[3][i]) + (A[i] % 2 if A[i] != 0 else 2)
    DP[4][i+1] = min(DP[3][i], DP[4][i]) + A[i]

print(min(DP[i][n] for i in range(1, 5)))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(input()) for i in range(n)]

INF = int(1e15)
DP = [[INF] * (n+1) for i in range(5)]
for i in range(4):
    DP[i][0] = 0

for i in range(n):
    DP[0][i+1] = DP[0][i] + A[i]
    DP[1][i+1] = min(DP[0][i], DP[1][i]) + (A[i] % 2 if A[i] != 0 else 2)
    DP[2][i+1] = min(DP[1][i], DP[2][i]) + (A[i] + 1) % 2
    DP[3][i+1] = min(DP[2][i], DP[3][i]) + (A[i] % 2 if A[i] != 0 else 2)
    DP[4][i+1] = min(DP[3][i], DP[4][i]) + A[i]

print(min(DP[i][n] for i in range(1, 5)))