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: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().rstrip().split()
def S(): return sys.stdin.readline().rstrip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
s = list(S())
ans = 0
one_cnt = 0
z_cnt = 0
dp = []
flg = 0
for i in range(n):
    if s[i] == "1":
        one_cnt += 1
        z_cnt = 0
        if flg:
            if dp:
                ans += max(dp)
                dp = []
            flg = 0
        if i == n - 1 or s[i + 1] == "0":
            # 手付かず、左端だけ消した、右端だけ残す、全て消した。
            if dp:
                if one_cnt > 2:
                    ndp = [max(dp), max(dp[0] + pre, dp[1] + pre - 1, dp[2] + 1),
                           max(dp[0], dp[1], dp[2]) + one_cnt - 1,
                           max(dp[0], dp[1], dp[2]) + one_cnt]
                elif one_cnt == 2:
                    ndp = [max(dp), max(dp[0] + pre, dp[1] + pre - 1, dp[2] + 1),
                           max(dp[0] + pre, dp[1] + pre - 1, dp[2] + 1),
                           max(dp[0], dp[1], dp[2]) + 2]
                else:
                    ndp = [-INF, -INF, max(dp),
                           max(dp[0] + pre, dp[1] + pre - pre, dp[2] + 1)]
                dp = ndp
            else:
                dp = [0, -INF, -INF, -INF]
            pre = one_cnt
    else:
        z_cnt += 1
        one_cnt = 0
        if z_cnt >= 2:
            flg = 1

if dp:
    ans += max(dp)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().rstrip().split()
def S(): return sys.stdin.readline().rstrip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
s = list(S())
ans = 0
one_cnt = 0
z_cnt = 0
dp = []
flg = 0
for i in range(n):
    if s[i] == "1":
        one_cnt += 1
        z_cnt = 0
        if flg:
            if dp:
                ans += max(dp)
                dp = []
            flg = 0
        if i == n - 1 or s[i + 1] == "0":
            # 手付かず、左端だけ消した、右端だけ残す、全て消した。
            if dp:
                if one_cnt > 2:
                    ndp = [max(dp), max(dp[0] + pre, dp[1] + pre - 1, dp[2] + 1),
                           max(dp[0], dp[1], dp[2]) + one_cnt - 1,
                           max(dp[0], dp[1], dp[2]) + one_cnt]
                elif one_cnt == 2:
                    ndp = [max(dp), max(dp[0] + pre, dp[1] + pre - 1, dp[2] + 1),
                           max(dp[0] + pre, dp[1] + pre - 1, dp[2] + 1),
                           max(dp[0], dp[1], dp[2]) + 2]
                else:
                    ndp = [max(dp), -INF, -INF,
                           max(dp[0] + pre, dp[1] + 1, dp[2])]
                dp = ndp
            else:
                dp = [0, -INF, -INF, -INF]
            pre = one_cnt
    else:
        z_cnt += 1
        one_cnt = 0
        if z_cnt >= 2:
            flg = 1

if dp:
    ans += max(dp)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().rstrip().split()
def S(): return sys.stdin.readline().rstrip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
s = list(S())
ans = 0
one_cnt = 0
z_cnt = 0
dp = []
flg = 0
for i in range(n):
    if s[i] == "1":
        one_cnt += 1
        z_cnt = 0
        if flg:
            if dp:
                ans += max(dp)
                dp = []
            flg = 0
        if i == n - 1 or s[i + 1] == "0":
            # 手付かず、左端だけ消した、右端だけ残す、全て消した。
            if dp:
                if one_cnt >= 2:
                    ndp = [max(dp), max(dp[0] + pre, dp[1] + pre - 1, dp[2] + 1),
                           max(dp[0], dp[1], dp[2]) + one_cnt - 1,
                           max(dp[0], dp[1], dp[2]) + one_cnt]
                else:
                    ndp = [max(dp), -INF, -INF,
                           max(dp[0], dp[1], dp[2]) + 1]
                dp = ndp
            else:
                dp = [0, -INF, -INF, -INF]
            pre = one_cnt
    else:
        z_cnt += 1
        one_cnt = 0
        if z_cnt >= 2:
            flg = 1


if dp:
    ans += max(dp)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))


def solve():
    n = ni()
    s = ns().split('00')
    ans = 0
    for x in s:
        x = x.strip('0')
        if '1' not in x:
            continue
        t = list(map(len, x.split('0')))
        m = len(t)
        if m < 2:
            continue
        dp = [0]*m
        for i in range(m-1):
            dp[i+1] = max(dp[i-1] + t[i], dp[i])
        ans += dp[-1]
    print(ans)

    return

solve()

# T = ni()
# for _ in range(T):
#     solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3

import re
from pprint import pprint


n = input()
s = input()

sb = re.sub(r'^0+', '', s)
sb = re.sub(r'0+$', '', sb)
sb = re.sub(r'00+', '-', sb)
#pprint('---- sb = %s' % (sb, ))

tlist = sb.split('-')
#pprint('---- tlist = %s' % (tlist, ))

counts_list = []
for t in tlist:
    counts = []
    tt = t.split('0')
    for ttt in tt:
        counts.append(len(ttt))
    counts_list.append(counts)
#pprint('---- counts_list = %s' % (counts_list, ))

decr = 0

for counts in counts_list:
    len_counts = len(counts)
    for i in range(len_counts):
        if (i > 0 and i < len_counts-1 and counts[i] == 1):
            counts[i-1] += 1
            counts[i] -= 1
            counts[i+1] += 1
            decr += 1
#pprint('---- decr = %d counts_list = %s' % (decr, counts_list, ))

ans = 0

for counts in counts_list:
    totalc = 0
    minc = 1000000000
    len_counts = len(counts)
    for i in range(len_counts):
        count = counts[i]
        if i > 0 and i < len_counts-1:
            count -= 1
        #pprint('  -- count1 = %d' % (count, ))
        if count <= 0:
            continue
        #pprint('  -- count2 = %d' % (count, ))
        totalc += count
        minc = min(minc, count)
    ans += (totalc - minc)

print(ans - decr)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = input()
s += "0"

left = 0
right = 0
nextstart = 0
flag = False
Total = 0
i = 0
while i < N:
    if s[i] == "1":
        if not flag: left += 1
        else: right += 1
    else:
        if not flag:
            if left > 0:
                if s[i+1] == "1": flag = True
                else: left = 0
        else:
            if left >= right:
                Total += left
                left = right - 1
                right = 0
                if left == 0: flag = False
            else:
                flag = False
                Total += right
                left = 0
                right = 0
    i += 1
if left and right: Total += max(left, right)
print(Total)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n= int(input())
s = input()[:-1]
s = s.split("00")
ans = 0
for i in s:
        tmp = 0
        a = []
        for j in i:
                if j == "1":
                        tmp += 1
                else:
                        if tmp > 0:
                                a.append(tmp)
                        tmp = 0
        if tmp > 0:
                a.append(tmp)
        n = len(a)
        if n < 2:
                continue
        dp = [0] *n
        for i in range(n):
                dp[i] = max(dp[i-1] ,dp[i-2]+(a[i] if a[i-1] > 1 or i < 1 else 0),(dp[i-3] + a[i] if i>=3 else 0))
        ans += dp[-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
n= int(input())
s = input()
migi = 0
hidari = 0
flag = 0
ans = 0
for i in range(n):
#        print(migi,hidari,flag,i)
        if int(s[i]) == 1 and flag == 0:
                migi += 1
        elif int(s[i]) == 1 and flag == 1:
                hidari += 1
        elif int(s[i]) == 0 and flag == 0 and migi > 0:
                flag = 1
        elif int(s[i]) == 0 and flag == 1 and hidari > 0:
                ans += max(migi,hidari)
                migi = 0
                hidari = 0
                flag = 0
        else:
                migi = 0
                hidari = 0
                flag = 0               
if flag == 1 and hidari > 0:
        ans += max(migi,hidari)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
L = []
f = "00"
for c in S:
    if f == "00":
        if c=="0":
            continue
        elif c=="1":
            f = "1"
            L.append([1])
    elif f == "0":
        if c=="0":
            f = "00"
        elif c=="1":
            f = "1"
            L[-1].append(1)
    elif f == "1":
        if c=="0":
            f = "0"
        elif c=="1":
            L[-1][-1] += 1
ans = 0
for L1 in L:
    if len(L1) < 2:
        continue
    cnt = 0
    for i in range(2):
        c = 0
        for j in range(i, len(L1), 2):
            try:
                c += max(L1[j], L1[j+1])
            except:
                pass
        cnt = max(cnt, c)
    ans += cnt
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
res = []
count = 0
if s[0] == '1':one = 1
else:one = 0
temp = []
tar = 0

for i in range(1,n-1):
    if s[i-1:i+2] == '101':   # 碰到了101
        tar = 1
        temp.append(one)
        one = 0               # 任务重新计数
    else:
        if s[i] == '0' and tar == 1:    # 在任务中碰到了连续0，结束任务
            temp.append(one)
            res.append(temp.copy())
            temp = []
            tar = 0
        if s[i] == '1':one += 1
        else: one = 0


if s[-1] == '1':one+=1
if tar == 1:
    temp.append(one)
    res.append(temp.copy())

for x in res:
    temp = 0
    m = len(x)
    f = []
    if m == 2:temp = max(x[0],x[1])
    if m == 3:temp = max(x[0],x[1],x[2])
    if m == 4:temp = max(x[0]+x[2],x[3]+max(x[0],x[1]))
    if m >= 5:
        f.append([x[0],x[1]])
        f.append([x[1],max(x[2],x[0])])
        f.append([x[0]+x[2],x[3]+max(x[0],x[1])])
        for i in range(3,m-1):
            f.append([x[i] + max(f[i-2][0],f[i-3][0]),x[i+1] + max(f[i-2][0],f[i-2][1])])
        temp = max(f[-1][0],f[-1][1],f[-2][0])

    count += temp


print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
res = []
count = 0
if s[0] == '1':one = 1
else:one = 0
temp = []
tar = 0

for i in range(1,n-1):
    if s[i-1:i+2] == '101':   # 碰到了101
        tar = 1
        temp.append(one)
        one = 0               # 任务重新计数
    else:
        if s[i] == '0' and tar == 1:    # 在任务中碰到了连续0，结束任务
            temp.append(one)
            res.append(temp.copy())
            temp = []
            tar = 0
        if s[i] == '1':one += 1
        else: one = 0


if s[-1] == '1':one+=1
if tar == 1:
    temp.append(one)
    res.append(temp.copy())

for x in res:
    temp = 0
    m = len(x)
    f = []
    if m == 2:temp = max(x[0],x[1])
    if m == 3:temp = max(x[0],x[1],x[2])
    if m == 4:temp = max(x[0]+x[2],x[3]+max(x[0],x[1]))
    if m >= 5:
        f.append([x[0],x[1]])
        f.append([x[1],max(x[2],x[0])])
        f.append([x[0]+x[2],x[3]+max(x[0],x[1])])
        for i in range(3,m-1):
            f.append([x[i] + max(f[i-2][0],f[i-3][0]),x[i+1] + max(f[i-2][0],f[i-2][1])])
        temp = max(f[-1][0],f[-1][1],f[-2][0])

    count += temp


print(res,count)

Submission status: Wrong Answer

Solution code:
input()
l=input().split("00")
l.sort(reverse=True)
if "" in l:
    l=l[:l.index("")]
res=0
for s in l:
    ll=s.strip("0").split("0")
    if len(ll)>1:
        lens=[]
        for item in ll:
            lens.append(len(item))
        res+=sum(lens[:-2])-len(lens)+2+max(lens[-2]-1,lens[-1])
        seq=0
        for i in range(len(lens)):
            if lens[i]==1:
               seq+=1
            else:
               res+=max(0,seq-1)//2
               seq=0
        res+=max(0,seq-1)//2
print(res)

Submission status: Wrong Answer

Solution code:
input()
l=input().split("00")
l.sort(reverse=True)
if "" in l:
    l=l[:l.index("")]
res=0
for s in l:
    ll=s.strip("0").split("0")
    if len(ll)>1:
        lens=[]
        for item in ll:
            lens.append(len(item))
        res+=sum(lens[:-2])-len(lens)+2+max(lens[-2]-1,lens[-1])
        seq=0
        for i in range(len(lens)):
            if lens[i]==1:
               seq+=1
            else:
               res+=max(0,seq-1)//2
               seq=0
print(res)

Submission status: Wrong Answer

Solution code:
# coding: utf-8


def dp(a):
    d0 = 0
    d1 = max(a[0:2])
    for i in range(2, len(a)):
        nd = max(d1, d0 + max(a[i-1], a[i]))
        d0 = d1
        d1 = nd
    return d1


def solve(N, S, i):
    cnt = 0
    if S[i] == "0":
        while i < N and S[i] == "0":
            i += 1
        return 0, i
    cnt1s = []
    while True:
        cnt1 = 0
        while i < N and S[i] == "1":
            i += 1
            cnt1 += 1
        if cnt1 == 0:
            break
        cnt1s.append(cnt1)
        i += 1
    if len(cnt1s) <= 1:
        cnt = 0
    elif len(cnt1s) <= 3:
        cnt = max(cnt1s)
    else:
        cnt = dp(cnt1s)
    return cnt, i


def main():
    N = int(input())
    S = input().strip()
    ans = 0
    i = 0
    while i < N:
        a, i = solve(N, S, i)
        ans += a
    return ans


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
x = raw_input().strip()
def solve(b):
    if len(b) == 1:
        return 0
    m = 1001001001
    dp = [-m, -m, -m, 0]
    p = 0
    for x in b:
        ndp = [-m] * 4
        t = -m
        for j in (1, 2, 3):
            if dp[j] < dp[j-1]:
                dp[j] = dp[j-1]
        ndp[0] = dp[3]
        g = max(dp[0] + p, dp[1] + p - 1, dp[2] + 1)
        if x == 1:
            ndp[3] = g
        else:
            ndp[1] = g
            ndp[2] = dp[2] + x - 1
            ndp[3] = dp[2] + x
        dp = ndp
    return max(dp)
a = []
p = ans = 0
c = x[0] == '1'
for i in xrange(1, n):
    if x[i-1] == x[i] == '0':
        if p < i - 1:
            ans += solve(a)
        a = []
        c = 0
        p = i
    elif x[i-1] != x[i] == '0':
        a.append(c)
        c = 0
    else:
        c += 1
if p < n - 1:
    if c:
        a.append(c)
    ans += solve(a)
print ans

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import re
INF = int(1e9)

N = int(input())
S = input()
ans = 0
for s in re.split('00+', S.strip('0')):
    seq = list(map(len, s.split('0')))
    if len(seq) <= 1:
        continue
    dp = [[-INF] * 3 for _ in seq]
    dp[0][0] = 0
    dp[0][1] = seq[0]  # right

    def set_max(i, j, x):
        dp[i][j] = max(dp[i][j], x)

    for i in range(len(seq)):
        if i == 0:
            continue
        set_max(i, 0, max(dp[i - 1]))
        if i + 1 < len(seq):
            set_max(i, 1, max(dp[i - 1][0], dp[i - 1][2]) + seq[i])
        if i > 0:
            if i > 1:
                set_max(i, 2, max(dp[i - 2][0], dp[i - 2][2]) + seq[i])
            else:
                set_max(i, 2, seq[i])
    ans += max(dp[-1])
print(ans)

Submission status: Wrong Answer

Solution code:
import re


def update(d, k, v):
    if k in d:
        d[k] = max(d[k], v)
    else:
        d[k] = v


rep_zeros = re.compile('0{2,}')

n = input()
s = input().strip('0')
ans = 0

for ps in re.split(rep_zeros, s):
    fs = ps.split('0')
    lfs = len(fs)
    if lfs == 1:
        continue
    ls = list(map(len, fs))
    pl = {ls[0]: 0}
    for cc in ls[1:]:
        nl = {}
        for pc, a in pl.items():
            if pc == 0:
                update(nl, cc, a)
                continue
            if cc == 1:
                update(nl, 0, a + pc)
            else:
                update(nl, 0, a + cc)
                update(nl, 1, a + cc - 1)
                if cc > 1:
                    update(nl, cc - 1, a + pc)
        pl = nl
    ans += max(pl.values())

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
import re
INF = int(1e9)

N = int(input())
S = input()
ans = 0
for s in re.split('00+', S.strip('0')):
    seq = list(map(len, s.split('0')))
    if len(seq) <= 1:
        continue
    dp = [-INF] * (len(seq) + 2)
    dp[0] = 0

    def set_max(i, x):
        dp[i] = max(dp[i], x)

    for i in range(len(seq)):
        if i > 0:
            set_max(i + 1, dp[i - 1])
            set_max(i + 2, dp[i - 1] + seq[i])
        else:
            set_max(i + 1, 0)
            set_max(i + 2, seq[i])
        if i > 1:
            set_max(i + 1, dp[i - 2] + seq[i])
    ans += max(dp[-1], dp[-2])
print(ans)

Submission status: Wrong Answer

Solution code:
import re

rep_zeros = re.compile('0{2,}')
rep_ioioi = re.compile('((?:10)+1)')

n = input()
s = input().strip('0')
ans = 0

for ps in re.split(rep_zeros, s):
    fs = ps.split('0')
    lfs = len(fs)
    if lfs == 1:
        continue
    ls = list(map(len, fs))
    pl = {(ls[0], 0)}
    for cc in ls[1:]:
        nl = set()
        for pc, a in pl:
            if pc == 0:
                nl.add((cc, a))
                continue
            if cc == 1:
                nl.add((0, a + pc))
            else:
                nl.add((0, a + cc))
                nl.add((1, a + cc - 1))
                if cc > 1:
                    nl.add((cc - 1, a + pc))
        pl = nl
    ans += max(a for c, a in pl)

print(ans)

Submission status: Wrong Answer

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 + 1):
            try:
                if s[i] == '0':
                    if i < N:
                        if s[i + 1] == '1':
                            countR = 0
                    break
            except IndexError:
                break
            countR += 1

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

print(count)

Submission status: Wrong Answer

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 + 1):
            try:
                if s[i] == '0':
                    if i < N:
                        if s[i + 1] == '1':
                            print(0)
                            countR = 0
                    break
            except IndexError:
                break
            countR += 1

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

print(count)

Submission status: Wrong Answer

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 + 1):
            try:
                if s[i] == '0':
                    if i < N:
                        if s[i + 1] == '1':
                            print(0)
                            countR = 0
                    break
            except IndexError:
                break
            countR += 1

        print(countL, countR)
        if countR <= countL:
            count += countL
            s[idx + 2] = '0'
            idx += 2
        else:
            count += countR
            idx += countR + 2
    idx += 1

print(count)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
x = raw_input().strip()
def solve(b):
    if len(b) == 1:
        return 0
    t = []
    p = b[0]
    r = 0
    for x in b[1:]:
        if p == 1:
            t.append(p)
            p = x
        elif x != 1:
            t.append(p - 1)
            t.append(1)
            r += 1
            p = x - 1
        else:
            t.append(p)
            p = x
    t.append(p)
    m = 1001001001
    dp = [0, -m]
    p = -m
    for x in t:
        b = dp[1] + p + x - 1
        if b < dp[0]:
            b = dp[0]
        dp = [b, dp[0]]
        p = x
    return r + max(dp[0], dp[1])
a = []
p = ans = 0
c = x[0] == '1'
for i in xrange(1, n):
    if x[i-1] == x[i] == '0':
        if p < i - 1:
            ans += solve(a)
        a = []
        c = 0
        p = i
    elif x[i-1] != x[i] == '0':
        a.append(c)
        c = 0
    else:
        c += 1
if p < n - 1:
    if c:
        a.append(c)
    ans += solve(a)
print ans

Submission status: Wrong Answer

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

i = 0
count = 0

while i < N - 2:
    if S[i:i + 3] == '000':
        i += 3
    elif S[i:i + 3] == '001':
        i += 2
    elif S[i:i + 3] == '010':
        i += 1
    elif S[i:i + 3] == '011':
        i += 2
    elif S[i:i + 3] == '100':
        i += 3
    elif S[i:i + 3] == '101':
        S = S[:i] + '010' + S[i + 3:]
        count += 1

        right = 0
        for c in S[i + 3:]:
            if c == '1':
                right += 1
            else:
                break

        left = 0
        for c in S[:i]:
            if c == '1':
                left += 1
            else:
                break

        if right < left and i > 0:
            i -= 1
        else:
            i += 1


    elif S[i:i + 3] == '110':
        i += 1
    elif S[i:i + 3] == '111':
        i += 2


print(count)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
s=input()
x=len(s.split("101"))-1
y=s.split("0")
j=-1
yy=0
for i in range(len(y)):
    if (y[i]=="" or i==len(y)-1) and j!=-1:

        ii=i
        if i==len(y)-1 and y[i]!="":
            ii+=1
        x1=0
        x2=0
        for k in range(ii-j):
          if k<ii-j-1:
            if k%2==0:
             x1+=max(len(y[j:ii][k]),len(y[j:ii][k+1]))
            else:
             x2+=max(len(y[j:ii][k]),len(y[j:ii][k+1]))
        if ii-j>1:
         yy+=max(x1,x2)
        
        j=-1

    if len(y[i])>0 and j==-1:
        j=i
print(yy)

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
x = raw_input().strip()
def solve(b):
    if len(b) == 1:
        return 0
    t = []
    p = b[0]
    r = 0
    for x in b[1:]:
        if p == 1:
            t.append(p)
            p = x
        elif x != 1:
            t.append(p - 1)
            t.append(1)
            r += 1
            p = x - 1
        else:
            t.append(p)
            p = x
    t.append(p)
    m = 1001001001
    dp = [0, -m]
    p = -m
    for x in t:
        b = dp[1] + p + x - 1
        if b < dp[0]:
            b = dp[0]
        c = dp[1]
        if c < dp[0]:
            c = dp[0]
        dp = [b, c]
        p = x
    return r + max(dp[0], dp[1])
a = []
p = ans = 0
c = x[0] == '1'
for i in xrange(1, n):
    if x[i-1] == x[i] == '0':
        if p < i - 1:
            ans += solve(a)
        a = []
        c = 0
        p = i
    elif x[i-1] != x[i] == '0':
        a.append(c)
        c = 0
    else:
        c += 1
if p < n - 1:
    if c:
        a.append(c)
    ans += solve(a)
print ans

Submission status: Wrong Answer

Solution code:
raw_input()

prev1 = '0'
prev2 = '0'
active = False
ones = 0
a = []
now = []
for c in raw_input():
  s = prev2 + prev1 + c
  if active:
    if s == '101':
      now.append(ones)
      ones = 0
    elif s == '100':
      active = False
      now.append(ones)
      a.append(now)
    if c == '1':
      ones += 1
  else:
    if prev2 == '1':
      ones += 1
    else:
      ones = 0
    if s == '101':
      now = [ones]
      active = True
      ones = 1
  prev2 = prev1
  prev1 = c
if active:
  now.append(ones)
  a.append(now)

print sum(sum(sorted(xs)[-int(len(xs)/2):]) for xs in a)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python

import re

def snuke(s):
    t = map(len, s.split('0'))
    if t == [1, 1, 1]:
        return 1
    return max(sum(t[::2]), sum(t[1::2]))

def main():
    N = int(raw_input());
    s = raw_input();

    #ss = re.split(r'00+', s)
    ss = re.findall('1+(?:01+)+', s)

    res = 0
    for s_ in ss:
        res += snuke(s_)

    print res

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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 i + 1 < N:
                    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: Wrong Answer

Solution code:
def get_score(q):
    score = 0
    l = len(q)
    if l < 2:
        return 0
    if l % 2 == 0:
        for i in range(0,l,2):
            score += max(q[i], q[i+1])
    else:
        m = float('inf')
        for i in range(0, l, 2):
            m = min(m, q[i])
        score = sum(q) - m - l//2
    return score


n = int(input())
s = input()
parts = list(filter(lambda x:x, map(lambda x:x.strip('0'), s.split('00'))))
total = 0
for part in parts:
    p = []
    cnt = 0
    for c in part:
        if c == '1':
            cnt += 1
        else:
            p.append(cnt)
            cnt = 0
    if cnt > 0:
        p.append(cnt)
    q = []
    L = len(p)
    for i in range(L):
        if p[i] > 1:
            if i>0 and q and q[-1] > 1:
                if q[-1] == 1:
                    pass
                else:
                    q[-1] -= 1
                    q.append(1)
                q.append(p[i]-1)
                total += 1
            else:
                q.append(p[i])
        else:
            q.append(p[i])
    total += get_score(q)
    
print(total)