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 0 s and 1 s.
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

Solution (Python):
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 = [-INF, -INF, max(dp),
                           max(dp[0] + pre, dp[1] + pre - 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)

Solution (Python):
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 - 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)

Solution (Python):
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()
    l = list()
    for i in range(1, n-1):
        if s[i] == '0':
            li = ri = i
            while li > 0 and s[li - 1] == '1':
                li -= 1
            while ri + 1 < n and s[ri + 1] == '1':
                ri += 1
            if li != i != ri:
                l.append((i-1, i+2))
                for j in range(li, i-1):
                    l.append((j, i+2))
                for j in range(i+2, ri+1):
                    l.append((i-1, j+1))
    l.sort()
    l.sort(key = lambda x: x[1])
    
    m = len(l)
    dp = [0]*(n+1)
    idx = 0
    for i in range(n+1):
        dp[i] = max(dp[i], dp[i-1])
        while idx < m and l[idx][0] == i:
            x, y = l[idx]
            dp[y] = max(dp[y], dp[x] + y - x - 2)
            idx += 1
    
    print(dp[n])
    return

solve()

Solution (Python):
import sys
readline = sys.stdin.readline

INF = 10**9+7
class Segtree:
    def __init__(self, A, intv, initialize = True, segf = max):
        self.N = len(A)
        self.N0 = 2**(self.N-1).bit_length()
        self.intv = intv
        self.segf = segf
        if initialize:
            self.data = [intv]*self.N0 + A + [intv]*(self.N0 - self.N)
            for i in range(self.N0-1, 0, -1):
                self.data[i] = self.segf(self.data[2*i], self.data[2*i+1]) 
        else:
            self.data = [intv]*(2*self.N0)
        
    def update(self, k, x):
        k += self.N0
        self.data[k] = x
        while k > 0 :
            k = k >> 1
            self.data[k] = self.segf(self.data[2*k], self.data[2*k+1])
    
    def query(self, l, r):
        L, R = l+self.N0, r+self.N0
        s = self.intv
        while L < R:
            if R & 1:
                R -= 1
                s = self.segf(s, self.data[R])
            if L & 1:
                s = self.segf(s, self.data[L])
                L += 1
            L >>= 1
            R >>= 1
        return s
    
    def binsearch(self, l, r, check, reverse = False):
        L, R = l+self.N0, r+self.N0
        SL, SR = [], []
        while L < R:
            if R & 1:
                R -= 1
                SR.append(R)
            if L & 1:
                SL.append(L)
                L += 1
            L >>= 1
            R >>= 1
        
        if reverse:
            for idx in (SR + SL[::-1]):
                if check(self.data[idx]):
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.data[2*idx+1]):
                    idx = 2*idx + 1
                else:
                    idx = 2*idx
            return idx - self.N0
        else:
            pre = self.data[l+self.N0]
            for idx in (SL + SR[::-1]):
                if not check(self.segf(pre, self.data[idx])):
                    pre = self.segf(pre, self.data[idx])
                else:
                    break
            else:
                return -1
            while idx < self.N0:
                if check(self.segf(pre, self.data[2*idx])):
                    idx = 2*idx
                else:
                    pre = self.segf(pre, self.data[2*idx])
                    idx = 2*idx + 1
            return idx - self.N0

N = int(readline())
S = [0]+list(map(int, readline().strip()))

leftone = [None]*(N+1)
left = None
for i in range(N+1):
    if S[i] == 1:
        if left is None:
            left = i
        leftone[i] = left
    else:
        left = None

T = Segtree([None]*(N+1), -INF, initialize = False)
dp = [0]*(N+1)
T.update(0, 0)
T.update(1, -1)
T.update(2, -2)
for i in range(3, N+1):
    res = dp[i-1]
    if S[i] == 1:
        if S[i-1] == 0 and S[i-2] == 1:
            left = leftone[i-2]
            res = max(res, T.query(left-1, i-2)+i-2)
        if leftone[i] > 2 and S[leftone[i]-1] == 0 and S[leftone[i]-2] == 1:
            res = max(res, dp[leftone[i]-3] + i-leftone[i]+2-1)
    dp[i] = res
    T.update(i, dp[i]-i)
print(dp[-1])

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
S = readline().rstrip().decode('utf-8')

def solve_partial(S):
    INF = 10**18
    """
    ・Sは1から始まり、1で終わる
    ・Sは00を含まない
    ・したがって、Sは1,01に分解可能
    ・残る最小個数を調べるdp。これは、1, 101111,111101 の3種を数えることと同じ
    ・a, b0cccc, dddd0e として、「現在の1がどれであるか -> 最小個数」でdp
    ・個数はa,b,eのときに数える
    """
    S = S.replace('01','2')
    a,b,c,d,e = 1,1,INF,0,INF
    for x in S[1:]:
        ace = min(a,c,e)
        if x == '1':
            a2 = ace+1
            c2 = c
            d2 = ace if ace<d else d
            e2 = INF
        else:
            a2 = ace+1
            c2 = a
            d2 = ace
            e2 = d+1
        a,c,d,e = a2,c2,d2,e2
    return len(S)-min(a,c,e)

answer = 0
for x in S.split('00'):
    x = x.strip('0')
    if x:
        answer += solve_partial(x)

print(answer)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
S = readline().rstrip().decode('utf-8')

def solve_partial(S):
    INF = 10**18
    """
    ・Sは1から始まり、1で終わる
    ・Sは00を含まない
    ・したがって、Sは1,01に分解可能
    ・残る最小個数を調べるdp。これは、1, 101111,111101 の3種を数えることと同じ
    ・a, b0cccc, dddd0e として、「現在の1がどれであるか -> 最小個数」でdp
    ・個数はa,b,eのときに数える
    """
    S = S.replace('01','2')
    a,b,c,d,e = 1,1,INF,0,INF
    for x in S[1:]:
        if x == '1':
            a2 = min(a,c,e)+1
            c2 = c
            d2 = min(a,c,d,e)
            e2 = INF
        else:
            a2 = min(a,c,e)+1
            c2 = a
            d2 = min(a,c,e)
            e2 = d+1
        a,c,d,e = a2,c2,d2,e2
    return len(S)-min(a,c,e)

answer = 0
for x in S.split('00'):
    x = x.strip('0')
    if x:
        answer += solve_partial(x)

print(answer)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
S = readline().rstrip().decode('utf-8')

def solve_partial(S):
    INF = 10**18
    """
    ・Sは1から始まり、1で終わる
    ・Sは00を含まない
    ・したがって、Sは1,01に分解可能
    ・残る最小個数を調べるdp。これは、1, 101111,111101 の3種を数えることと同じ
    ・a, b0cccc, dddd0e として、「現在の1がどれであるか -> 最小個数」でdp
    ・個数はa,b,eのときに数える
    """
    S = S.replace('01','2')
    a,b,c,d,e = 1,1,INF,0,INF
    for x in S[1:]:
        if x == '1':
            a2 = min(a,c,e)+1
            b2 = min(a,c,e)+1
            c2 = c
            d2 = min(a,c,d,e)
            e2 = INF
        else:
            a2 = min(a,c,e)+1
            b2 = min(a,c,e)+1
            c2 = b
            d2 = min(a,c,e)
            e2 = d+1
        a,b,c,d,e = a2,b2,c2,d2,e2
    return len(S)-min(a,c,e)

answer = 0
for x in S.split('00'):
    x = x.strip('0')
    if x:
        answer += solve_partial(x)

print(answer)

Solution (Python):


import re


n = input()
s = input()


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

tlist = sb.split('-')
counts_list = []

for t in tlist:
    counts = []
    tt = t.split('0')
    for ttt in tt:
        counts.append(len(ttt))
    counts_list.append(counts)

ans = 0

for counts in counts_list:

    len_counts = len(counts)
    sum_all = sum_decr = sum_one = sum_zero = 0

    for i in range(len_counts - 1):

        new_sum_all = new_sum_decr = new_sum_one = new_sum_zero = 0

        
        new_sum_all = max(sum_all, sum_decr, sum_one, sum_zero)

        
        new_sum_decr = max(new_sum_decr, sum_all + counts[i])
        new_sum_decr = max(new_sum_decr, sum_decr + counts[i] - 1)
        new_sum_decr = max(new_sum_decr, sum_one + 1)

        
        new_sum_one = max(new_sum_one, sum_all + counts[i+1] - 1)
        if (counts[i] >= 2):
            new_sum_one = max(new_sum_one, sum_decr + counts[i+1] - 1)
        new_sum_one = max(new_sum_one, sum_one + counts[i+1] - 1)

        
        new_sum_zero = max(new_sum_zero, sum_all + counts[i+1])
        if (counts[i] >= 2):
            new_sum_zero = max(new_sum_zero, sum_decr + counts[i+1])
        new_sum_zero = max(new_sum_zero, sum_one + counts[i+1])

        sum_all = new_sum_all
        sum_decr = new_sum_decr
        sum_one = new_sum_one
        sum_zero = new_sum_zero

    ans += max(sum_all, sum_decr, sum_one, sum_zero)


print(ans)

Solution (Python):


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)


tlist = sb.split('-')


counts_list = []
for t in tlist:
    counts = []
    tt = t.split('0')
    for ttt in tt:
        counts.append(len(ttt))
    counts_list.append(counts)


ans = 0

for counts in counts_list:

    len_counts = len(counts)
    sum_all = sum_decr = sum_one = sum_zero = 0

    for i in range(len_counts - 1):

        new_sum_all = new_sum_decr = new_sum_one = new_sum_zero = 0

        
        new_sum_all = max(sum_all, sum_decr, sum_one, sum_zero)

        
        new_sum_decr = max(new_sum_decr, sum_all + counts[i])
        new_sum_decr = max(new_sum_decr, sum_decr + counts[i] - 1)
        new_sum_decr = max(new_sum_decr, sum_one + 1)

        
        new_sum_one = max(new_sum_one, sum_all + counts[i+1] - 1)
        if (counts[i] >= 2):
            new_sum_one = max(new_sum_one, sum_decr + counts[i+1] - 1)
        new_sum_one = max(new_sum_one, sum_one + counts[i+1] - 1)

        
        new_sum_zero = max(new_sum_zero, sum_all + counts[i+1])
        if (counts[i] >= 2):
            new_sum_zero = max(new_sum_zero, sum_decr + counts[i+1])
        new_sum_zero = max(new_sum_zero, sum_one + counts[i+1])

        sum_all = new_sum_all
        sum_decr = new_sum_decr
        sum_one = new_sum_one
        sum_zero = new_sum_zero

        
        

    

    ans += max(sum_all, sum_decr, sum_one, sum_zero)


print(ans)

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


s = s[s.find('1'):] 
while s.count('000'):
    s = s.replace('000', '00')
cs = list(map(len, s.split('0')))



dp = [[-float('inf')] * 4 for i in range(len(cs))]
dp[0][0] = 0
for i in range(1, len(cs)):
    if cs[i] == 0:
        dp[i][3] = max(dp[i - 1])
        continue

    
    dp[i][0] = max(dp[i - 1])
    
    dp[i][3] = max(dp[i - 1][0] + max(cs[i - 1], cs[i]), \
                   dp[i - 1][1] + max(cs[i - 1] - 1, cs[i]), \
                   dp[i - 1][2] + cs[i], \
                   dp[i - 1][3])
    if cs[i] >= 2:
        
        dp[i][1] = max(dp[i - 1][0] + cs[i - 1], \
                       dp[i - 1][1] + cs[i - 1] - 1, \
                       dp[i - 1][2] + 1, \
                       dp[i - 1][3])
        
        dp[i][2] = max(dp[i - 1][0] + max(cs[i - 1], cs[i] - 1), \
                       dp[i - 1][1] + max(cs[i - 1] - 1, cs[i] - 1), \
                       dp[i - 1][2] + cs[i] - 1, \
                       dp[i - 1][3])

print(max(dp[-1]))

Solution (Python):

N = int(input())
s = list(map(int, list(input())))

        
def calc_add(temp_list):
    if len(temp_list) <= 1:
        return 0
    else:
        res = 0
        
        dp_l = [0]*len(temp_list)
        
        dp_r = [0]*len(temp_list)
        
        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:
                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 = []
res = 0

for i in range(N):
    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 == N - 1:
        res += calc_add(temp_list)
        
print(res)

Solution (Python):
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():
            update(nl, cc, a)
            if pc == 0:
                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)

Solution (Python):
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
        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
        p = x
    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

Solution (Python):
N = int(input())
S = [x == '1' for x in input()]
S.append(0)
ans = 0
now = 0

def dp(arr):
    L = len(arr)
    if L == 1:
        return 0
    dp = [[0] * 4 for _ in range(L)]
    dp[0] = [-1, arr[0], -1, 0]
    ret = max(dp[0])
    for i in range(L - 1):
        dp[i + 1][0] = dp[i][3] + arr[i + 1]
        if dp[i][0] != -1 and arr[i] > 1:
            dp[i + 1][0] = max(dp[i + 1][0], dp[i][0] + arr[i + 1] - 1)
        if dp[i][2] != -1 and arr[i] > 1:
            dp[i + 1][0] = max(dp[i + 1][0], dp[i][2] + arr[i + 1])
        dp[i + 1][1] = dp[i][3] + arr[i + 1]
        if dp[i][0] != -1 and i != L - 2:
            dp[i + 1][1] = max(dp[i + 1][1], dp[i][0] + arr[i + 1])
        if dp[i][1] != -1 and arr[i + 1] > 1 and i != L - 2:
            dp[i + 1][1] = max(dp[i + 1][1], dp[i][1] + arr[i + 1] - 1)
        if dp[i][2] != -1 and i != L - 2:
            dp[i + 1][1] = max(dp[i + 1][1], dp[i][2] + arr[i + 1])
        dp[i + 1][2] = dp[i][1]
        dp[i + 1][3] = max([dp[i][0], dp[i][3], dp[i][2]])
        ret = max(ret, max(dp[i + 1]))
    
    return ret


while now < N:
    if S[now] == 1:
        left = now
        cnt_1 = 1
        prev = 1
        now += 1
        arr = []
        while now < N and (prev != 0 or S[now] != 0):
            if S[now] == 0:
                arr.append(cnt_1)
                cnt_1 = 0
            else:
                cnt_1 += 1
            prev = S[now]
            now += 1
        if cnt_1:
            arr.append(cnt_1)
        if now == N:
            if prev == 0:
                right = N - 2
            else:
                right = N - 1
        else:
            right = now - 2
        
        ans += dp(arr)
        while now < N and S[now] == 0:
            now += 1
    else:
        now += 1
print(ans)

Solution (Python):
n = int(input())
s = input()

ss = []
cnt = 0
for c in s:
    if c == '1':
        cnt += 1
    else:
        if cnt > 0:
            ss.append(cnt)
            cnt = 0
        ss.append(cnt)
if cnt > 0:
    ss.append(cnt)
def solve(seq):
    l = len(seq)
    if not seq:
        return 0
    while seq[-1] == 0:
        seq.pop()

    
    dp = [[0]*3 for i in range(l+1)]
    
    for i in range(l):
        
        nxt = max(dp[i])
        dp[i+1][0] = max(dp[i+1][0], nxt)
        if i+1 < l:
            if seq[i] >= 2:
                
                dp[i+1][1] = max(dp[i+1][1], dp[i][0] + seq[i])
            if seq[i] >= 2 and seq[i+1] >= 2:
                
                dp[i+1][1] = max(dp[i+1][1], dp[i][1] + seq[i]-1)
                
                dp[i+1][1] = max(dp[i+1][1], dp[i][2] + 1)
                
                dp[i+1][2] = max(dp[i+1][2], dp[i][1] + max(seq[i], seq[i+1])-1)
                
                dp[i+1][2] = max(dp[i+1][2], dp[i][2] + seq[i+1]-1)
            if seq[i+1] >= 2:
                
                dp[i+1][2] = max(dp[i+1][2], dp[i][0] + max(seq[i], seq[i+1]-1))
        if i+2 <= l:
            dp[i+2][0] = max(dp[i+2][0], dp[i][0] + max(seq[i], seq[i+1]))
            if seq[i] >= 2:
                dp[i+2][0] = max(dp[i+2][0], dp[i][1] + max(seq[i]-1, seq[i+1]))
                dp[i+2][0] = max(dp[i+2][0], dp[i][2] + seq[i+1])
        
    
    
    return max(max(e) for e in dp)
idx = 0
cur = []
ans = 0
while idx < len(ss):
    if idx+1 < len(ss) and ss[idx] == ss[idx+1] == 0:
        idx += 2
        ans += solve(cur)
        cur = []
    elif not cur and ss[idx] == 0:
        idx += 1
    else:
        if ss[idx]:
            cur.append(ss[idx])
        idx += 1
if cur:
    ans += solve(cur)
print(ans)