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: Accepted

Solution code:

"""
Writer: SPD_9X2
https://atcoder.jp/contests/yahoo-procon2019-qual/tasks/yahoo_procon2019_qual_d

startとGoalを決めると、その間は奇数回、それ以外は偶数回通ることとなる
また、始点と終点は交換可能なので、始点が左にある方としてよい

始点・終点間は最小１で、好きな位置を2ずつ増やすことが可能
始点から左、終点より右は0があればそれ以降先は全て0にせざるを得ない
それ以外は2,4,6…と2ずつ好きに増やすことが可能

→大体情報が出そろってきた

もし終点と始点が全探索可能だとしたら？
始点より左の最適化 = 終点より右はどうするんだ？
→切れ目を全探索すれば行ける　始点・終点間は自明

高速化→dpだろうなぁ
フェーズごとに分ける

左のずっと0フェーズ→偶数フェーズ→(始点)→奇数フェーズ→(終点)→偶数フェーズ→0フェーズ
の5段階ごとに最小値を持っておいてあげればよい
あとは適当にdp(フェーズは戻っちゃだめだけど飛ばしても良いことに注意)
→解けた！！！
"""

dp = [0] * 5

L = int(input())

for i in range(L):

    ndp = [float("inf")] * 5

    A = int(input())

    ndp[0] = dp[0] + A

    if A == 0:
        D = 2
    else:
        D = A % 2
    ndp[1] = min(dp[0],dp[1]) + D
    ndp[2] = min(dp[0],dp[1],dp[2]) + (A+1)%2
    ndp[3] = min(dp[0],dp[1],dp[2],dp[3]) + D
    ndp[4] = min(dp) + A

    dp = ndp

print (min(dp))

Submission status: Accepted

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

dp = [0] * 5
for a in A :
  ndp = [0] * 5
  ndp[0] = dp[0] + a
  ndp[1] = min(dp[0:2]) + a % 2
  ndp[2] = min(dp[0:3]) + (a + 1) % 2
  ndp[3] = min(dp[0:4]) + a % 2
  ndp[4] = min(dp[0:5]) + a
  
  if a == 0 : 
    ndp[1] += 2
    ndp[3] += 2
    
  dp = ndp
  
print(min(dp))

Submission status: Accepted

Solution code:
l=int(input())
dp=[0]*5
for i in range(l):
	a,b,c,d,e,f=dp+[int(input())]
	p=int(f==0)*2
	dp=[a+f,min(a,b)+f%2+p,min(a,b,c)+(f+1)%2,min(b,c,d)+f%2+p,min(b,c,d,e)+f]
print(min(dp))

Submission status: Accepted

Solution code:
l=int(input())
A=[int(input())for _ in range(l)]
dp=[0]*5
for i in range(l):
	a,b,c,d,e,f=dp+[A[i]]
	p=int(f==0)*2
	dp=[a+f,min(a,b)+f%2+p,min(a,b,c)+(f+1)%2,min(b,c,d)+f%2+p,min(b,c,d,e)+f]
print(min(dp))

Submission status: Accepted

Solution code:
l=int(input())
A=[int(input())for _ in range(l)]
dp=[0]*5
for i in range(l):
    a,b,c,d,e,f=dp+[A[i]]
    p=int(f==0)*2
    dp=[a+f,min(a,b)+f%2+p,min(a,b,c)+(f+1)%2,min(b,c,d)+f%2+p,min(b,c,d,e)+f]
print(min(dp))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
b = [0]*n
for i in range(n):
  if a[i]%2:
    b[i] = 1
  elif a[i]:
    b[i] = 2
a.append(0)
b.append(0)
n += 1
DP = [[[10**18 for i in range(3)] for j in range(3)] for k in range(n+1)]
for i in range(3):
  for j in range(3):
    DP[0][i][j] = 0
for i in range(1,n+1):
  for k in (0,2):
    DP[i][0][k] = min(DP[i][0][k],DP[i-1][0][k]+abs(a[i-1]))
  for l in (1,2):
    DP[i][0][2] = min(DP[i][0][2],DP[i-1][l][2]+abs(a[i-1]))
  DP[i][0][2] = min(DP[i][0][2],DP[i-1][2][1]+abs(a[i-1]))

  DP[i][1][2] = min(DP[i][1][2],DP[i-1][0][0]+abs(b[i-1]-1))
  DP[i][1][2] = min(DP[i][1][2],DP[i-1][1][2]+abs(b[i-1]-1))
  DP[i][1][2] = min(DP[i][1][2],DP[i-1][2][1]+abs(b[i-1]-1))

  for k in (1,2):
    DP[i][2][k] = min(DP[i][2][k],DP[i-1][2][k]+abs(b[i-1]-2))
  DP[i][2][2] = min(DP[i][2][2],DP[i-1][1][2]+abs(b[i-1]-2))
  DP[i][2][1] = min(DP[i][2][1],DP[i-1][0][0]+abs(b[i-1]-2))
print(min(*DP[n][0]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

N = int(input())
A = [int(input()) for _ in range(N)]

p1 = 0
p2 = 0
P1 = [0]
P2 = [0]
for a in A:
    if a == 0:
        p1 -= 2
        p2 += 1
    elif a % 2 == 0:
        p2 -= 1
    elif a % 2 == 1:
        p1 -= 1
        p2 += 1
    p1 += a
    P1.append(p1)
    P2.append(p2)

dp = [P1[N], P1[N]+P2[N], -P1[N], 0]

for i in reversed(range(N)):
    dp[0] = max(dp[0], P1[i])
    dp[1] = max(dp[1], P2[i]+dp[0])
    dp[2] = min(dp[2], P2[i]-dp[1])
    dp[3] = min(dp[3], P1[i]+dp[2])

print(dp[3]+sum(A))

Submission status: Accepted

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

N = int(input())
A = [int(input()) for _ in range(N)]

p1 = 0
p2 = 0
P1 = [0]
P2 = [0]
for a in A:
    if a == 0:
        p1 -= 2
        p2 += 1
    elif a % 2 == 0:
        p2 -= 1
    elif a % 2 == 1:
        p1 -= 1
        p2 += 1
    p1 += a
    P1.append(p1)
    P2.append(p2)

S = sum(A)
dp = [[0, 0, 0, 0] for _ in range(N+1)]

dp[N] = [P1[N], P1[N]+P2[N], -P1[N], 0]

for i in reversed(range(N)):
    dp[i][0] = max(dp[i+1][0], P1[i])
    dp[i][1] = max(dp[i+1][1], P2[i]+dp[i][0])
    dp[i][2] = min(dp[i+1][2], P2[i]-dp[i][1])
    dp[i][3] = min(dp[i+1][3], P1[i]+dp[i][2])

print(dp[0][3]+S)

Submission status: Accepted

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

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

dp = [[0 for _ in range(5)] for _ in range(L+1)]
dp[0][0] = 0
for i in range(L):
    if A[i] != 0:
        dp[i+1][0] = dp[i][0] + A[i]
        dp[i+1][1] = min(dp[i][:2]) + A[i] % 2
        dp[i+1][2] = min(dp[i][:3]) + (A[i]+1) % 2
        dp[i+1][3] = min(dp[i][:4]) + A[i] % 2
        dp[i+1][4] = min(dp[i]) + A[i]
    else:
        dp[i + 1][0] = dp[i][0] + A[i]
        dp[i + 1][1] = min(dp[i][:2]) + 2
        dp[i + 1][2] = min(dp[i][:3]) + 1
        dp[i + 1][3] = min(dp[i][:4]) + 2
        dp[i + 1][4] = min(dp[i]) + A[i]
print(min(dp[-1]))

Submission status: Accepted

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] + 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] + 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: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


def cumsum(li):
    ret = []
    sum = 0
    for i in li:
        sum += i
        ret.append(sum)
    return ret


# 解説AC
N = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for _ in range(N)]

# dp[i]: i まで見たときの答えの最小値
# dp[i][0]: どこも通ってない
# dp[i][1]: D まで通った
# dp[i][2]: S まで通った
# dp[i][3]: T まで通った
# dp[i][4]: U まで通った
dp = [[0] * 5 for _ in range(N)]
for i, a in enumerate(cumsum(A)):
    dp[i][0] = a
dp[0][1] = int((A[0] % 2 != 0) + (A[0] == 0) * 2)
dp[0][2] = int(A[0] % 2 != 1)
dp[0][3] = int((A[0] % 2 != 0) + (A[0] == 0) * 2)
dp[0][4] = A[0]

for i in range(1, N):
    dp[i][ 1] = min(dp[i - 1][ :1 + 1]) + int((A[i] % 2 != 0) + (A[i] == 0) * 2)
    dp[i][ 2] = min(dp[i - 1][ :2 + 1]) + int(A[i] % 2 != 1)
    dp[i][ 3] = min(dp[i - 1][ :3 + 1]) + int((A[i] % 2 != 0) + (A[i] == 0) * 2)
    dp[i][ 4] = min(dp[i - 1][ :4 + 1]) + A[i]
print(min(dp[-1]))

Submission status: Accepted

Solution code:
def main():
    L, *A = map(int, open(0))

    EO = [(2, 1), (1, 0), (0, 1)]

    dp = [0] * 5

    for a in A:
        e, o = EO[(a - 1) % 2 + 1 if a else 0]

        dp[0] += a
        dp[1] = min(dp[0], dp[1] + e)
        dp[2] = min(dp[1], dp[2] + o)
        dp[3] = min(dp[2], dp[3] + e)
        dp[4] = min(dp[3], dp[4] + a)

    print(min(dp))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    L, *A = map(int, open(0))

    D = [(2, 1), (1, 0), (0, 1)]

    s0 = s1 = s2 = s3 = s4 = 0

    for a in A:
        e, o = D[(a - 1) % 2 + 1 if a else 0]

        s0 += a
        s1 = min(s0, s1 + e)
        s2 = min(s1, s2 + o)
        s3 = min(s2, s3 + e)
        s4 = min(s3, s4 + a)

    print(min(s0, s1, s2, s3, s4))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    L, *A = map(int, open(0))

    D = [(2, 1), (1, 0), (0, 1)]

    s0 = s1 = s2 = s3 = s4 = 0

    for a in A:
        e, o = D[(a - 1) % 2 + 1 if a else 0]

        s0 += a
        s1 = s0 if s0 < s1 + e else s1 + e
        s2 = s1 if s1 < s2 + o else s2 + o
        s3 = s2 if s2 < s3 + e else s3 + e
        s4 = s3 if s3 < s4 + a else s4 + a

    print(min(s0, s1, s2, s3, s4))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
L, *A = map(int, open(0))

D = [(2, 1), (1, 0), (0, 1)]

s0 = s1 = s2 = s3 = s4 = 0

for a in A:
    e, o = D[(a - 1) % 2 + 1 if a else 0]

    s0 += a
    s1 = s0 if s0 < s1 + e else s1 + e
    s2 = s1 if s1 < s2 + o else s2 + o
    s3 = s2 if s2 < s3 + e else s3 + e
    s4 = s3 if s3 < s4 + a else s4 + a

print(min(s0, s1, s2, s3, s4))

Submission status: Accepted

Solution code:
L, *A = map(int, open(0))

D = [(2, 1), (1, 0), (0, 1)]

s0 = s1 = s2 = s3 = s4 = 0

for a in A:
    e, o = D[(a - 1) % 2 + 1 if a else 0]

    s0 += a
    s1 = min(s0, s1 + e)
    s2 = min(s1, s2 + o)
    s3 = min(s2, s3 + e)
    s4 = min(s3, s4 + a)

print(min(s0, s1, s2, s3, s4))

Submission status: Accepted

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(b,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: Accepted

Solution code:
from itertools import accumulate

L = int(input())
A = [int(input()) for _ in range(L)]
two_way_A = [a%2 if a>0 else 2 for a in A]
one_way_A = [(a+1)%2 for a in A]

dp = [[0]*L for _ in range(5)]
transition = [A, two_way_A, one_way_A, two_way_A, A] 

dp[0] = list(accumulate(A))
for i in range(1,5):
  dp[i][0] = min(transition[i][0], dp[i-1][0])
  for j in range(1,L):
    dp[i][j] = min(min(dp[i-1][j-1],dp[i][j-1])+transition[i][j], dp[i-1][j])

print(dp[-1][-1])

Submission status: Accepted

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

dp = [0] * (L + 1)
dp[0] = [0] * 5

for i in range(L):
  dp[i + 1] = [0] * 5
  dp[i + 1][0] = dp[i][0] + A[i]
  cost1 = A[i] % 2 if A[i] != 0 else 2
  dp[i + 1][1] = min([dp[i][0] + cost1, dp[i][1] + cost1])
  cost2 = (A[i] + 1) % 2
  dp[i + 1][2] = min([dp[i][0] + cost2, dp[i][1] + cost2, dp[i][2] + cost2])
  cost3 = A[i] % 2 if A[i] != 0 else 2
  dp[i + 1][3] = min([dp[i][0] + cost3, dp[i][1] + cost3, dp[i][2] + cost3, dp[i][3] + cost3])
  dp[i + 1][4] = min([dp[i][0] + A[i], dp[i][1] + A[i], dp[i][2] + A[i], dp[i][3] + A[i], dp[i][4] + A[i]])

print(min(dp[L]))

Submission status: Accepted

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)
DP4=[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,DP0[i-1]+(A[i]+1)%2)
    DP3[i]=min(DP3[i-1]+div2(A[i]),DP2[i-1]+div2(A[i]),DP1[i-1]+div2(A[i]),DP0[i-1]+div2(A[i]))
    DP4[i]=min(DP4[i-1]+A[i],DP3[i-1]+A[i],DP2[i-1]+A[i],DP1[i-1]+A[i],DP0[i-1]+A[i])

print(min(DP0[L-1],DP1[L-1],DP2[L-1],DP3[L-1],DP4[L-1]))

Submission status: Accepted

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] > 0 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: Accepted

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] > 0 else 2
            else:
                extra = (A[i] + 1) % 2
            dp[i + 1][j] = min(dp[i][:j + 1]) + 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: Accepted

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]
        if a[i] != 0:
                dp[i+1][1] = min(dp[i][0]+a[i]%2,dp[i][1]+a[i]%2)
        else:
                dp[i+1][1] = min(dp[i][0]+2,dp[i][1]+2)
        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)
        a[i]-=1
        if a[i] != 0:
                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)
        else:
                dp[i+1][3] = min(dp[i][0]+2,dp[i][1]+2,dp[i][2]+2,dp[i][3]+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: Accepted

Solution code:
L = int(input())
A = [int(input()) for i 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 2
    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: Accepted

Solution code:
L=int(input())
A=[int(input()) for i in range(L)]
su=sum(A)
#print(A)

DP=[[0 for i in range(5)] for j in range(L+1)]

mod=A[0]%2
if A[0]==0 :
    mod=2
DP[1]=[A[0],min(A[0],mod%2),min(A[0],mod%2,(mod+1)%2),min(A[0],mod%2,(mod+1)%2),min(A[0],mod%2,(mod+1)%2)]

for i in range(2,L+1):
    mod=A[i-1]%2
    if A[i-1]==0 :
        mod=2
    DP[i][0]+=DP[i-1][0]+A[i-1]
    DP[i][1]+=min(DP[i-1][0],DP[i-1][1])+mod
    DP[i][2]+=min(DP[i-1][j] for j in range(3))+(mod+1)%2
    DP[i][3]+=min(DP[i-1][j] for j in range(4))+mod
    DP[i][4]+=min(DP[i-1][j] for j in range(5))+A[i-1]

#print(DP)
print(min(DP[L]))

Submission status: Accepted

Solution code:
L=int(input())
dp=[0]*5
for i in range(L):
  A=int(input())
  
  even=A%2
  if A==0:
    even=2
  odd=(A+1)%2
  
  #それぞれのDPには、i番目のAを5つのゾーンに割り当てた際の最小が記録される
  dp[4]=min(dp[:])+A
  dp[3]=min(dp[:4])+even
  dp[2]=min(dp[:3])+odd
  dp[1]=min(dp[:2])+even
  dp[0]=dp[0]+A
  
print(min(dp))

Submission status: Accepted

Solution code:
L, *A = map(int, open(0).read().split())
a0 = a1 = a2 = a3 = a4 = 0
rest = sum(A)
P = [2, 1, 0]
Q = [1, 0, 1]
for a in A:
    b = (a - 1) % 2 + 1 if a else 0
    p = P[b]; q = Q[b]
    a0 += a
    a1 = min(a0, a1 + p)
    a2 = min(a1, a2 + q)
    a3 = min(a2, a3 + p)
    a4 = min(a3, a4 + a)
print(min(a0, a1, a2, a3, a4))

Submission status: Accepted

Solution code:
a = int(input())

b = []

for i in range(a):
	b.append(int(input()))

c = [[0] * 5 for i in range(a+1)]

for i in range(a):
	c[i+1][0] = c[i][0] + b[i]
	c[i+1][1] = min(c[i][0],c[i][1]) + (b[i] % 2 if b[i] > 0 else 2)
	c[i+1][2] = min(c[i][0],c[i][1],c[i][2]) + ((b[i]+1) % 2)
	c[i+1][3] = min(c[i][0],c[i][1],c[i][2],c[i][3]) + (b[i] % 2 if b[i] > 0 else 2)
	c[i+1][4] = min(c[i][0],c[i][1],c[i][2],c[i][3],c[i][4]) + b[i]

print(min(c[-1]))

Submission status: Accepted

Solution code:
def main():
    l = int(input())
    nums = [int(input()) for _ in range(l)]
    dp = [[0 for _ in range(5)] for _ in range(l + 1)]

    for i in range(l):
        dp[i + 1][0] = dp[i][0] + nums[i]
        dp[i + 1][1] = min(dp[i][:2]) + (nums[i] % 2 if nums[i] != 0 else 2)
        dp[i + 1][2] = min(dp[i][:3]) + (nums[i] + 1) % 2
        dp[i + 1][3] = min(dp[i][:4]) + (nums[i] % 2 if nums[i] != 0 else 2)
        dp[i + 1][4] = min(dp[i]) + nums[i]

    print(min(dp[l]))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

func = [lambda x: x,
        lambda x: 2 if x == 0 else int(x%2),
        lambda x: int(x%2==0),
        lambda x: 2 if x == 0 else int(x%2),
        lambda x: x]

before = [0]*5
for i in range(L):
    bmin = 1e10
    after = [0]*5
    for j in range(5):
        bmin = min(bmin, before[j])
        after[j] = bmin + func[j](A[i])
    before = after

print(min(before))

Submission status: Accepted

Solution code:
l = int(input())
a = []
for i in range(0, l):
    a.append(int(input()))

left = [0] * (l + 1)
start= [0] * (l + 1)
goal = [0] * (l + 1)
right = [0] * (l + 1)
last = [0] * (l + 1)

for i in range(1, l + 1):
    left[i] = left[i - 1] + a[i - 1]
    
for i in range(1, l + 1):
    c = 0
    if a[i - 1] == 0:
        c = 2
    elif a[i - 1] % 2 == 1:
        c = 1
    start[i] = min(left[i], start[i - 1] + c)

for i in range(1, l + 1):
    c = 0
    if a[i - 1] % 2 == 0:
        c = 1
    goal[i] = min(start[i], goal[i - 1] + c)
    
for i in range(1, l + 1):
    c = 0
    if a[i - 1] == 0:
        c = 2
    elif a[i - 1] % 2 == 1:
        c = 1
    right[i] = min(goal[i], right[i - 1] + c)
    
for i in range(1, l + 1):
    last[i] = min(last[i - 1] + a[i - 1], right[i])
    
print(last[-1])

Submission status: Accepted

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 2
    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: Accepted

Solution code:
#ふぇー
import sys
def 解():
    L=int(input())
    aA=[int(_) for _ in sys.stdin.readlines()]
    dp = [0]*5
    for i in range(L):
        i生 = aA[i]
        bA = i生 % 2
        i偶 = (2 if i生 == 0 else bA)
        i奇 = (1 if i生 == 0 else ( 0 if bA else 1) )
        dp[4] = min(dp[:]) + i生
        dp[3] = min(dp[:4]) + i偶
        dp[2] = min(dp[:3]) + i奇
        dp[1] = min(dp[:2]) + i偶
        dp[0] = dp[0] + i生
    print(min(dp))
解()

Submission status: Accepted

Solution code:
L = int(input())
aList = []
for _ in range(L):
    aList.append(int(input()))

dp = [0 for _ in range(5)]
for a in aList:
    outer = a % 2 if a > 0 else 2
    inner = (a + 1) % 2
    dp[4] = min(dp[:5]) + a
    dp[3] = min(dp[:4]) + outer
    dp[2] = min(dp[:3]) + inner
    dp[1] = min(dp[:2]) + outer
    dp[0] += a

print(min(dp))

Submission status: Accepted

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):
            if 1<=k<=3:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k]-B[i-1]))
            else:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+A[i-1])
ans=10**9
#print(B)
#print(dp)
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)

Submission status: Accepted

Solution code:
L = int(input())
A = [int(input()) for i in range(L)]
DP = [[0 for _ in range(5)] for _ in range(L+1)]

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


for i in range(L):
    for j in range(5):
        DP[i+1][j] = min(DP[i][:(j+1)]) + cost(j,A[i])

print(min(DP[L]))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
L = int(input())
dp = [0] * 5
for a in map(int, (readline() for _ in range(L))):
    back = a % 2 if a > 0 else 2
    through = (a + 1) % 2
    dp[4] = min(dp[:5]) + a
    dp[3] = min(dp[:4]) + back
    dp[2] = min(dp[:3]) + through
    dp[1] = min(dp[:2]) + back
    dp[0] += a
print(min(dp))

Submission status: Accepted

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 error_mat_print(m):
    for r in m:
        print(r, 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)]
    dp[0][0] = 0

    for i, a in enumerate(A):
        t = dp[0][i]
        dp[0][i+1] = t + a
        t = min(t, dp[1][i])
        dp[1][i+1] = t+ ((a%2) if a != 0 else 2)
        t = min(t, dp[2][i])
        dp[2][i+1] = t + ((a+1)%2)
        t = min(t, dp[3][i])
        dp[3][i+1] =t + ((a%2) if a != 0 else 2)
        t = min(t, dp[4][i])
        dp[4][i+1] = t + 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: Accepted

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 error_mat_print(m):
    for r in m:
        print(r, 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(6)]
    for i in range(5):
        dp[i][0] = 0
    # dp[0][0] = 0

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

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

        # error_print(a)
        # error_mat_print(dp)

    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: Accepted

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

Xs = [0, 2, 1, 2, 0]
dp = [[0]*(5) for _ in range(L+1)]
for i, A in enumerate(As, 1):
    A2 = 2 - A%2 if A > 2 else A
    for t, (X, A0) in enumerate(zip(Xs, [A, A2, A2, A2, A])):
        dp[i][t] = min(dp[i-1][:t+1]) + abs(A0-X)
print(min(dp[-1]))

Submission status: Accepted

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

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

    for i, a in enumerate(A):
        for j in range(5):
            if j > 0:
                dp[j][i] = min(dp[j-1][i], dp[j][i])
            dp[j][i+1] = min(dp[j][i+1], dp[j][i]+s(a, j))
        # dp[0][i+1] = dp[0][i] + a
        # dp[1][i+1] = min(dp[0][i], dp[1][i]) + ((a%2) if a != 0 else 2)
        # dp[2][i+1] = min(dp[1][i], dp[2][i]) + ((a+1)%2)
        # dp[3][i+1] = min(dp[2][i], dp[3][i]) + ((a%2) if a != 0 else 2)
        # dp[4][i+1] = min(dp[3][i], dp[4][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: Accepted

Solution code:
# -*- coding: utf-8 -*-
l = int(input())
al = [int(input()) for _ in range(l)]
INF = 10**15
dp = [INF]*5  # 0,偶数,奇数,偶数,0の状態
dp[0] = 0

def cost(i, k):
    # i番目を状態kにした時に必要な回数
    if k==0 or k==4:
        return al[i]
    elif k==1 or k==3:
        if al[i]>0:
            return al[i]%2
        else:
            return 2
    else:
        if al[i]%2==0:
            return 1
        else:
            return 0

for i in range(l):
    dp2 = [min(dp[:j+1]) for j in range(5)]
    for j in range(5):
        dp[j] = dp2[j] + cost(i,j)

    # for j in range(5):
    #     for k in range(j,5):
    #         dp[i+1][k] = min(
    #             dp[i+1][k],
    #             dp[i][j] + cost(i, k)
    #         )

print(min(dp))