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:
L = input()
A = [input() for _ in range(L)]

dp = [[float("inf")] * 5 for _ in range(L)]
dp[0][0] = A[0]
dp[0][1] = A[0] % 2 if A[0] > 0 else 2
dp[0][2] = 1 - A[0] % 2
dp[0][3] = A[0] % 2 if A[0] > 0 else 2
dp[0][4] = A[0]

for i in range(L - 1):
    dp[i + 1][0] = dp[i][0] + A[i + 1]
    dp[i + 1][1] = min(dp[i][:2]) + A[i + 1] % 2 if A[i + 1] > 0 else 2
    dp[i + 1][2] = min(dp[i][:3]) + 1 - A[i + 1] % 2
    dp[i + 1][3] = min(dp[i][:4]) + A[i + 1] % 2 if A[i + 1] > 0 else 2
    dp[i + 1][4] = min(dp[i][:5]) + A[i + 1]
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

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

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

def cost(s, i, A):
    if s == 0 or s == 4:
        return A[i]
    elif s == 1 or s == 3:
        if A[i] > 0:
            if A[i]%2 == 0:
                return 0
            else:
                return 1
        else:
            return 2
    else:
        if A[i]%2 == 1:
            return 1
        else:
            return 0

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



print(min(DP[L]))

Submission status: Wrong Answer

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

#DP

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

DP = [[float("inf") for i in range(5)] for j in range(L)]
DP[0][0] = 0

def cost(s, i, A):
    if s == 0 or s == 4:
        return A[i]
    elif s == 1 or s == 3:
        if A[i] > 0:
            if A[i]%2 == 0:
                return 0
            else:
                return 1
        else:
            return 2
    else:
        if A[i]%2 == 1:
            return 1
        else:
            return 0

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



print(min(DP[L-1]))

Submission status: Wrong Answer

Solution code:
l = int(input())
a = []
for i in range(l):
  a.append(int(input()))
  
dp=[[0]*5 for i in range(l+1)]

for i in range(0,l):
  if a[i]==0:
    even=1
  else:
    even=a[i]%2
  
  dp[i+1][0]=dp[i][0]+a[i]
  dp[i+1][1]=min(dp[i][0],dp[i][1]) + even
  dp[i+1][2]=min(dp[i][0],dp[i][1],dp[i][2]) + (a[i]+1)%2
  dp[i+1][3]=min(dp[i][0],dp[i][1],dp[i][2],dp[i][3]) + even
  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[l]))

Submission status: Wrong Answer

Solution code:
l = int(input())
a = []
for i in range(l):
  a.append(int(input()))
  
dp=[[0]*5 for i in range(l+1)]

for i in range(0,l):
  
  dp[i+1][0]=dp[i][0]+a[i]
  dp[i+1][1]=min(dp[i][0],dp[i][1]) + a[i]%2
  dp[i+1][2]=min(dp[i][0],dp[i][1],dp[i][2]) + (a[i]+1)%2
  dp[i+1][3]=min(dp[i][0],dp[i][1],dp[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[l]))

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [0] * L
for i in range(L):
    A[i] = int(input())
p = sum(A)
E = [0]*(L+1) #左からi個を偶数とした時の改善得点
for i in range(L):
    if A[i] == 0:
        ref = -2
    elif A[i]%2==0:
        ref = A[i]
    else:
        ref = A[i] - 1
    E[i+1] = E[i] + ref
O = [0]*(L+1) #左からi個を偶数から奇数とした時の改善得点
for i in range(L):
    if A[i] == 0:
        ref = +1
    elif A[i]%2==0:
        ref = -1
    else:
        ref = +1
    O[i+1] = O[i] + ref

mi = E[0]
ans = 0
miind = 0
maind = 0
for i in range(L+1):#E[0]からE[i]までの最小とE[i]の差を取る
    if E[i] < mi:
        mi = E[i]
        miind = i
    if E[i]-mi >= ans:
        ans = E[i]-mi
        maind = i

p -= ans
#0,1,2,miind, ..., maind
#最大で取るとO[maind] - O[miind]
mi = O[miind]
ans = 0
for i in range(miind, maind+1):
    if O[i] < mi:
        mi = O[i]
    if O[i]-mi >= ans:
        ans = O[i]-mi

p -= ans
print(p)

Submission status: Wrong Answer

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
# N,M = map(int, input().split())
L = int(input())
A = [int(input()) for _ in  range(L)]


DP = [0] * 5
for i in range(L):
    r0 = A[i]
    r1 = (A[i] + 1) % 2

    if A[i] == 0:
        r2 = 2
    else:
        r2 = A[i] % 2

    for i,r in zip(range(4,-1,-1), [r0,r2,r1,r2,r0]):
        DP[i] = min(DP[:i+1]) + r
        print(DP)
print(min(DP))

Submission status: Wrong Answer

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

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

for i, x in enumerate(a):
    y = x % 2 + (x==0) * 2
    z = (x + 1) % 2
    
    dp[i+1][0] = min(dp[i][0:1]) + x
    dp[i+1][1] = min(dp[i][0:2]) + y
    dp[i+1][2] = min(dp[i][0:3]) + z
    dp[i+1][3] = min(dp[i][0:4]) + y
    dp[i+1][4] = min(dp[i][0:5]) + x

print(dp[-1][4])

Submission status: Wrong Answer

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

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

for i, x in enumerate(a):
    y = x % 2 + (x==0) * 2
    z = (x + 1) % 2
    
    dp[i+1][0] = dp[i][0] + x
    dp[i+1][1] = min(dp[i][0] + y, dp[i][1] + y)
    dp[i+1][2] = min(dp[i][1] + z, dp[i][0] + z, dp[i][2] + z)
    dp[i+1][3] = min(dp[i][2] + y, dp[i][1] + y, dp[i][0] + y, dp[i][3] + y)
    dp[i+1][4] = min(dp[i][3] + x, dp[i+1][3])

print(dp[-1][4])

Submission status: Wrong Answer

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

left = -1
right = L
left_min = 1
right_min = 1

sum_temp = 0
for l in range(L):
    sum_temp += A[l]
    if left_min >= sum_temp / (l + 1):
        left_min = sum_temp / (l + 1)
        left = l

sum_temp = 0
for r in range(L-left-1):
    sum_temp += A[-r-1]
    if right_min >= sum_temp / (r + 1):
        right_min = sum_temp / (r + 1)
        right = -r-1
        
ans = sum(A[:left+1]) + sum(A[right:])

A_ = [a % 2 for a in A][left+1:right]

left = -1
right = len(A_)
left_min = 0.5
right_min = 0.5

sum_temp = 0
for l in range(len(A_)):
    sum_temp += A_[l]
    if left_min > sum_temp / (l+1):
        left_min = sum_temp / (l+1)
        left = l
        
sum_temp = 0
for r in range(len(A_)-left-1):
    sum_temp += A_[-r-1]
    if right_min > sum_temp / (r+1):
        right_min = sum_temp / (r+1)
        right = -r-1

ans += sum(A_[:left+1]) + sum(A_[right:]) + sum([1 - a for a in A_[left+1:right]])
print(ans)

Submission status: Wrong Answer

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

INF = float('inf')
dp = [[INF] * 5 for _ in range(l+10)]
dp[0][0] = 0

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

    dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + a[i])
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + a[i] % 2 if a[i] > 0 else dp[i][1] + 2)
    dp[i + 1][2] = min(dp[i + 1][2], dp[i][2] + (a[i] + 1) % 2)
    dp[i + 1][3] = min(dp[i + 1][3], dp[i][3] + a[i] % 2 if a[i] > 0 else dp[i][1] + 2)
    dp[i + 1][4] = min(dp[i + 1][4], dp[i][4] + a[i])

ans = float('inf')
for j in range(5):
    ans = min(ans, dp[l][j])

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = [int(input()) for _ in range(n)]
def ear(n,t):
    if n:
        if t == 1 or t == 3:
            return n%2
        elif t == 2:
            return (n-1)%2
        else:
            return n
    else:
        if t == 1 or t == 3:
            return 2
        elif t == 2:
            return 1
        else:
            return 0

dp = [[0]*5 for _ in range(n)]
for i in range(n):
    for t in range(5):
        if i == 0:
            dp[i][t] = ear(arr[i],t)
        else:
            if t == 0:
                dp[i][t] = dp[i-1][t] + arr[i]
            else:
                dp[i][t] = ear(arr[i],t) + min(dp[i-1][t],dp[i-1][t-1])
print(min(dp[n-1]))

Submission status: Wrong Answer

Solution code:
L = int(input())

inf = float('inf')
dp = [[inf] * (L+1) for _ in range(5)]
dp[0][0] = 0
for i in range(1, L+1):
    a = int(input())
    if a == 0:
        dp[0][i] = dp[0][i-1] + a
        dp[1][i] = min(dp[0][i-1], dp[1][i-1]) + 2
        dp[2][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1]) + 1
        dp[3][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1]) + 2
        dp[4][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1], dp[4][i-1])
    else:
        b = a & 1
        c = b ^ 1
        dp[0][i] = dp[0][i-1] + a
        dp[1][i] = min(dp[0][i-1], dp[1][i-1]) + b
        dp[2][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1]) + c
        dp[3][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1]) + b
        dp[4][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1], dp[4][i-1]) + a

print(min(dp[3][L], dp[4][L]))

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [0] * L
for i in range(L):
    A[i] = int(input())
p = sum(A)
E = [0]*(L+1) #左からi個を偶数とした時の改善得点
for i in range(L):
    if A[i] == 0:
        ref = -2
    elif A[i]%2==0:
        ref = A[i]
    else:
        ref = A[i] - 1
    E[i+1] = E[i] + ref
O = [0]*(L+1) #左からi個を偶数から奇数とした時の改善得点
for i in range(L):
    if A[i] == 0:
        ref = -1
    elif A[i]%2==0:
        ref = -1
    else:
        ref = +1
    O[i+1] = O[i] + ref

mi = E[0]
ans = 0
miind = 0
maind = 0
for i in range(L+1):#E[0]からE[i]までの最小とE[i]の差を取る
    if E[i] < mi:
        mi = E[i]
        miind = i
    if E[i]-mi >= ans:
        ans = E[i]-mi
        maind = i

p -= ans
#0,1,2,miind, ..., maind
#最大で取るとO[maind] - O[miind]
mi = O[miind]
ans = 0
for i in range(miind, maind+1):
    if O[i] < mi:
        mi = O[i]
    if O[i]-mi >= ans:
        ans = O[i]-mi

p -= ans
print(p)

Submission status: Wrong Answer

Solution code:
L = int(input())

inf = float('inf')
dp = [[inf] * (L+1) for _ in range(5)]
dp[0][0] = 0
for i in range(1, L+1):
    a = int(input())
    if a == 0:
        dp[0][i] = dp[0][i-1] + a
        dp[1][i] = min(dp[0][i-1], dp[1][i-1] + 2)
        dp[2][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1]) + 1
        dp[3][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1]) + 2
        dp[4][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1], dp[4][i-1])
    else:
        b = a & 1
        c = not b
        dp[0][i] = dp[0][i-1] + a
        dp[1][i] = min(dp[0][i-1], dp[1][i-1]) + b
        dp[2][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1]) + c
        dp[3][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1]) + b
        dp[4][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1], dp[4][i-1]) + a

print(min(dp[3][L], dp[4][L]))

Submission status: Wrong Answer

Solution code:
L = int(input())

inf = float('inf')
dp = [[inf] * (L+1) for _ in range(5)]
dp[0][0] = 0
for i in range(1, L+1):
    a = int(input())
    if a == 0:
        dp[0][i] = dp[0][i-1] + a
        dp[1][i] = min(dp[0][i-1], dp[1][i-1] + 2)
        dp[2][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1]) + 1
        dp[3][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1]) + 2
        dp[4][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1], dp[4][i-1])
    else:
        b = a & 1
        c = not b
        dp[0][i] = dp[0][i-1] + a
        dp[1][i] = min(dp[0][i-1], dp[1][i-1]) + b
        dp[2][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1]) + c
        dp[3][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1]) + b
        dp[4][i] = min(dp[0][i-1], dp[1][i-1], dp[2][i-1], dp[3][i-1]) + a

print(min(dp[3][L], dp[4][L]))

Submission status: Wrong Answer

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

INF = float('inf')
dp = [[INF] * 5 for _ in range(l+10)]
dp[0][0] = 0

for i in range(l):
    for j in range(5):
        if j + 1 <= 4:
            dp[i][j+1] = min(dp[i][j+1], dp[i][j])
        if j == 0:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i])
        elif j == 1:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i] % 2)
        elif j == 2:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (a[i] + 1) % 2)
        elif j == 3:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i] % 2)
        elif j == 4:
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i])

ans = float('inf')
for j in range(5):
    ans = min(ans, dp[l][j])

print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor

import heapq
import itertools
import operator


def get_int():
    return int(input())


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


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


def get_char_list():
    return list(input().strip())


def trim0(l):
    assert isinstance(l, list)
    while l and l[0] == 0:
        l.pop(0)
    while l and l[-1] == 0:
        l.pop(-1)


def even_len(l):
    return len(list(itertools.takewhile(lambda x: x % 2 == 0, l)))


def count0(l):
    return len(list(filter(lambda x: x == 0, l)))


def count1(l):
    return len(list(filter(lambda x: x == 1, l)))


def count_evens(l):
    return len(list(filter(lambda x: x % 2 == 0, l)))


def get_end_point(l):
    assert isinstance(l, list)
    costs = [0] * len(l)
    count = 0
    for i in range(1, len(l)):
        if l[i - 1] == 0:
            count -= 2
        elif l[i - 1] == 1:
            pass
        else:
            count += l[i - 1] - (l[i - 1] % 2)
        costs[i] = count
    return max(filter(lambda x: x[1] == min(costs), enumerate(costs)), key=lambda x: x[0])[0]


def get_start_point(l):
    assert isinstance(l, list)
    costs = [0] * (len(l) + 1)
    count = 0
    for i in range(1, len(l) + 1):
        if l[len(l) - i] <= 1:
            count -= 1
        elif l[len(l) - i] % 2 == 1:
            count -= 1
        else:
            count += 1
        costs[len(l) - i] = count
    return min(filter(lambda x: x[1] == min(costs), enumerate(costs)), key=lambda x: x[0])[0]


def main():
    l = get_int()
    a = [get_int() for _ in range(l)]

    left_end = get_end_point(a)
    left_start = left_end + get_start_point(a[left_end:])
    right_end = l - get_end_point(a[::-1])
    right_start = right_end - get_start_point(a[:right_end][::-1])

    cost_outer = sum(a[:left_end]) + sum(a[right_end:])
    cost_l = 0
    cost_l += count0(a[left_end:left_start]) * 2
    cost_l += (left_start - left_end) - count_evens(a[left_end:left_start])
    cost_r = 0
    cost_r += count0(a[right_start:right_end]) * 2
    cost_r += (right_end - right_start) - count_evens(a[right_start:right_end])
    cost_middle = count_evens(a[left_start:right_start])
    print(cost_outer + cost_l + cost_r + cost_middle)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [0] * (L + 1)
DP = [[0, 0, 0, 0, 0] for i in range(L + 1)]
for i in range(L):
  a = int(input())
  DP[i + 1][0] = DP[i][0] + a
  DP[i + 1][1] = min(DP[i][1], DP[i + 1][0]) + a % 2
  DP[i + 1][2] = min(min(DP[i][:3]), DP[i + 1][1]) + (1 - a % 2)
  DP[i + 1][3] = min(min(DP[i][:4]), DP[i + 1][2]) + a % 2
  DP[i + 1][4] = min(min(DP[i]), DP[i + 1][3]) + a
print(min(DP[L]))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

N = int(input())
A = [int(input()) for i in range(N)]
E = [int(a%2==0) for a in A]
O = [int(a%2==1) for a in A]

def accumulate(X):
    S = [0]*len(X)
    tmp = 0
    for i in range(len(X)):
        tmp += X[i]
        S[i] = tmp
    return S

LAS = accumulate(A)
RAS = accumulate(A[::-1])[::-1]

LEFT = [0]*(N+1)
RIGHT = [0]*(N+1)

for i in range(1, N+1):
    LEFT[i] = min(LAS[i-1], LEFT[i-1]+E[i-1])
for i in range(N-1, -1, -1):
    RIGHT[i] = min(RAS[i], RIGHT[i+1]+O[i])

RIGHT += [0]

ans = 1e10
for i in range(N+1):
    ans = min(ans, LEFT[i] + RIGHT[i+1])

for i in range(1, N+1):
    LEFT[i] = min(LAS[i-1], LEFT[i-1]+O[i-1])
for i in range(N-1, -1, -1):
    RIGHT[i] = min(RAS[i], RIGHT[i+1]+E[i])
RIGHT += [0]

for i in range(N+1):
    ans = min(ans, LEFT[i] + RIGHT[i+1])

print(ans)

Submission status: Wrong Answer

Solution code:
import math

data = input().split()  
l = int(data[0])
a = []
tmp = []
for i in range(l):
    val = int(input())
    if(val == 0):
        a.append(tmp)
        tmp = []
    else:
        tmp.append(val)
a.append(tmp)

size=[]
for i in a:
    size.append(sum(i))

z=sum(size)-max(size)

ind=size.index(max(size))

#print(a)
lst=[]
suma=0
for n in range(len(a[ind])):
    if(a[ind][n]%2 == 0):
        lst.append(suma)
        suma=0
    else:
        suma+=1
lst.append(suma)

#print(lst)
#print(sum(lst))
#print(max(lst))

sanp=0
sanp=sum(lst)-max(lst)

print(sanp+z)

Submission status: Wrong Answer

Solution code:
def func(l, a):
    # スタートを0としてiまで歩く時に減らせるコスト
    imos = [0 for _ in range(l+1)]
    res = [0 for _ in range(l)]
    for i in range(1, l+1):
        if a[i-1] % 2 == 1:
            imos[i] = imos[i - 1] + a[i - 1]
            res[i-1] = 0
        else:
            imos[i] = imos[i - 1] + a[i - 1] - 1
            if a[i-1] == 0:
                a[i-1] = 0
            else:
                res[i-1] = 1
    # print("a:{}".format(a))
    # print("imos:{}".format(imos))
    # print("res:{}".format(res))
    _sum = 0
    for i in range(1, l+1):
        if res[i-1] == 1:
            _sum += 1
        else:
            _sum -= 1
            if _sum == -1:
                _sum = 0
        imos[i] += _sum
    # print("imos:{}\n".format(imos))
    _max = 0
    _min = float("inf")
    for i in range(len(imos)):
        _min = min(_min, imos[i])
        _max = max(_max, imos[i] - _min)
    return sum(a) - _max
l = int(input())
a = [0 for _ in range(l)]
for i in range(l):
    a[i] = int(input())
print(min(func(l, a), func(l, list(reversed(a)))))
# print("print(\"vs \" + str(func({}, {})))".format(l, a))
# print("1 vs " + str(min(func(4, [1, 0, 2, 3]), func(4, list(reversed([1, 0, 2, 3]))))))
# print("3 vs " + str(min(func(8, [2, 0, 0, 2, 1, 3, 4, 1]), func(8, list(reversed([2, 0, 0, 2, 1, 3, 4, 1]))))))
# print("1 vs " + str(func(7, [314159265, 358979323, 846264338, 327950288, 419716939, 937510582, 0])))

Submission status: Wrong Answer

Solution code:
import numpy as np
L = int(input())
A = np.empty(L,dtype=np.int)
for i in range(L):
  a = int(input())
  if a == 0:
    A[i] = 2
  elif a % 2:
    A[i] = 1
  else:
    A[i] = 0

s1 = A.sum()
s2 = np.abs(A-1).sum()

P1=np.empty(L+2,dtype=np.int)
Q1=np.empty(L+2,dtype=np.int)
P2=np.empty(L+2,dtype=np.int)
Q2=np.empty(L+2,dtype=np.int)
P1m=np.empty(L+2,dtype=np.int)
Q1m=np.empty(L+2,dtype=np.int)
P2m=np.empty(L+2,dtype=np.int)
Q2m=np.empty(L+2,dtype=np.int)
P1[0]=P2[0]=Q1[0]=Q2[0]=0
P1m[0]=P2m[0]=Q1m[0]=Q2m[0]=0

p=0
q=0
for i in range(1,L+1):
  a = A[i-1]
  if a == 2:
    p += 1
  elif a == 0:
    p += -1
  else:
    p += 1
  P1[i] = max(P1[i-1],p)
  P1m[i] = min(P1m[i-1],p)

  P2[i] = min(P2[i-1],p)
  P2m[i] = max(P2m[i-1],p)
  
  b = A[L-i]
  if b == 2:
    q += 1
  elif b == 0:
    q += -1
  else:
    q += 1
  Q1[i] = max(Q1[i-1],q)
  Q1m[i] = min(Q1m[i-1],q)
  Q2[i] = min(Q2[i-1],q)
  Q2m[i] = max(Q2m[i-1],q)

P1[L+1]=P1[L]
P2[L+1]=P2[L]
Q1[L+1]=Q1[L]
Q2[L+1]=Q2[L]
P1m[L+1]=P1m[L]
P2m[L+1]=P2m[L]
Q1m[L+1]=Q1m[L]
Q2m[L+1]=Q2m[L]

P1 = P1 - P1m
Q1 = Q1 - Q1m
P2 = P2 - P2m
Q2 = Q2 - Q2m

t1 = (P1+Q1[::-1]).max()
ans1 = s1 - t1
t2 = (P2+Q2[::-1]).min()
ans2 = s2 + t1

ans = min(ans1,ans2)

print(ans)

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [int(input()) for _ in range(L)]
ans = [0 for _ in range(5)]
for i, a in enumerate(A):
    x = 2 if a == 0 else a % 2
    y = (a + 1) % 2
    ans[0] += a
    ans[1] = min(ans[1] + y, ans[0])
    ans[2] = min(ans[1], ans[2] + x)
    ans[3] = min(ans[2], ans[3] + y)
    ans[4] = min(ans[3], ans[4] + A[i])

print(min(ans))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

N = int(input())
A = [int(input()) for i in range(N)]
E = [int(a%2==0) for a in A]
O = [int(a%2==1) for a in A]

def accumulate(X):
    S = [0]*len(X)
    tmp = 0
    for i in range(len(X)):
        tmp += X[i]
        S[i] = tmp
    return S

LAS = accumulate(A)
RAS = accumulate(A[::-1])[::-1]

LES = accumulate(E)
ROS = accumulate(O[::-1])[::-1]

LEFT = [0]*(N+1)
RIGHT = [0]*(N+1)

for i in range(1, N+1):
    LEFT[i] = min(LAS[i-1], LEFT[i-1]+E[i-1])
for i in range(N-1, -1, -1):
    RIGHT[i] = min(RAS[i], RIGHT[i+1]+O[i])

RIGHT += [0]

ans = 1e10
for i in range(N+1):
    ans = min(ans, LEFT[i] + RIGHT[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
L = int(input())
A = [int(input()) for _ in range(L)]
ans = [0 for _ in range(5)]
for i, a in enumerate(A):
    x = 2 if a == 0 else a % 2
    y = 2 if a == 0 else a % 2
    ans[0] += a
    ans[1] = min(ans[1] + y, ans[0])
    ans[2] = min(ans[1], ans[2] + x)
    ans[3] = min(ans[2], ans[3] + y)
    ans[4] = min(ans[3], ans[4] + A[i])

print(min(ans))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
l = int(input())
odd = [0]
even = [0]
for i in range(l):
    a = int(input())
    if a%2==1:
        odd.append(odd[i]+1)
        even.append(even[i])
    else:
        odd.append(odd[i])
        even.append(even[i]+1)

res = 10**15
for e in range(l):
    r = even[e]-even[0]
    r += odd[l]-odd[e]
    res = min(res,r)

for e in range(l-1,-1,-1):
    r = odd[l]-odd[e]
    r += even[e]-even[0]
    res = min(res,r)

print(res)

Submission status: Wrong Answer

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

left = -1
right = L
left_min = 0.5
right_min = 0.5

for l in range(L):
    if left_min >= sum(A[:l+1]) / (l + 1):
        left_min = sum(A[:l+1]) / (l + 1)
        left = l
        
for r in range(L-left-1):
    if right_min >= sum(A[-r-1:]) / (r + 1):
        right_min = sum(A[-r-1:]) / (r + 1)
        right = r
        
ans = sum(A[:left+1]) + sum(A[right:])

A_ = [a % 2 for a in A][left+1:right]

left = -1
right = len(A_)
left_min = 0.5
right_min = 0.5

for l in range(len(A_)):
    if left_min >= sum(A_[:l+1]) / (l+1):
        left_min = sum(A_[:l+1]) / (l+1)
        left = l
        
for r in range(len(A_)-left-1):
    if right_min >= sum(A_[-r-1:]) / (r+1):
        right_min = sum(A_[-r-1:]) / (r+1)
        right = r

ans += sum(A_[:left+1]) + sum(A_[right:]) + sum([1 - a for a in A_[left+1:right]])
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = [int(input()) for n in range(N)]

F = []
G = [0] * (N + 1)


odd_node = 0
pre_link = 0
sum_link = 0
head = 0
for n, l in enumerate(L):
    tail = n
    sum_l = pre_link + l
    pre_link = l
    if sum_l % 2 == 1:
        odd_node += 1
        G[n] = 1

    if l == 0:
        F.append([(head, tail), odd_node, sum_link])
        odd_node = 0
        sum_link = 0
        head = tail + 1
    else:
        sum_link += l

if pre_link % 2 == 1:
    odd_node += 1
    G[N] = 1
F.append([(head, N - 1), max(0, odd_node), sum_link])

F2 = []
L2 = []
f1 = F[0]
marge = 0
for i in range(1, len(F)):
    f2 = F[i]

    f1_tail = f1[0][1]
    f2_head = f2[0][0]
    if ((G[f1_tail] != G[f2_head])
            or ((G[f1_tail] == 0) and (f1[1] != 0 or f2[1] != 0))
            or (f1[2] == 0 or f2[2] == 0)):
        F2.append(max(0, (f1[1] // 2) - 1))
        L2.append(f1[2])
        f1 = f2
    else:
        marge += 1
        new_odd_node = f1[1] + f2[1]
        new_odd_node += 2 if G[f1_tail] == 0 else -2
        f1 = [(f1[0][0], f2[0][1]), new_odd_node, f1[2] + f2[2] + 1]

F2.append(max(0, (f1[1] // 2) - 1))
L2.append(f1[2])

max_d = 0
for f, l in zip(F2, L2):
    max_d = max(max_d, l - f)

print(sum(L2) - max_d + marge)

Submission status: Wrong Answer

Solution code:
L = int(input())
A = list(int(input()) for i in range(L))


#while True :
#    if A[0] == 0 :
#        A.pop(0)
#    else :
#        break
#L = len(A)

ans = 0

ans1 = 0
ans2 = 0

for i in range(1,L) :
    if A[i] % 2 == 0 :
        ans1 += 1
A = A[::-1]
for i in range(1,L) :
    if A[i] % 2 == 0 :
        ans2 += 1

ans = min(ans1,ans2)

print(ans-1)

Submission status: Wrong Answer

Solution code:
def nonzero(a):
    la = len(a)

    # smaller than start => expect even
    sc = 0
    best_sc = None
    start = 0
    for i in range(0, la):
        if a[i] & 1 == 0:
            sc += 1
        else:
            sc -= 1
        if (best_sc is None) or (best_sc < sc):
            best_sc = sc
            start = i

    # bigger than goal => expect even
    sc = 0
    best_sc = None
    goal = la - 1
    for i in range(la - 1, -1, -1):
        if a[i] & 1 == 0:
            sc += 1
        else:
            sc -= 1
        if (best_sc is None) or (best_sc < sc):
            best_sc = sc
            goal = i
            if start >= goal:
                return 0

    # between start and goal => expect odd
    for i in range(start + 1, goal):
        if a[i] & 1 == 1:
            sc += 1
    return sc


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

best_sc = None
if 0 not in a[1:l-1]:
    best_sc = nonzero(a)
else:
    for i in range(1, l-1):
        if a[i] == 0:
            #print(f'a[{i}] == 0:')
            sc = sum(a[:i]) + nonzero(a[i+1:])
            #print(f'sc = {sc}')
            if (best_sc is None) or (best_sc > sc):
                best_sc = sc
            sc = nonzero(a[:i]) + sum(a[i+1:])
            #print(f'sc = {sc}')
            if (best_sc is None) or (best_sc > sc):
                best_sc = sc

print(best_sc)

Submission status: Wrong Answer

Solution code:
from collections import deque
L = int(input())

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

checked = []
i = 0
score = 0
even_num = 0
zerocount = 0
before_even = False
double_even = False
while i < L:
    a = A[i]
    if a % 2 == 1:
        score += a
        i += 1
        before_even = False
    elif a == 0:
        while A[i] == 0:
            i += 1
            if i >= L:
                break
            zerocount += 1
        if score+even_num != 0:
            checked.append([score, zerocount, even_num, double_even])
        score = 0
        zerocount = 0
        even_num = 0
        before_even = False
        double_even = False
    else:
        if before_even:
            double_even = True
        score += a-1
        even_num += 1
        i += 1
        before_even = True
checked.append([score, 0, even_num, double_even])
now_s = 0
now_z = 0
now_e = 0
now_d = False
new = checked[:]


ans = 0
max_i = 0
max_score = 0
S = []
flags = []
for i, each in enumerate(new):
    s, z, e, d = each
    ans += s+e
    temp = s-z
    S.append(temp)
    if e > 0:
        flags.append([True, d])
    else:
        flags.append([False, d])
p = False
for f1, f2 in flags:
    if f2:
        p = True
        ans -= 1
        break
    if f1:
        p = True
if p:
    ans -= 1

SubS = []
temp_s = 0
for s in S[::-1]:
    temp_s += s
    SubS.append(temp_s)
SubS = [0, ] + SubS[::-1]
l = len(SubS)
max_index1 = 0
max_index2 = 0
for i, s in enumerate(SubS):
    if i == l:
        break
    if i == l-1:
        maxS = SubS[i]
    else:
        maxS = max(SubS[i+1:])
    if maxS - s >= max_score:
        max_score = maxS
#print(SubS)
ans -= max_score
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
L = int(input())
A = np.empty(L,dtype=np.int)
for i in range(L):
  a = int(input())
  if a == 0:
    A[i] = 2
  elif a % 2:
    A[i] = 1
  else:
    A[i] = 0

s1 = A.sum()
s2 = np.abs(A-1).sum()

P1=np.empty(L+2,dtype=np.int)
Q1=np.empty(L+2,dtype=np.int)
P2=np.empty(L+2,dtype=np.int)
Q2=np.empty(L+2,dtype=np.int)
P1[0]=P2[0]=Q1[0]=Q2[0]=0

p=0
q=0
for i in range(1,L+1):
  a = A[i-1]
  if a == 2:
    p += 1
  elif a == 0:
    p += -1
  else:
    p += 1
  P1[i] = max(P1[i-1],p)
  P2[i] = min(P2[i-1],p)
  
  b = A[L-i]
  if b == 2:
    q += 1
  elif b == 0:
    q += -1
  else:
    q += 1
  Q1[i] = max(Q1[i-1],q)
  Q2[i] = min(Q2[i-1],q)

P1[L+1]=P1[L]
P2[L+1]=P2[L]
Q1[L+1]=Q1[L]
Q2[L+1]=Q2[L]

t1 = (P1+Q1[::-1]).max()
ans1 = s1 - t1
t2 = (P2+Q2[::-1]).min()
ans2 = s2 + t1

ans = min(ans1,ans2)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
target = [int(input()) for _ in range(n)]

zero = target.count(0)

if zero == 0:
    print(0)
elif zero == 1:
    if len(target[:target.index(0)]) > len(target[target.index(0)+1:]):
        print(sum(target[target.index(0)+1:]))
    else:
        print(sum(target[:target.index(0)]))

Submission status: Wrong Answer

Solution code:
L = int(input())
stones_count = []
for i in range(0, L):
    A = int(input())
    if A % 2 == 1:
        stones_count.append(1)
    elif A == 0:
        stones_count.append(0)
    else:
        stones_count.append(2)

sub_count_0 = []
sub_count_1 = []
sub_count_2 = []
count_0 = 0
count_1 = 0
count_2 = 0
for i in range(0, L):
    if stones_count[i] == 0:
        if count_1 != 0:
            sub_count_1.append(count_1)
        if count_2 != 0:
            sub_count_2.append(count_2)
        count_1 = 0
        count_2 = 0
        
        count_0 += 1
    elif stones_count[i] == 1:
        if count_0 != 0:
            sub_count_0.append(count_0)
        if count_2 != 0:
            sub_count_2.append(count_2)
        count_2 = 0
        count_1 += 1
    else:
        if count_0 != 0:
            sub_count_0.append(count_0)
        count_1 += 1
        count_2 += 1

if count_0 != 0:
    sub_count_0.append(count_0)
if count_1 != 0:
    sub_count_1.append(count_1)
if count_2 != 0:
    sub_count_2.append(count_2)

result = min(len(sub_count_1) - 1, sum(sub_count_0))
print(result + len(sub_count_2) - 1)