Problem:


Score : 800 points

Problem Statement

Given is a string S, where each character is 0, 1, or ?.

Consider making a string S' by replacing each occurrence of ? with 0 or 1 (we can choose the character for each ? independently).
Let us define the unbalancedness of S' as follows:

(The unbalancedness of S') = \max \{ The absolute difference between the number of occurrences of 0 and 1 between the l-th and r-th character of S (inclusive) :\ 1 \leq l \leq r \leq |S|\}

Find the minimum possible unbalancedness of S'.

Constraints

1 \leq |S| \leq 10^6

Each character of S is 0, 1, or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible unbalancedness of S'.

Sample Input 1

0??

Sample Output 1

1

We can make S'= 010 and achieve the minimum possible unbalancedness of 1.

Sample Input 2

0??0

Sample Output 2

2

Sample Input 3

??00????0??0????0?0??00??1???11?1?1???1?11?111???1

Sample Output 3

4



Sample input:
0??


Sample output:
1

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 二分探索、左右からの貪欲法、?をスキップした状態で1の連続数と0の連続数
S = list(sr())
answer = 0

def solve(S):
    global answer
    one_seq = 0
    one_max = 0
    one_start = set()
    zero_seq = 0
    zero_max = 0
    zero_start = set()
    cur = 0
    for i, s in enumerate(S):
        if s == '1':
            one_seq += 1
            zero_seq -= 0
            zero_seq = max(0, zero_seq)
            cur += 1
        elif s == '0':
            zero_seq += 1
            one_seq -= 0
            one_seq = max(0, one_seq)
            cur -= 1
        elif s == '?':
            one_seq -= 1
            zero_seq -= 1
            one_seq = max(0, one_seq)
            zero_seq = max(0, zero_seq)  # ここでは保留
        if one_seq > 0 and one_seq == one_max:
            one_start.add(i-one_seq+1)
        elif one_seq > 0 and one_seq > one_max:
            one_start = {i-one_seq+1}
            one_max = one_seq
        if zero_seq > 0 and zero_seq == zero_max:
            zero_start.add(i-zero_seq+1)
        elif zero_seq > 0 and zero_seq > zero_max:
            zero_start = {i-zero_seq+1}
            zero_max = zero_seq
    answer = max(one_max, zero_max)
    one_seq = 0
    zero_seq = 0
    cur = 0
    for i, s in enumerate(S):
        if s == '1':
            one_seq += 1
            zero_seq -= 0
            zero_seq = max(0, zero_seq)
            cur += 1
        elif s == '0':
            zero_seq += 1
            one_seq -= 0
            one_seq = max(0, one_seq)
            cur -= 1
        elif s == '?':
            if zero_seq == answer and i+1 in one_start:
                zero_max += 1
                answer += 1
                return None
            elif one_seq == answer and i+1 in zero_start:
                one_max += 1
                answer += 1
                return None
            if i+1 in one_start:
                S[i] = '0'
                cur -= 1
            elif i+1 in zero_start:
                S[i] = '1'
                cur += 1
            else:
                one_seq -= 1
                zero_seq -= 1
                one_seq = max(0, one_seq)
                zero_seq = max(0, zero_seq)  # ここでは保留           
    return S

for i in range(3):
    ret = solve(S[::-1])
    if ret:
        S = ret
    solve(S)

print(answer)

Submission status: Wrong Answer

Solution code:
import itertools

S = input()
T = S
T_list=[]
#'?'のインデックス
indexes =[i for i, x in enumerate(T) if x=='?']
#print(indexes)

#?を全探索
nlist = itertools.product(['0','1'],repeat=len(indexes))
#print(nlist)
for n in nlist:
  T=S
  #print(n)
  for j in range(len(indexes)):
    T = T.replace('?',n[j],1)
  T_list.append(T)

def vmax(T):
  vmax=0
  v=0
  for l in range(len(T)-1):
    for r in range(l+1,len(T)):
      v = abs(T[l:r+1].count('0')-T[l:r+1].count('1'))
      if v >= vmax:
        vmax =v
  return vmax

m =10**10
x =0
for t in T_list:
  x =vmax(t)
  if x < m:
    m = x
print(m)

Submission status: Wrong Answer

Solution code:
s=input()
qus=s.count("?")
zeros=s.count("0")
ones=s.count("1")
result=abs(zeros-ones)
if qus%2 ==1:
  result+= -1
max1=1
max2=0
last=""
news=""
for subs in s:
  if subs == "?":
    if last=="0":
      news+="1"
    elif last =="1":
      news+="0"
    else:
      if len(news)>1:
        if news[-1]=="0":
          news+="1"
        else:
          news +="0"
      else:
        news += "1"
    max1=1
    last=subs
    continue
  if last == subs:
    max1+=1
  else:
    max1=1
    last=subs
  max2=max(max1,max2)  
result=max(result,max2)
zeros=news.count("0")
ones=news.count("1")
max2=abs(zeros-ones)
result=max(result,max2)
max1=1
last=""
max2=0
for subs in news:
  if last == subs:
    max1+=1
  else:
    max1=1
    last=subs
  max2=max(max1,max2)  
result=max(result,max2)
print(result)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 二分探索、左右からの貪欲法、?をスキップした状態で1の連続数と0の連続数
S = list(sr())
answer = 0

def solve(S):
    global answer
    one_seq = 0
    one_max = 0
    one_start = set()
    zero_seq = 0
    zero_max = 0
    zero_start = set()
    cur = 0
    for i, s in enumerate(S):
        if s == '1':
            one_seq += 1
            zero_seq -= 0
            zero_seq = max(0, zero_seq)
            cur += 1
        elif s == '0':
            zero_seq += 1
            one_seq -= 0
            one_seq = max(0, one_seq)
            cur -= 1
        elif s == '?':
            one_seq -= 1
            zero_seq -= 1
            one_seq = max(0, one_seq)
            zero_seq = max(0, zero_seq)  # ここでは保留
        if one_seq > 0 and one_seq == one_max:
            one_start.add(i-one_seq+1)
        elif one_seq > 0 and one_seq > one_max:
            one_start = {i-one_seq+1}
            one_max = one_seq
        if zero_seq > 0 and zero_seq == zero_max:
            zero_start.add(i-zero_seq+1)
        elif zero_seq > 0 and zero_seq > zero_max:
            zero_start = {i-zero_seq+1}
            zero_max = zero_seq
    answer = max(one_max, zero_max)
    one_seq = 0
    zero_seq = 0
    cur = 0
    for i, s in enumerate(S):
        if s == '1':
            one_seq += 1
            zero_seq -= 0
            zero_seq = max(0, zero_seq)
            cur += 1
        elif s == '0':
            zero_seq += 1
            one_seq -= 0
            one_seq = max(0, one_seq)
            cur -= 1
        elif s == '?':
            if zero_seq == answer and i+1 in one_start:
                zero_max += 1
                answer += 1
                return None
            elif one_seq == answer and i+1 in zero_start:
                one_max += 1
                answer += 1
                return None
            if i+1 in one_start:
                S[i] = '0'
                cur -= 1
            elif i+1 in zero_start:
                S[i] = '1'
                cur += 1
            else:
                one_seq -= 1
                zero_seq -= 1
                one_seq = max(0, one_seq)
                zero_seq = max(0, zero_seq)  # ここでは保留           
    return S

for i in range(10):
    ret = solve(S)
    if ret:
        S = ret
    solve(S[::-1])
    

print(answer)

Submission status: Wrong Answer

Solution code:
def examA():
    T = I()
    ans = [0]*T
    for t in range(T):
        N = I()
        A = LI()
        S = SI()
        if S[-1]=="1":
            ans[t] = 1
            continue


    print(ans)
    return

def examB():
    S = SI()
    flag = True
    for i,s in enumerate(S):
        if i%2==0:
            if s=="1":
                flag = False
                break
        else:
            if s=="0":
                flag = False
                break
    if flag:
        print(1)
        return
    flag = True
    for i,s in enumerate(S):
        if i%2==1:
            if s=="1":
                flag = False
                break
        else:
            if s=="0":
                flag = False
                break
    if flag:
        print(1)
        return
    ans = 2
    cur = 0
    max0 = set()
    for i,s in enumerate(S):
        if s=="0":
            cur += 1
        else:
            if s=="?" and cur==ans:
                max0.add(i)
            cur -= 1
            if cur<0:
                cur = 0
        if ans<cur:
            ans = cur
    cur = 0
    max1 = set()
    for i,s in enumerate(S):
        if s=="1":
            cur += 1
        else:
            if s=="?" and cur==ans:
                max1.add(i)
            cur -= 1
            if cur<0:
                cur = 0
        if ans<cur:
            ans = cur
    cur = 0
    for i,s in enumerate(S):
        if s=="0":
            cur += 1
        else:
            if s=="?" and i in max1:
                cur += 1
            else:
                cur -= 1
            if cur<0:
                cur = 0
        if ans<cur:
            ans = cur
    cur = 0
    for i,s in enumerate(S):
        if s=="1":
            cur += 1
        else:
            if s=="?" and i in max0:
                cur += 1
            else:
                cur -= 1
            if cur<0:
                cur = 0
        if ans<cur:
            ans = cur
    print(ans)
    return

def examC():
    N, A, B = LI()

    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return


from decimal import Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examB()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn

111??000
111?000
"""

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit('(i8[::1],i8[::1],i8[::1])', cache=True)
def f(S, T1, T2):
    m, x, M = 0, 0, 0
    for i in range(len(S)):
        if S[i] == -1:
            x -= 1
        elif S[i] == 1:
            x += 1
        else:
            if x - 1 + T1[i + 1] >= m:
                x -= 1
            elif x + 1 + T2[i + 1] > M:
                x -= 1
            else:
                x += 1
        m = min(m, x)
        M = max(M, x)
    return M - m

S = np.array(list(read().rstrip().decode()), 'U1')

Si = np.zeros(len(S), np.int64)
Si[S == '0'] = -1
Si[S == '1'] = 1
S = Si

def get_min_after(S):
    T = S.copy()
    T[S == 0] = 1
    T = np.cumsum(np.concatenate([[0], T]))
    T = np.minimum.accumulate(T[::-1])[::-1] - T
    return T


T1 = get_min_after(S)
T2 = -get_min_after(-S)
print(f(S, T1, T2))

Submission status: Wrong Answer

Solution code:
S = list(input())

def long_repeat(line):
 
    max = 0
    cnt = 1
    
    for i in range(len(line)-1):
        if cnt > 1:
            cnt -= 1
            continue
        for j in range(i+1,len(line)):
            if line[i] == line[j]:
                cnt += 1
            else:
                break
        if max < cnt:
            max = cnt
 
    return max

s = long_repeat(S)

if len(S) <= s+1:
    print(s-1)
else:
    print(s)

Submission status: Wrong Answer

Solution code:
z=0
n=0
s=input()
l=[]
q=0
for i in range(len(s)):
	if s[i]=="0":
		z+=1
	elif s[i]=="1":
		n+=1
	else:
		if z>n:
			q="1"
			n+=1
		elif z<n:
			q="0"
			z+=1
		else:
			if s[i-1]=="1":
				z+=1
			elif s[i-1]=="?":
				if q=="0":
					n+=1
				else:
					z+=1
			else:
				n+=1
	
	l.append(abs(z-n))
print(max(l))

Submission status: Wrong Answer

Solution code:
def solve(s):
    n = len(s)
    # count 0
    # ? -> 1
    res_0 = 0
    cnt_00 = 0
    cnt_01 = 0
    # count 1
    # ? -> 0
    res_1 = 0
    cnt_10 = 0
    cnt_11 = 0
    for i in range(n):
        if s[i] == "0":
            cnt_00 += 1
            cnt_10 += 1
        elif s[i] == "1":
            cnt_01 += 1
            cnt_11 += 1
        else:
            cnt_01 += 1
            cnt_10 += 1
        res_0 = max(res_0, cnt_00 - cnt_01)
        res_1 = max(res_1, cnt_11 - cnt_10)
        if cnt_00 < cnt_01:
            cnt_00 = 0
            cnt_01 = 0
        if cnt_11 < cnt_10:
            cnt_10 = 0
            cnt_11 = 0
    # print(res_0, res_1)
    if max(res_0, res_1) > 1:
        if res_0 != res_1:
            return max(res_0, res_1)
        else:
            # check
            # count 0
            # ? -> 1
            cnt_00 = 0
            cnt_01 = 0
            left_0 = 0
            # count 1
            # ? -> 0
            cnt_10 = 0
            cnt_11 = 0
            left_1 = 0
            max_0_flg = [0] * n
            max_1_flg = [0] * n
            for i in range(n):
                if s[i] == "0":
                    cnt_00 += 1
                    cnt_10 += 1
                elif s[i] == "1":
                    cnt_01 += 1
                    cnt_11 += 1
                else:
                    cnt_01 += 1
                    cnt_10 += 1
                # print(cnt_00 - cnt_01, left_0, cnt_11 - cnt_10, left_1)
                if cnt_00 - cnt_01 == res_0:
                    for j in range(left_0, i + 1):
                        max_0_flg[j] = 1
                    left_0 = i + 1
                if cnt_11 - cnt_10 == res_1:
                    for j in range(left_1, i + 1):
                        max_1_flg[j] = 1
                    left_1 = i + 1
                if cnt_00 < cnt_01:
                    cnt_00 = 0
                    cnt_01 = 0
                    left_0 = i + 1
                if cnt_11 < cnt_10:
                    cnt_10 = 0
                    cnt_11 = 0
                    left_1 = i + 1
            # print(max_0_flg, max_1_flg)
            # check
            flag_0 = False
            flag_1 = False
            count_q = 0
            flag_tie = False
            for i in range(n):
                if max_0_flg[i] == 1:
                    flag_0 = True
                    if flag_1 and count_q % 2 == 1:
                        flag_tie = True
                    flag_1 = False
                    count_q = 0
                elif max_1_flg[i] == 1:
                    flag_1 = True
                    if flag_0 and count_q % 2 == 1:
                        flag_tie = True
                    flag_0 = False
                    count_q = 0
                elif s[i] == "?":
                    count_q += 1
                else:
                    flag_0 = False
                    flag_1 = False
                    count_q = 0

            # tie break
            if flag_tie:
                return res_0 + 1
            else:
                return res_0

    else:
        # 0 happens
        flag_01 = False
        flag_02 = False
        # 1 happens
        flag_11 = False
        flag_12 = False
        for i in range(n):
            if s[i] == "0":
                if i % 2 == 0:
                    flag_02 = True
                else:
                    flag_01 = True
            elif s[i] == "1":
                if i % 2 == 0:
                    flag_12 = True
                else:
                    flag_11 = True
        flag_one = True
        if flag_01 and flag_02:
            flag_one = False
        if flag_11 and flag_12:
            flag_one = False
        if flag_one:
            # 1 is possible
            return 1
        else:
            return 2


def main():
    s = input()
    res = solve(s)
    print(res)


def test():
    assert solve("0??") == 1
    assert solve("0??0") == 2
    assert solve("??00????0??0????0?0??00??1???11?1?1???1?11?111???1") == 4
    assert solve("11?00") == 3


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

Submission status: Wrong Answer

Solution code:
s=list(input())
zero=s.count('0')
iti=s.count('1')
hatena=s.count('?')
if(hatena==0):
	print(abs(zero-iti))
elif(zero-iti==0):
	print(1)
elif(zero+iti<=hatena):
		print(abs(zero-iti))
elif(len(s)%2==0):
	print(abs(zero-iti)+1)
elif((len(s)%2)!=0):
	print(abs(zero-iti)-1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit('(i8[::1],i8[::1])', cache=True)
def f(S, T):
    m, x, M = 0, 0, 0
    for i in range(len(S)):
        if S[i] == -1:
            x -= 1
        elif S[i] == 1:
            x += 1
        else:
            if x - 1 + T[i] >= m:
                x -= 1
            else:
                x += 1
        m = min(m, x)
        M = max(M, x)
    return M - m

S = np.array(list(read().rstrip().decode()), 'U1')

Si = np.zeros(len(S), np.int64)
Si[S == '0'] = -1
Si[S == '1'] = 1
S = Si

T = S.copy()
T[S == 0] = 1
T = np.cumsum(T)
T = np.minimum.accumulate(T[::-1])[::-1] - T
print(f(S, T))

Submission status: Wrong Answer

Solution code:
z=0
n=0
s=input()
fz=s.count("0")
fo=s.count("1")

l=[]
for i in range(len(s)):
	if s[i]=="0":
		z+=1
	elif s[i]=="1":
		n+=1
	else:
		if z>n:
			n+=1
		elif z<n:
			z+=1
		else:
			if fz>fo:
				n+=1
				fo+=1
			else:	
				z+=1
				fz+=1
	l.append(abs(z-n))
print(max(l))

Submission status: Wrong Answer

Solution code:
a = input()
X = a.count("0")
Y = a.count("1")
Z = a.count("?") % 2
q = (abs(X - Y) - Z)
if Z > q:
    q = 0
print(q)

Submission status: Wrong Answer

Solution code:
def solve(S):
    if S[0] == "?":
        for i in range(len(S)):
            if S[i] == "0":
                if i % 2 == 0:
                    S[0] = "0"
                else:
                    S[0] = "1"
                break
            elif S[i] == "1":
                if i % 2 == 0:
                    S[0] = "1"
                else:
                    S[0] = "0"
                break
    if S[0] == "?":
        return 1

    sum0 = 0
    sum1 = 0
    un = 1
    val = -1
    for i in range(len(S)):
        if S[i] == "0":
            sum0 += 1
            val = -1
        elif S[i] == "1":
            sum1 += 1
            val = -1
        else :
            if sum0 > sum1:
                sum1 += 1
            elif sum1 > sum0:
                sum0 += 1
            else:
                if val == -1:
                    for j in range(i, len(S)):
                        if S[j] == "0":
                            if (j - i) % 2 == 0:
                                val = 0
                            else:
                                val = 1
                            break
                        elif S[j] == "1":
                            if (j - i) % 2 == 0:
                                val = 1
                            else:
                                val = 0
                            break
                if val == 1:
                    sum1 += 1
                elif val == 0:
                    sum0 += 1
                else:
                    return un
        un = max(un, abs(sum0 - sum1))
    return un

S = list(input())
print(min(solve(S), solve(S[::-1])))

Submission status: Wrong Answer

Solution code:
s = input()
mi = 0
ma = 10 ** 6


def check(m):
    a = 0
    b = 0
    for i in s:
        if i == "0":
            if a + 1 > m:
                a = a - 1
            else:
                a = a + 1
            b = b + 1
        elif i == "1":
            if b - 1 < 0:
                b = b + 1
            else:
                b = b - 1
            a = a - 1
        else:
            if a + 1 > m:
                a = a - 1
            else:
                a = a + 1
            if b - 1 < 0:
                b = b + 1
            else:
                b = b - 1
        if b > m or a < 0:
            return False
    return a >= b


def check2(m):
    a = 0
    b = 0
    for i in s:
        if i == "0":
            if a + 1 > 0:
                a = a - 1
            else:
                a = a + 1
            b = b + 1
        elif i == "1":
            if b - 1 < -m:
                b = b + 1
            else:
                b = b - 1
            a = a - 1
        else:
            if a + 1 > 0:
                a = a - 1
            else:
                a = a + 1
            if b - 1 < -m:
                b = b + 1
            else:
                b = b - 1
        if b > 0 or a < -m:
            return False
    return a >= b


# print(check(1))
# input()
while mi != ma:
    mm = (mi + ma) // 2
    if check(mm) or check2(mm):
        ma = mm
    else:
        mi = mm + 1
print(mi)

Submission status: Wrong Answer

Solution code:
z=0
n=0
s=input()
l=[]
for i in range(len(s)):
	if s[i]=="0":
		z+=1
	elif s[i]=="1":
		n+=1
	else:
		if z>n:
			n+=1
		elif z<n:
			z+=1
		else:
			if i<len(s)-1 and s[i+1]=="1":
				z+=1
			else:	
				n+=1
	l.append(abs(z-n))
print(max(l))

Submission status: Wrong Answer

Solution code:
s = input()

zCount = s.count("0")
oCount = s.count("1")
qCount = s.count("?")
diff = abs(zCount - oCount)
# if zCount > oCount:
#     diff = zCount - oCount
# else:
#     diff = oCount - zCount 
if qCount % 2 == 0:
    print(diff)
else:
    diff += 1
    print(diff)

Submission status: Wrong Answer

Solution code:
# ??00????0??0????0?0??00??1???11?1?1???1?11?111???1
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.buffer.readline().rstrip()
rs = lambda: sys.stdin.buffer.readline().split()
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))



s = rr()
temp = 0
cnt = 0
c = collections.Counter(s)
print(abs(c[48] - c[49]))

Submission status: Wrong Answer

Solution code:
S = input()

count0 = S.count('0')
#print(count0)
count1 = S.count('1')
#print(count1)
countQ = S.count('?')
#print(countQ)
abs1 = abs(count0 - count1)
#print(abs1)

if abs1 == 0:
  if len(S) == 2:
    print(1)
  elif countQ % 2 == 0:
    print(2)
  else:
    print(1)
else:
  if countQ % 2 == 0:
    print(abs1)
  else:
    print(abs1 + 1)

Submission status: Wrong Answer

Solution code:
S=input()

N = len(S)


if N == 1:
    print(1)
else:
    max = [-1,-1]
    tmpmax=-1
    cnt = 0
    maxpos= -1
    right=0
    rightmax=0
    left=0
    leftmax=0
    chk = ['0','1']

    for k in range(2):

        for i in range(N):
            if S[i] == chk[k]:
                cnt = cnt + 1
                if cnt > tmpmax:
                    tmpmax = cnt
                    maxpos = i
            else:
                cnt = 0

        for i in range(maxpos+1,N):
            if S[i] != chk[k]:
                right = right - 1
            else:
                right = right + 1
            if right > 0 and right > rightmax:
                rightmax = right

        for i in range(maxpos-tmpmax,-1,-1):
            if S[i] != chk[k]:
                left = left - 1
            else:
                left = left + 1
            if left > 0 and left > leftmax:
                leftmax = left

        if tmpmax == -1:
            max[k] = 1
        else:
            max[k] = tmpmax + rightmax + leftmax

    if max[0] > max[1]:
        max = max[0]
    else:
        max = max[1]

Submission status: Wrong Answer

Solution code:
s = input()

zCount = s.count("0")
oCount = s.count("1")
qCount = s.count("?")
if zCount > oCount:
    diff = zCount - oCount
else:
    diff = oCount - zCount 
if qCount % 2 == 0:
    print(diff)
else:
    diff += 1
    print(diff)

Submission status: Wrong Answer

Solution code:
a = input()
X = a.count("0")
Y = a.count("1")
Z = a.count("?")  %  2
q = (abs(X - Y)  -  Z)
print(q)

Submission status: Wrong Answer

Solution code:
from collections import Counter

S=input()
l=len(S)
list=list(S)

def change(list):
  i=1
  while(i<l):
    if(list[i]=="?"):
       if(list[i-1]=="0"):
         list[i]="1"
       else:
         list[i]="0"
    i+=1

if(list[0]!="?"):
  change(list)
  c = Counter(list)
  print(abs(c["1"]-c["0"]))
else:
  slist=list

  list[0]=0
  change(list)

  slist[0]=1
  change(slist)

  c1 = Counter(list)
  c2 = Counter(slist)

  r1=abs(c1["1"]-c1["0"])
  r2=abs(c2["1"]-c2["0"])

  if(r1<r2):
    print(r1+1)
  else:
    print(r2+1)

Submission status: Wrong Answer

Solution code:
S=input()
mode0_cnt=0
mode1_cnt=0
ans=1


if "11" in S:
	for h in range(len(S)):
		if mode0_cnt==0 and mode1_cnt==0:
			if S[h]=="0":
				mode0_cnt+=1
			elif S[h]=="1":
				mode1_cnt+=1
		elif mode1_cnt>0:
			if S[h]=="1":
				mode1_cnt+=1
				ans=max(ans,mode1_cnt)
			else:
				mode1_cnt-=1
		else:
			if S[h]=="0":
				mode0_cnt+=1
				ans=max(ans,mode0_cnt)
			else:
				mode0_cnt-=1
	print(ans)

elif "00" in S:
	for h in range(len(S)):
		if mode0_cnt==0 and mode1_cnt==0:
			if S[h]=="0":
				mode0_cnt+=1
			elif S[h]=="1":
				mode1_cnt+=1
		elif mode1_cnt>0:
			if S[h]=="1":
				mode1_cnt+=1
				ans=max(ans,mode1_cnt)
			else:
				mode1_cnt-=1
		else:
			if S[h]=="0":
				mode0_cnt+=1
				ans=max(ans,mode0_cnt)
			else:
				mode0_cnt-=1
	print(ans)

else:
	temp_bef=""
	temp_af=""
	cnt=0
	flag=1
	#?が偶数か奇数か
	for h in range(len(S)):
		if temp_bef=="":
			if S[h]=="1" or S[h]=="0":
				temp_bef=S[h]
		else:
			if S[h]=="?":
				cnt+=1
			elif S[h]==temp_bef:
				if cnt%2==0:
					flag=2
					break
				temp_bef==""
				cnt=0
			else:
				if cnt%2==1:
					flag=2
					break
				temp_bef==""
				cnt=0
	
	print(1 if flag ==1 else 2)

Submission status: Wrong Answer

Solution code:
z=0
n=0
s=input()
l=[]
for i in range(len(s)):
	if s[i]=="0":
		z+=1
	elif s[i]=="1":
		n+=1
	else:
		if z>n:
			n+=1
		else:
			z+=1
	
	l.append(abs(z-n))
print(max(l))

Submission status: Wrong Answer

Solution code:
S = input()

count0 = S.count('0')
#print(count0)
count1 = S.count('1')
#print(count1)
countQ = S.count('?')
#print(countQ)
abs1 = abs(count0 - count1)
#print(abs1)

if abs1 == 0:
  if countQ % 2 == 0:
    print(2)
  else:
    print(1)
else:
  if countQ % 2 == 0:
    print(abs1)
  else:
    print(abs1 + 1)

Submission status: Wrong Answer

Solution code:
a = input()
X = a.count("0")
Y = a.count("1")
Z = a.count("?") % 2
q = (abs(X - Y) - Z)
print(q)

Submission status: Wrong Answer

Solution code:
a = input()
b= []
c= []
for i in range(0, len(a)):
    if a[i]=="?":
        if i%2 == 0:
            a = a.replace("?", "1")
        else:
            a= a.replace("?", "0")
if a[i]== "1":
    b.append("1")
elif a[i]== "0":
    c.append("0")
print(abs(len(b)-len(c)))

Submission status: Wrong Answer

Solution code:
S = input()

count0 = S.count('0')
#print(count0)
count1 = S.count('1')
#print(count1)
countQ = S.count('?')
#print(countQ)
abs1 = abs(count0 - count1)
#print(abs1)

if abs1 == 0:
  if countQ % 2 == 0:
    print(3)
  else:
    print(1)
else:
  if countQ % 2 == 0:
    print(abs1)
  else:
    print(abs1 + 1)

Submission status: Wrong Answer

Solution code:

S= input()

TmpS = list(S)
oneSize = len(S.replace('0','').replace('?',''))
zeroSize = len(S.replace('1','').replace('?',''))

zeroCount = 0
oneCount = 0
resCount = 0
res=1
tmp = ''
for i in range(len(TmpS)):
    if TmpS[i] == '1':
        oneCount += 1
    elif TmpS[i] == '0':
        zeroCount += 1
    else:
        sizeDiff =abs(oneSize - zeroSize)
        countDiff =abs(oneCount - zeroCount)
        if sizeDiff == 0 and countDiff == 0:
            if tmp == '1' or (i<len(TmpS)-1 and TmpS[i+1]=='1'):
                TmpS[i] = '0'
                zeroCount += 1
                zeroSize += 1
            else:
                TmpS[i] = '1'
                oneCount += 1
                oneSize += 1

        else:
            tmpOne = oneSize if sizeDiff > countDiff else oneCount
            tmpZero = zeroSize if sizeDiff > countDiff else zeroCount
            if tmpOne > tmpZero:
                TmpS[i] = '0'
                zeroCount += 1
                zeroSize += 1
            else:
                TmpS[i] = '1'
                oneCount += 1
                oneSize += 1

    if tmp == TmpS[i]:
        resCount += 1
        if resCount > res:
            res = resCount
    else:
        resCount = 1
    tmp = TmpS[i]

print(res)

Submission status: Wrong Answer

Solution code:
from collections import Counter

S=input()
l=len(S)
list=list(S)

def change(list):
  i=1
  while(i<l):
    if(list[i]=="?"):
       if(list[i-1]=="0"):
         list[i]="1"
       else:
         list[i]="0"
    i+=1

if(list[0]!="?"):
  change(list)
  c = Counter(list)
  print(abs(c["1"]-c["0"]))
else:
  slist=list

  list[0]=0
  change(list)

  slist[0]=1
  change(slist)

  c1 = Counter(list)
  c2 = Counter(slist)

  r1=abs(c1["1"]-c1["0"])
  r2=abs(c2["1"]-c2["0"])

  if(r1<r2):
    print(r1)
  else:
    print(r2)

Submission status: Wrong Answer

Solution code:
s=input()

cnt0=0
cnt1=0
r=0
for c in s:
    if c == "0":
        cnt0+=1
    elif c == "1":
        cnt1+=1
    elif c == "?":
        if cnt0 > cnt1:
            cnt1 += 1
        else:
            cnt0 += 1

    r=max(r,abs(max(cnt0,cnt1)-min(cnt0,cnt1)))

print(r)

Submission status: Wrong Answer

Solution code:
s = input()
z, o, q = s.count('0'), s.count('1'), s.count('?')
print(abs(z-o) if q%2==0 else abs(z-o)-1)

Submission status: Wrong Answer

Solution code:
S = map(str,input())
S ="".join(S)
lists = list(S)
n = 0
for i in lists:
    if i == "0":
        n +=1
print(n)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

S = list(input())

cnt1 = 0
m_cnt1 = 0
for s in S:
    if s == '1':
        cnt1 += 1
    else:
        cnt1 -= cnt1 > 0
    m_cnt1 = max(m_cnt1, cnt1)

cnt2 = 0
m_cnt2 = 0
for s in S:
    if s == '2':
        cnt2 += 1
    else:
        cnt2 -= cnt2 > 0
    m_cnt2 = max(m_cnt2, cnt2)

print(max(m_cnt1, m_cnt2))

Submission status: Wrong Answer

Solution code:
s = input()
zero = 0
ones = 0
unbalance = 10**18
last = 0
mode = 0
maxlast = 0
for i in range(len(s)):
	if s[i] == "0":
		zero += 1
		if mode == 0:
			last += 1
		else:
			mode = 0
			last = 1
	elif s[i] == "1":
		ones += 1
		if mode == 1:
			last += 1
		else:
			mode = 0
			last = 1
	else:
		if ones > zero:
			zero += 1
			if mode == 0:
				last += 1
			else:
				mode = 0
				last = 1
		else:
			ones += 1
			if mode == 1:
				last += 1
			else:
				mode = 0
				last = 1
	maxlast = max(maxlast, last)
	unbalance = min(abs(zero-ones), unbalance)
print(max(unbalance, maxlast))

Submission status: Wrong Answer

Solution code:
S = map(str,input())
S ="".join(S)
lists = list(S)
n = 0
for i in lists:
    if i == "0":
        n +=1
    else:
        if len(S)>5:
            print("4")
print(n)

Submission status: Wrong Answer

Solution code:
s=input()
qus=s.count("?")
zeros=s.count("0")
ones=s.count("1")
result=abs(zeros-ones)
if qus%2 ==1:
  result+= -1
max1=1
max2=0
last=""
for subs in s:
  if subs == "?":
    max1=1
    last=""
    continue
  if last == subs:
    max1+=1
  else:
    max1=1
    last=subs
  max2=max(max1,max2)
result=max(result,max2)
print(result)