Problem:
 Score : 600 points    Problem Statement You are given a string s consisting of A , B and C .  Snuke wants to perform the following operation on s as many times as possible:   Choose a contiguous substring of s that reads ABC and replace it with BCA .   Find the maximum possible number of operations.      Constraints  1 \leq |s| \leq 200000  Each character of s is A , B and C .         Input Input is given from Standard Input in the following format:  s       Output Find the maximum possible number of operations.        Sample Input 1 ABCABC      Sample Output 1 3  You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA . This is the maximum result.       Sample Input 2 C      Sample Output 2 0       Sample Input 3 ABCACCBABCBCAABCB      Sample Output 3 6

Solution (Python):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq as hq
import itertools as its
import operator as op



inf = float('inf')
N = 0


def set_inputs():
    global N
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    
    s = get_str()
    a = 0
    i = 0
    ans = 0
    while i < len(s):
        if s[i] == 'A':
            a += 1
            i += 1
            continue
        if a > 0 and i < len(s) - 1 and s[i:i+2] == 'BC':
            ans += a
            i += 2
            continue
        a = 0
        i += 1
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    main()

Solution (Python):
import re
s = input()
s = s.replace('BC', 'x')
sl = re.findall(r'[^BC]+', s)
n = 0
for s in sl:
    xs = 0
    for c in reversed(s):
        if c == 'x':
            xs += 1
        else:
            n += xs
print(n)

Solution (Python):




def main():
  text = input().strip()
  text = text.replace('BC', 'X')
  base = 0
  count = 0

  for i in range(len(text)):
    if text[i] == 'A':
      base += 1
    elif text[i] == 'X':
      count += base
    else:
      base = 0

  print(count)


if __name__ == '__main__':
  main()

Solution (Python):


from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict
import math


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def mod_inv(x):
    
    global mod
    return pow(x, mod - 2, mod)


def gcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    ret = math.factorial(x)
    ret = ret // (math.factorial(x - y) * math.factorial(y))

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)





def main():
    s = input()

    slistlist = []
    slist = []
    prev = None
    for c in s:
        if c == 'A':
            if prev == 'B':
                if slist:
                    slistlist.append(slist)
                slist = []
            slist.append(c)
            prev = None
        if c == 'B':
            if prev == 'B':
                if slist:
                    slistlist.append(slist)
                slist = []
            prev = c
        if c == 'C':
            if prev == 'B':
                slist.append('X')
            else:
                if slist:
                    slistlist.append(slist)
                slist = []
            prev = None
    if slist:
        slistlist.append(slist)

    ans = 0
    for slist in slistlist:
        count = 0
        for i in slist:
            if i == 'A':
                count += 1
            if i == 'X':
                ans += count

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import os
import sys

if os.getenv('LOCAL'):
    sys.stdin = open('_in.txt', 'r')

sys.setrecursionlimit(2147483647)
INF = float('inf')

S = sys.stdin.readline().rstrip()

a = 0
ans = 0
i = 0
while i < len(S):
    if S[i] == 'A':
        a += 1
    elif S[i] == 'B':
        if i + 1 < len(S) and S[i + 1] == 'C':
            i += 1
            ans += a
        else:
            a = 0
    else:
        a = 0
    i += 1
print(ans)

Solution (Python):
s = input()

s = s.replace("A","0").replace("BC", "1").replace("B"," ").replace("C"," ")
ss = list(s.split())
ans = 0
for e in ss:
    next = 0
    for i in range(len(e)):
        if e[i] == "1":
            ans += (i-next)
            next += 1
print(ans)

Solution (Python):
def solve():
    s = input()
    n=len(s)
    cnt = 0
    ans = 0
    j = 0
    for i in range(n-1):
        if j>=n-1:
            break
        if s[j]=='A':
            cnt+=1
        elif s[j]=='B' and s[j+1]=='C':
            ans+=cnt
            j+=1
        else:
            cnt = 0
        j+=1
    return ans
print(solve())

Solution (Python):
s = input()
sg = s.replace('BC', 'G')
sg = sg + 'E'

ans = 0
anstmp = 0
acount = 0
for i in sg:
    if i == 'A':
        acount += 1
    elif i == 'G':
        anstmp += acount
    else:
        ans += anstmp
        acount = 0
        anstmp = 0

print(ans)

Solution (Python):
S = input()[::-1]
N = len(S)
ans = 0
i = 0
cb = 0
ans = 0
while i < N:
    if S[i:i + 2] == 'CB':
        i += 2
        cb += 1
    elif S[i] == 'A':
        i += 1
        ans += cb
    else:
        i += 1
        cb = 0
print(ans)

Solution (Python):
s = input()

s_len = len(s)
ans = 0
A_cnt = 0
BC_flag = False

i = 0
while i < s_len:
	if not BC_flag:
		if s[i] == "A":
			A_cnt += 1
			BC_flag = True
		i += 1
	else:
		if s[i] == "A":
			A_cnt += 1
			i += 1
		elif s[i:i+2] == "BC":
			ans += A_cnt
			i += 2
		else:
			A_cnt = 0
			BC_flag = False
print(ans)

Solution (Python):
S = input()
L = len(S)
S += "!"
c = 0
i = 0
count_of_A = 0
while i < L:
    if S[i] == 'A':
        count_of_A += 1
        i += 1
    elif S[i] == 'B' and S[i+1] == 'C':
        c += count_of_A
        i += 2
    else:
        count_of_A = 0
        i += 1

print(c)

Solution (Python):
s = input().strip()

ss = None
r = 0
ac = 0
for c in s:
    if c == "A":
        if ss == "A":
            ac += 1
        else:
            ac = 1
    elif c == "C" and ss == "B":
        r += ac
        ss = "A"

        continue
    elif c == "B" and ss == "A":
        pass
    else:
        ac = 0
    ss = c

print(r)

Solution (Python):
s = list(input())


bc_cnt = 0
res = 0
for i in range(len(s)-2, -1, -1):
    if s[i] == "B":
        if s[i+1] == "C":
            bc_cnt += 1
        else:
            bc_cnt = 0
    elif s[i] == "A":
        if s[i+1] == "C":
            bc_cnt = 0
    else:
        if s[i+1] == "C":
            bc_cnt = 0
    if s[i] == 'A':
        res += bc_cnt
print(res)

Solution (Python):
s = input()
s_X = list(s.replace("BC", "X"))

A_count = 0
ans = 0
for i in range(len(s_X)):
    if s_X[i] == "A":
        A_count += 1
    elif s_X[i] == "X":
        ans += A_count
    else:
        A_count = 0

print(ans)

Solution (Python):



s=input().split("BC")
s.pop()
ans = 0
now=0

for i in s:
    if "B" not in i and "C" not in i:
        now += len(i)
        ans += now
    else:
        x=i.rfind('B')
        y=i.rfind('C')
        now = len(i) - max(x,y) - 1
        ans += now


print(ans)

Solution (Python):
def main():
    n = input().rstrip()

    n = n.replace('BC','z')

    op = 0
    cz = 0
    isZ = False
    for i in reversed(n):
        if i == 'z':
            isZ = True
            cz += 1
        elif i == 'A' and isZ:
            op += cz
        else:
            isZ = False
            cz = 0
    print(op)
    
if __name__=='__main__':
    main()

Solution (Python):
def count(S):
    p = 0
    res = 0
    for s in S:
        if s == '0':
            p += 1
        else:
            res += p
    return res

S = input().strip()
a = '0'
b = 1
bc = '1'
x = '2'
T = ''
stack = []
for s in S:
    if s == 'A':
        if stack:
            T += x
        stack = []
        T += a
    elif s == 'B':
        stack.append(b)
    elif s == 'C':
        if stack and stack[-1] == b:
            stack.pop()
            if stack:
                T += x
            T += bc
        else:
            stack.append(x)
ans = 0
for g in T.split(x):
    ans += count(g)
print(ans)

Solution (Python):
st = input()
N = len(st)
ans = 0
cnt = 0
i = 0
while i < N-1:
  if st[N-2-i:N-i] == "BC":
    cnt += 1
    i += 2
  elif st[N-1-i] ==  'A':
    ans += cnt
    i += 1
  else:
    cnt = 0
    i +=1
else:
  if st[0] == 'A':
    ans += cnt
  
print(ans)

Solution (Python):
S = input()
N = len(S)
A = [0]*N
BC = [0]*N
count = 0
i = 0
while i < N:
  if S[i] == "A":
    A[count] += 1
    i += 1
  elif i<N-1 and  S[i] == "B" and S[i+1] == "C":
    BC[i] += A[count]
    i += 2
  else:
    count += 1
    i += 1
print(sum(BC))

Solution (Python):
s=input()
n=len(s)
s+="X"
acnt=0
bccnt=0
res=0
i=0
while(i<n):
    if s[i:i+2]=="BC":
        bccnt+=1
        i+=1
    elif s[i]=="A":
        acnt+=1
        res-=bccnt
    else:
        res+=bccnt*acnt
        bccnt=0
        acnt=0
    i+=1
res+=bccnt*acnt
print(res)

Solution (Python):
import sys, math, collections, heapq, itertools
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    s = input()
    t = ""
    i = 0
    while i < len(s):
        if s[i] == "A":
            t += "A"
            i += 1
        elif s[i] == "B":
            if i < len(s) - 1:
                if s[i+1] == "C":
                    t += "D"
                    i += 2
                else:
                    t += "X"
                    i += 1
            else:
                t += "X"
                i += 1
        else:
            t += "X"
            i += 1
    
    total = 0
    numA = 0
    for i in range(len(t)):
        if t[i] == "X":
            numA =  0
        else:
            if t[i] == "A": numA += 1
            else: total += numA

    print(total)
    INF = 10 ** 25
    mod = 7 + 10 ** 9
    return 0
  
if __name__ == "__main__":
    solve()

Solution (Python):

S = input()
S = S.replace('BC','X')



cnt_A = 0
answer = 0
for s in S:
  if s == 'A':
    cnt_A += 1
  elif s =='X':
    answer += cnt_A
  else:
    cnt_A = 0
print(answer)

Solution (Python):
s=input()

b_charged=False
s1 = []

for i in range(len(s)):
    if s[i]=="B":
        b_charged = True
        s1.append("B")
    elif s[i]=="C":
        if b_charged:
            b_charged=False
            s1[-1]="BC"
        else:
            s1.append("C")
    else:
        b_charged=False
        s1.append("A")

a_count = 0
count = 0
for i in range(len(s1)):
    if s1[i]=="A":
        a_count+=1
    elif s1[i]=="BC":
        count+=a_count
    else:
        a_count=0

print(count)

Solution (Python):
s = input()
t = ""
last = ""
for i in s:
    if last == "B":
        if i == "C":
            t += "X"
        elif i == "B":
            t += last
        else:
            t += last
            t += i
    else:
        if i != "B":
            t += i
    last = i
count = 0
ans = 0
for i in t[::-1]:
    if i == "X":
        count += 1
    elif i == "A":
        ans += count
    else:
        count = 0
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    s = SI().replace('BC', 'X')
    cnt = 0
    ans = 0
    for k in s[::-1]:
        if k == 'X':
            cnt += 1
        elif k == 'A':
            ans += cnt
        else:
            cnt = 0
    return ans

print(main())

Solution (Python):

s = input()
ls_s = []
Skip = 0
s2 = ""
for i in range(len(s)-1):
    if Skip:
        Skip = 0
    elif s[i:i+2] == "BC":
        s2 += 'W'
        Skip = 11
    elif s[i] == "A":
        s2 += 'B'
    else:
        ls_s.append(s2)
        s2 = ""
ls_s.append(s2)

        
def bw(s):
    ls = []
    cnt_b =0
    for i in range(len(s)):
        if s[i] == "W":
            ls.append(cnt_b)
        else:
            cnt_b+=1
    return sum(ls)
sm = 0
for s2 in ls_s:
    sm += bw(s2)
print (sm)

Solution (Python):
s = input()
s = s.replace('BC','D')
N = len(s)
a = [0]*(N+1)
ans = [0]*(N+1)

for n in range(N):
    ans[n+1] = ans[n]
    if s[n] == 'A':
        a[n+1] = a[n]+1
    elif s[n] == 'D':
        a[n+1] = a[n]
        ans[n+1] += a[n]

print(ans[N])

Solution (Python):
s=list(input())
n=len(s)
a=0
ans=0
i=0
while i < n-1:
    if s[i]=='A':
        a+=1
        i+=1
    elif s[i]=='B' and s[i+1]=='C':
        ans+=a
        i+=2
    else:
        a=0
        i+=1
print(ans)

Solution (Python):
S = input()
S = S[::-1]


C = False
CB = False
cbcount = 0
ans = 0
for i in range(len(S)):
    if S[i] == "C":
        if CB:
            C = True
            CB = False

        elif not C:
            C = True

        else:
            cbcount = 0

    elif S[i] == "B":
        if C:
            CB = True
            C = False
            cbcount += 1
        else:
            C = False
            CB = False
            cbcount = 0
    else:
        if CB:
            ans += cbcount
        else:
            C = False
            CB = False
            cbcount = 0
print(ans)

Solution (Python):
"""
ABCABC
BCABCA
BCBCAA

"""

s =input().replace('BC', 'Z')
ss = s.split('B')
sss = []
for t in ss:
    sss.extend(t.split('C'))
ss = [t for t in sss if t]


def solve(s):
    a_cnt = 0
    cnt = 0
    for c in s:
        if c == 'A':
            a_cnt += 1
        else:
            cnt += a_cnt
    return cnt

total = 0
for s in ss:
    total += solve(s)
print(total)

Solution (Python):
def main():
	s = input()
	ans = 0
	stock_a = 0
	state = 0 
	ans = 0
	for i in range(len(s)):
		if state == 0:
			if s[i] == "A":
				stock_a = 1
				state = 1
			else:
				stock_a = 0
		elif state == 1:
			if s[i] == "B":
				state = 2
			elif s[i] == "A":
				stock_a += 1
			else:
				stock_a = 0
				state = 0
		elif state == 2:
			if s[i] == "C":
				ans += stock_a
				state = 1
			elif s[i] == "A":
				stock_a = 1
				state = 1
			else:
				state = 0
				stock_a = 0
	print(ans)

main()

Solution (Python):
s=input()
n=len(s)
s+="ZZZ"
si=0
ra=0
rbc=0
ans=0
while si<n:
    if(s[si]=="A"):
        ra+=1
        si+=1
    else:
        if(s[si:si+2]=="BC"):
            ans+=ra
            si+=2
        else:
            ra=0
            si+=1
print(ans)

Solution (Python):
s = raw_input()

ans = 0
c = 0
i = 0
while i<len(s)-1:
  if s[i]=="A":
    c += 1
    i += 1
  elif s[i:i+2]=="BC":
    ans += c
    i += 2
  else:
    c = 0
    i += 1
print ans

Solution (Python):
from sys import exit, setrecursionlimit, stderr, stdin
from functools import reduce
from itertools import *
from collections import defaultdict, Counter
from bisect import bisect
import functools

def input():
  return stdin.readline().strip()

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

s = input()
s = s.replace("BC", "X")
N = len(s)

ans = 0

x = 0
for i in range(N-1, -1, -1):
  if s[i] == "X":
    x += 1
  elif s[i] == "A":
    ans += x
  else:
    x = 0
print(ans)

Solution (Python):
l = []
for i in input().replace('BC', '!').split('B'):
        l += i.split('C')

ans = 0
for i in l:
        p = i.count('!')
        cnt = 0
        for j in i:
                if j == '!': cnt += 1
                else: ans += p - cnt
print(ans)

Solution (Python):
S=input()
data=[]
now=[]
idx=0
while idx<len(S):
    if S[idx]=="A":
        now.append(0)
        idx+=1
    elif S[idx]=="B" and idx+1<len(S):
        idx+=1
        if S[idx]=="C":
            now.append(1)
            idx+=1
        elif len(now)>0:
            data.append(now)
            now=[]
    else:
        idx+=1
        if len(now)>0:
            data.append(now)
            now=[]
if len(now)>0:
    data.append(now)

def calc(x):
    cnt=0
    ans=0
    for i in x:
        if i==0:
            cnt+=1
        elif i==1:
            ans+=cnt
    return ans
ans=0
for x in data:
    ans+=calc(x)
print(ans)

Solution (Python):
S = input()
ans = 0
cnt_a = 0
pc = "."
for c in S:
    
    if pc=="B":
        if c=="C":
            ans += cnt_a
        elif c=="A":
            cnt_a = 1
        else:
            cnt_a = 0
    else:
        if c=="C":
            cnt_a = 0
        elif c=="B":
            pass
        else:
            cnt_a += 1
    pc = c

print(ans)

Solution (Python):
s = input()
ans = 0
a = 0
b = False
for i in range(len(s)):
	if s[i] == "A":
		if b:
			a = 0
		a += 1
		b = False
	elif s[i] == "B":
		if b:
			a = 0
		b = True
	else:
		if b:
			ans += a
		else:
			a = 0
		b = False
print(ans)

Solution (Python):
s=input()
s=s.replace("BC","D").replace("B","C")
l=[i for i in s.split("C") if len(i) !=0]
cnt=0

for line in l:
  dcnt =0 
  for ch in reversed(line):
    if ch == "D":
      dcnt+=1
    else:
      cnt+=dcnt

print(cnt)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)


def main():
    s = S()
    l = len(s)
    a = []
    si = 0
    while si < l:
        if s[si] == 'A':
            a.append(1)
            si += 1
        elif s[si:si+2] == 'BC':
            a.append(2)
            si += 2
        else:
            a.append(0)
            si += 1

    r = 0
    t = 0
    for c in a:
        if c == 0:
            t = 0
        elif c == 1:
            t += 1
        else:
            r += t


    return r


print(main())

Solution (Python):
s = input().replace('BC', 'D')

acc = 0
ans = 0
for c in s:
    if c == 'B' or c == 'C':
        acc = 0
        continue
    if c == 'A':
        acc += 1
    if c == 'D':
        ans += acc
print(ans)

Solution (Python):
import re
s = input()
subs = re.sub('BC', 'X', s)

answer = 0
A = 0
for i in range(len(subs)):
    if subs[i] == 'A' :
        A += 1
    elif subs[i] == 'X':
        answer += A
    else:
        A = 0
print(answer)

Solution (Python):
import sys
from collections import deque
from collections import defaultdict
import math
sys.setrecursionlimit(20000000)

 
s = input()

s = s.replace("BC","X")
n = len(s)
ten = []
ans = 0
for i in range(n):
        if s[i] == "A" or s[i] == "X":
                ten.append(s[i])
        else:
                acount = 0
                for j in range(len(ten)):
                        if ten[j] == "A":
                                acount+=1
                        else:
                                ans += acount
                ten= []
acount = 0
for j in range(len(ten)):
        if ten[j] == "A":
                acount+=1
        else:
                ans += acount
print(ans)

Solution (Python):

from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    s = input().replace('BC', 'D')

    res, cnt = 0, 0
    for si in s:
        if si == 'A':
            cnt += 1
        if si == 'B' or si == 'C':
            cnt = 0
        if si == 'D':
            res += cnt
    print(res)




BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        if self._buffer.tell():
            return self._buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()