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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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]))
    #print(*dp, sep='\n')
    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
        #print('#', arr)
        ans += dp(arr)
        while now < N and S[now] == 0:
            now += 1
    else:
        now += 1
print(ans)

Submission status: Accepted

Solution code:
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]*(l+1)
    dp = [[0]*3 for i in range(l+1)]
    #print(seq)
    for i in range(l):
        # 0 -> 0, 1 -> 0, 2 -> 0
        nxt = max(dp[i])
        dp[i+1][0] = max(dp[i+1][0], nxt)
        if i+1 < l:
            if seq[i] >= 2:
                # 0 -> 1
                dp[i+1][1] = max(dp[i+1][1], dp[i][0] + seq[i])
            if seq[i] >= 2 and seq[i+1] >= 2:
                # 1 -> 1
                dp[i+1][1] = max(dp[i+1][1], dp[i][1] + seq[i]-1)
                # 2 -> 1
                dp[i+1][1] = max(dp[i+1][1], dp[i][2] + 1)
                # 1 -> 2
                dp[i+1][2] = max(dp[i+1][2], dp[i][1] + max(seq[i], seq[i+1])-1)
                # 2 -> 2
                dp[i+1][2] = max(dp[i+1][2], dp[i][2] + seq[i+1]-1)
            if seq[i+1] >= 2:
                # 0 -> 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])
        #print(dp[i])
    #print(dp[l])
    #print("-")
    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)

Submission status: Runtime Error

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:
            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: Runtime Error

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:
            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
                print(dp)
            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: Runtime Error

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:
            ans += max(dp)
            dp = []
            flg = 0
        if i == n - 1 or s[i + 1] == "0":
            # 手付かず、左端だけ消した、右端だけ残す、全て消した。
            if not dp:
                dp = [0, -INF, -INF, -INF]
            else:
                if one_cnt >= 2:
                    ndp = [max(dp), max(dp[1] + pre - 1, dp[2] + 1, dp[3]),
                           max(dp[0] + one_cnt - 1, dp[1] + one_cnt, 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
            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: Runtime Error

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:
            ans += max(dp)
            dp = []
            flg = 0
        if i == n - 1 or s[i + 1] == "0":
            # 手付かず、左端だけ消した、右端だけ残す、全て消した。
            if not dp:
                dp = [0, -INF, 0, -INF]
            else:
                if one_cnt >= 2:
                    ndp = [max(dp), max(dp[1] + pre - 1, dp[2] + 1, dp[3]),
                           max(dp[0] + one_cnt - 1, dp[1] + one_cnt, 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
            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: Runtime Error

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:
            ans += max(dp[-1])
            dp = []
            flg = 0

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

if dp:
    ans += max(dp[-1])

if ans == -INF:
    print(0)
else:
    print(ans)

Submission status: Runtime Error

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:
            ans += max(dp[-1])
            dp = []
            flg = 0

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

if dp:
    ans += max(dp[-1])

if ans == -INF:
    print(0)
else:
    print(ans)

Submission status: Runtime Error

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:
            ans += max(dp[-1])
            dp = []
            flg = 0

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

if dp:
    ans += max(dp[-1])
print(ans)

Submission status: Runtime Error

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]+f[0])
        for i in range(3,m-1):
            f.append(x[i] + max(f[i-2][0],f[i-3][0]))
            f.append(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: Runtime Error

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]+f[0])
        for i in range(3,m-1):
            f.append(x[i] + max(f[i-2][0],f[i-3][0]))
            f.append(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: Runtime Error

Solution code:
N,M,K = map(int,input().split())
if K ==0:
    print("Yes")
    exit()
for n in range(N):
    for m in range(M):
        if n*m+(N-n)*(M-m) == K:
            print("Yes")
            exit()
print("No")

Submission status: Runtime Error

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

        
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:
                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 == M:
        res += calc_add(temp_list)
        
print(res)

Submission status: Runtime Error

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

res = 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:
                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:
# 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):
    return sum(temp_list)
def calc_add_2(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:
                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:
# 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):
    return sum(temp_list)
def calc_add_2(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:
                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:
# 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:
                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)