Problem:


Score : 700 points

Problem Statement

N cells are arranged in a row.
Some of them may contain tokens.
You are given a string s that consists of 0s and 1s.
If the i-th character of s is 1, the i-th cell (from left) contains a token.
Otherwise, it doesn't contain a token.

Snuke wants to perform the following operation as many times as possible.
In each operation, he chooses three consecutive cells.
Let's call the cells X, Y, Z from left to right.
In order for the operation to be valid, both X and Z must contain tokens and Y must not contain a token.
Then, he removes these two tokens and puts a new token on Y.

How many operations can he perform if he performs operations in the optimal way?

Constraints

1 \leq N \leq 500,000

|s| = N

Each character in s is either 0 or 1.

Input

Input is given from Standard Input in the following format:

N
s

Output

Print the answer.

Sample Input 1

7
1010101

Sample Output 1

2

For example, he can perform two operations in the following way:

Perform an operation on the last three cells. Now the string that represents tokens becomes 1010010.

Perform an operation on the first three cells. Now the string that represents tokens becomes 0100010.

Note that the choice of operations matters.
For example, if he chooses three cells in the middle first, he can perform no more operations.

Sample Input 2

50
10101000010011011110001001111110000101010111100110

Sample Output 2

10



Sample input:
7
1010101


Sample output:
2

Submission status: Runtime Error

Solution code:
# D
N = int(input())
s = list(map(int, list(input())))

res = 0
i = 0



L = 0
for i in range(N):
    if s[i] == 1:
        L = i
        break
for i in range(N):
    if s[i] == 1:
        M = i        

        
def calc_add(temp_list):
    if len(temp_list) <= 1:
        return 0
    else:
        res = 0
        # use 101 for left
        dp_l = [0]*len(temp_list)
        # use 101 for right 
        dp_r = [0]*len(temp_list)
        # do not use 101
        dp_c = [0]*len(temp_list)
        for i in range(len(temp_list) - 1):
            if temp_list[i] > 1:
                dp_l[i+1] = max(dp_l[i] + temp_list[i] - 1, dp_c[i] + temp_list[i])
            else:
                dp_l[i+1] = dp_c[i] + temp_list[i]
            if temp_list[i+1] > 1:
                dp_r[i+1] = max(dp_l[i] + temp_list[i+1], dp_c[i] + temp_list[i+1], dp_r[i] + temp_list[i+1] - 1)
            else:
                dp_r[i+1] = dp_c[i] + temp_list[i+1]
            dp_c[i+1] = max(dp_l[i], dp_r[i], dp_c[i])
        
        return max(dp_l[-1], dp_r[-1], dp_c[-1])
    

cont0 = 1
cont00 = 1
temp_list = []

for i in range(L, M+1):
    if s[i] == 1:
        if cont0 == 1:
            temp_list.append(1)
            cont0 = 0
            cont00 = 0
        else:
            temp_list[-1] += 1
    else:
        if cont00 == 1:
            pass
        elif cont0 == 1:
            cont00 = 1
            res += calc_add(temp_list)
            temp_list = []
        else:
            cont0 = 1
            
    if i == M:
        res += calc_add(temp_list)
        temp_list = []
        
print(res)

Submission status: Runtime Error

Solution code:
# def makelist(n, m):
#     return [[0 for i in range(m)] for j in range(n)]

N = int(input())
s = input()

dp = []

cnt = [0]*2
cnt[int(s[0])] += 1
for i in range(1, N):
    if i == N-1:

    else:
        now = int(s[i])
        if cnt[now] > 0:
            cnt[now] += 1
        else:
            anti = (cnt + 1) % 2
            if anti == 0:
                dp.append(-cnt[anti])
            else:
                dp.append(cnt[anti])
            cnt[anti] = 0
            cnt[now] = 1

ans = 0
nex = False
pre = False
for e in dp:
    if not start and e > 0:
        start = True
    else:
        continue

    if e > 0:
        if nex:
            nex = False
            pre = True
            ans += e
    elif e == -1:
        if pre:
            ans -= 1
            nex = True
        else:
            nex = True
        pre = False
    elif e <= -2:
        pre = False
        

print(ans)

Submission status: Runtime Error

Solution code:
# D
N = int(input())
s = list(map(int, list(input())))

res = 0
i = 0


temp_list = [0]
L = 0
for i in range(N):
    if s[i] == 1:
        L = i
        break
for i in range(N):
    if s[i] == 1:
        M = i        

for i in range(L, M+1):
    if i == M:
        # stop add calculate
        if temp_list[0] == 0:
            temp_list = temp_list[1:]
        res += sum(temp_list) - min(temp_list)
        temp_list = [0]
    elif s[i] == 1:
        temp_list[-1] += 1
    elif s[i] == 0 and s[i+1] == 0:
        # stop add calculate
        if temp_list[0] == 0:
            temp_list = temp_list[1:]
        res += sum(temp_list) - min(temp_list)
        temp_list = [0]
    else:
        temp_list.append(0)
        
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
s = input()

while s.count('000'):
    s = s.replace('000', '00')
ss = s.split('00')

A = []
for si in ss:
    num = 0
    ai = []
    for c in si:
        if c == '1':
            num += 1
        else:
            if num > 0:
                ai.append(num)
                num = 0

    if num > 0:
        ai.append(num)

    A.append(ai)

ans = 0
for ai in A:
    if ai[0] < ai[-1]:
        ai = ai[::-1]

    flg = True
    for i, aa in enumerate(ai):
        if i == len(ai) - 1:
            break

        if flg == True:
            ans += aa
            flg = False
        else:
            ans += aa - 1
            if aa == 1:
                flg = True

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
s = list(input())

count = 0
idx = 0
while True:
    if N - 2 <= idx:
        break
    if s[idx:idx + 3] == ['1', '0', '1']:

        countL = 1
        for i in range(0, idx)[::-1]:
            if s[i] == '0':
                break
            countL += 1

        countR = 1
        for i in range(idx + 3, N):
            if s[i] == '0':
                if s[i + 1] == '1':
                    countR = 0
                break
            countR += 1

        if countR <= countL:
            count += countL
            s[idx + 2] = '0'
            idx += 3
        else:
            count += countR
            idx += countR + 2
    idx += 1

print(count)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


TARGET_S = "101"
ALETERED_S = "010"
TARGET_S_LEN = 2

def position_generator(ctarget_string):
    """
    Test:
    >>> position_generator("101000101")
    [0, 6]
    """
    ret_list = []
    search_pos = 0
    while ctarget_string.find(TARGET_S, search_pos) != -1:
        add_index = ctarget_string.find(TARGET_S, search_pos)
        ret_list.append(add_index)
        search_pos += add_index + TARGET_S_LEN
    return ret_list

def search(target_string, replace_num):
    if target_string.find(TARGET_S) == -1:
        return replace_num
    else:
        v = float("-inf")
        replace_num += 1
        for replace_pos in position_generator(target_string):
            replaced_string = target_string[:replace_pos] + ALETERED_S + target_string[replace_pos + 3:]
            search_value = search(replaced_string, replace_num)
            if v < search_value:
                v = search_value
        return v

def main():
    _ = input()
    t_string = input()
    replace_num = 0
    print(search(t_string, replace_num))
    return None

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# D - 101 to 010
n = int(input())
s = input()
ss = s
cnt=0
while "1011" in ss:
    #print(ss)
    i = ss.find("1011")
    j = i+2
    while ss[j]=="1" and j<n:
        #print(j)
        j+=1
    cnt+=j-i-2
    ss = ss[:i] + "0"*(j-i-3)+"010" + ss[j:]
    
    
while "101" in ss:
    #print("F")
    m= len(ss)
    i = 0
    while i <= m-3:
        if ss[i:i+3]=="101":
            j = 0
            ss = ss[:i] + "010" + ss[i+3+j:]
            #print(ss)
            cnt+=1
            i+=3
            #print(cnt)
        else:
            i+=1
print(cnt)

Submission status: Runtime Error

Solution code:
N = map(int,input())
S = input()
def solve(S):
    if len(S)<3:
        return 0
    if len(S) == 3:
        if S == '101':
            return 1
        else:
            return 0
    for i in range(len(S)):
        if S[i] == '1':
            break
    for j in range(i,len(S)):
        if S[j] == '0':
            break
    if j >= len(S)-1:
        ans = 0
        return ans
    if S[j+1] == '0':
        ans = solve(S[j + 2:])
        return ans
    else:
        if j+2 < len(S)-1 and S[j+2] == '0':
            a= solve(S[j+1:])
            b = j-i+solve(S[j+2:])
            ans = max(a, b)
        else:
            a = solve(S[j+1:])
            b = 1 + solve(S[i:j - 1] + '010' + S[j + 2:])
            ans = max(a, b)
        return ans

print(solve(S))

Submission status: Runtime Error

Solution code:
# D - 101 to 010
n = int(input())
s = input()

ss = s
ss.find("1011")
"1011" in ss
cnt=0
while "1011" in ss:
    #print(ss)
    i = ss.find("1011")
    j = i+2
    while ss[j]=="1" and j<n:
        #print(j)
        j+=1
    cnt+=j-i-2
    ss = ss[:i] + "0"*(j-i-3)+"010" + ss[j:]

#ss = s
#cnt=0
while "101" in ss:
    #print("F")
    m= len(ss)
    i = 0
    while i <= m-3:
        if ss[i:i+3]=="101":
            j = 0
            ss = ss[:i] + "010" + ss[i+3+j:]
            #print(ss)
            cnt+=1
            i+=3
            #print(cnt)
        else:
            i+=1
print(cnt)

Submission status: Runtime Error

Solution code:
N = map(int,input())
S = input()
dic = {}
def solve(S):
    if len(S)<3:
        return 0
    if len(S) == 3:
        if S == '101':
            return 1
        else:
            return 0
    if S in dic:
        return dic[S]
    for i in range(len(S)):
        if S[i] == '1':
            break
    for j in range(i,len(S)):
        if S[j] == '0':
            break
    if j >= len(S)-1:
        ans = 0
        dic[S] = ans
        return ans
    if S[j+1] == '0':
        ans = solve(S[j + 2:])
        dic[S] = ans
        return ans
    else:
        if j+2 < len(S)-1 and S[j+2] == '0':
            a= solve(S[j+1:])
            b = j-i+solve(S[j+2:])
            ans = max(a, b)
            dic[S] = ans
        else:
            a = solve(S[j+1:])
            b = 1 + solve(S[i:j - 1] + '010' + S[j + 2:])
            ans = max(a, b)
            dic[S] = ans
        return ans

print(solve(S))

Submission status: Runtime Error

Solution code:
N = map(int,input())
S = input()
dic = {}
def solve(S):
    if len(S)<3:
        return 0
    if len(S) == 3:
        if S == '101':
            return 1
        else:
            return 0
    if S in dic:
        return dic[S]
    for i in range(len(S)):
        if S[i] == '1':
            break
    for j in range(i,len(S)):
        if S[j] == '0':
            break
    if j >= len(S)-1:
        ans = 0
        dic[S] = ans
        return ans
    if S[j+1] == '0':
        ans = solve(S[j + 2:])
        dic[S] = ans
        return ans
    else:
        a= solve(S[j+1:])
        b = 1+solve(S[i:j-1]+'010'+S[j+2:])
        ans = max(a, b)
        dic[S] = ans
        return ans

print(solve(S))

Submission status: Runtime Error

Solution code:
def count_ops(s, ops = 0):
  if "101" in s:
    return count_ops(s.replace("101", "010", 1), ops + 1)
  return ops

N = int(input())
s = input()

print(count_ops(s, 0))