One day Nikita found the string containing letters "a" and "b" only. 

Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".

Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?


-----Input-----

The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". 


-----Output-----

Print a single integer — the maximum possible size of beautiful string Nikita can get.


-----Examples-----
Input
abba

Output
4
Input
bab

Output
2


-----Note-----

It the first sample the string is already beautiful.

In the second sample he needs to delete one of "b" to make it beautiful.

Difficulty: interview

Starter code:

Solutions:
s=input()
dp=[[0,0,0] for i in range(len(s))]
for i in range(len(s)):
    if s[i]=='a':
        dp[i][0]=dp[i-1][0]+1
        dp[i][1]=dp[i-1][1]
        dp[i][2]=max(dp[i-1][1]+1,dp[i-1][2]+1)
    else:
        dp[i][0]=dp[i-1][0]
        dp[i][1]=max(dp[i-1][0]+1,dp[i-1][1]+1)
        dp[i][2]=dp[i-1][2]
e=len(s)-1
print(max(dp[e][0],dp[e][1],dp[e][2]))


s = input()

d = [[0 for i in range(len(s) + 5)] for j in range(3)]

for i in range(1, len(s) + 1):
    d[0][i] = d[0][i - 1] + (s[i - 1] == 'b')
    
    d[1][i] = d[1][i - 1] + (s[i - 1] == 'a')
    d[1][i] = min(d[1][i], d[0][i - 1] + (s[i - 1] == 'a'))
    
    d[2][i] = d[2][i - 1] + (s[i - 1] == 'b')
    d[2][i] = min(d[2][i], d[1][i - 1] + (s[i - 1] == 'b'))
n = len(s)
print(len(s) - min(d[0][n], d[1][n], d[2][n]))


from sys import stdin, stdout

s = stdin.readline().rstrip()
n = len(s)
aCount=0
bCount=0
aList=[0]
bList=[0]
for i in range(n):
    if s[i]=='a':
        aCount+=1
        aList.append(aCount)
        bList.append(bCount)
    else:
        bCount+=1
        aList.append(aCount)
        bList.append(bCount)

bestScore = 0
for i in range(n+1):#start
    for j in range(i,n+1):#end
        score=0
        score+=aList[i]
        score+=bList[j]-bList[i]
        score+=aCount-aList[j]
        if score>bestScore:
            bestScore=score

print(bestScore)


from collections import defaultdict
from itertools import groupby


def reverse_enumerate(seq):
    n = len(seq)
    for i in range(n):
        yield n - 1 - i, seq[n - 1 - i]


class Symbol(object):
    def __init__(self, symbol, iter=None):
        self.symbol = symbol
        self.size = sum(1 for _ in iter) if iter else 0

    def __hash__(self):
        return hash(self.symbol)


def solve(inp):
    symbols = [Symbol(s, i) for s, i in groupby(inp)]
    longest_end_a = defaultdict(int)
    longest_start_a = defaultdict(int)
    longest_end_b = defaultdict(int)
    n = len(symbols)
    if n == 1:
        return len(inp)
    for idx, symbol in reverse_enumerate(symbols):
        if symbol.symbol == "a":
            longest_end_a[idx] = longest_end_a[idx + 1] + symbol.size
            longest_end_b[idx] = longest_end_b[idx + 1]
        else:
            longest_end_a[idx] = longest_end_a[idx + 1]
            longest_end_b[idx] = longest_end_b[idx + 1] + symbol.size
    for idx, symbol in enumerate(symbols):
        if symbol.symbol == "a":
            longest_start_a[idx] = longest_start_a[idx - 1] + symbol.size
        else:
            longest_start_a[idx] = longest_start_a[idx - 1]
    return max(
        longest_start_a[idx] + (longest_end_b[idx] - longest_end_b[second_idx]) + longest_end_a[second_idx]
        for idx in range(n)
        for second_idx in range(idx + 1, n + 1)
    )


def __starting_point():
    print(solve(input()))

__starting_point()

from collections import defaultdict
from itertools import groupby


def reverse_enumerate(seq):
    n = len(seq)
    for i in range(n):
        yield n - 1 - i, seq[n - 1 - i]


class Symbol(object):
    def __init__(self, symbol, iter=None):
        self.symbol = symbol
        self.size = sum(1 for _ in iter) if iter else 0

    def __hash__(self):
        return hash(self.symbol)


def solve(inp):
    symbols = [Symbol(s, i) for s, i in groupby(inp)]
    longest_end_a = defaultdict(int)
    longest_start_a = defaultdict(int)
    longest_end_b = defaultdict(int)
    n = len(symbols)
    if n == 1:
        return len(inp)
    for idx, symbol in reverse_enumerate(symbols):
        if symbol.symbol == "a":
            longest_end_a[idx] = longest_end_a[idx + 1] + symbol.size
            longest_end_b[idx] = longest_end_b[idx + 1]
        else:
            longest_end_a[idx] = longest_end_a[idx + 1]
            longest_end_b[idx] = longest_end_b[idx + 1] + symbol.size
    for idx, symbol in enumerate(symbols):
        if symbol.symbol == "a":
            longest_start_a[idx] = longest_start_a[idx - 1] + symbol.size
        else:
            longest_start_a[idx] = longest_start_a[idx - 1]
    return max(
        longest_start_a[idx] + (longest_end_b[idx] - longest_end_b[second_idx]) + longest_end_a[second_idx]
        for idx in range(n)
        for second_idx in range(idx + 1, n + 1)
    )


def __starting_point():
    print(solve(input()))
__starting_point()

str = input()
l = len(str)
ok = 0
test1 = 0
test2 = 0
for i in range(l):
    if(str[i] == 'a'):
        ok = ok + 1
        test2 = max(test1+1,test2+1)
    else:
        test1 = max(test1+1,ok+1)
print(max(test1,test2))

s = input()
a, b = [0], [0]
a_count, b_count = 0, 0
for x in s:
    if x == 'a':
        a_count += 1
    else:
        b_count += 1
    a.append(a_count)
    b.append(b_count)
best = b_count
for i in range(len(a)):
    for j in range(i, len(a)):
        best = max(best, a[i] + (a_count-a[j]) + (b[j]-b[i]))
print(best)



str = input()
l = len(str)
ok = 0
test1 = 0
test2 = 0
for i in range(l):
    if(str[i] == 'a'):
        ok = ok + 1
        test2 = max(test1 + 1, test2 + 1)
    else:
        test1 = max(test1 + 1,ok + 1)
print(max(test1, test2))


s=input()
                        #________b..._________
back=0                  #itung a sampe ktemu b [a....]
now=0                   #setelah a../nggak, jumlah b [a...b.. / b...]
after_ab=0              #setelah a..b.. [a..b..a]
for i in s:
    if i=='a':
        back+=1
        after_ab=max(after_ab,now)+1
    else:
        now=max(back,now)+1
print(max(back,max(now,after_ab)))
        
        


s = input()
size = len(s)
a = [0]*(size+1)
b = [0]*(size+1)
for i in range(size):
    a[i+1] = a[i] + (1 if s[i] == 'a' else 0)
    b[i+1] = b[i] + (1 if s[i] == 'b' else 0)
ans = 0
for i in range(size+1):
    for j in range(i+1):
        val = b[i] - b[j] + a[j] + a[size] - a[i]
        ans = max(val,ans)
print(ans)


s = input()
le = len(s)
mx = 0
a = s.count('a')
b = s.count('b')
mx = max(mx, a)
mx = max(mx, b)
cnta = [0]*(le+1)
cntb = [0]*(le+1)
cnta[0] = 1 if s[0]=='a' else 0
cntb[0] = 1-cnta[0]

for i in range(1,le):
	cnta[i] = cnta[i-1]
	cntb[i] = cntb[i-1]
	if s[i] == 'a':
		cnta[i] += 1
	else:
		cntb[i] += 1

for i in range(le):
	for j in range(i+1, le):
		mx = max(mx, cnta[i]+b-cntb[i])
		mx = max(mx, cntb[i]+a-cnta[i])
		mx = max(mx, cnta[i]+cntb[j]-cntb[i]+a-cnta[j])
		
print(mx)


strs=input()
array=[]
char=[]
count=1
char.append(strs[0])
i=1
while(i<len(strs)):
    if(strs[i]==strs[i-1]):
        count+=1
    else:
        array.append(count)
        char.append(strs[i])
        count=1
    i+=1
array.append(count)
bback=[]
aback=[]
bfront=[]
afront=[]
i=0
while(i<len(array)):
    if(i==0):
        if(char[i]=='b'):
            aback.append(0)
            bback.append(array[0])
        else:
            bback.append(0)
            aback.append(array[0])
    else:
        if(char[i]=='b'):
            aback.append(aback[i-1])
            bback.append(bback[i-1]+array[i])
        else:
            aback.append(aback[i-1]+array[i])
            bback.append(bback[i-1])
    i+=1
i=0
while(i<len(array)):
    afront.append(aback[-1]-aback[i])
    bfront.append(bback[-1]-bback[i])
    i+=1
if(len(array)==1):
    print(array[0])
else:
    maxm=-1
    i=0
    while(i<len(array)):
        if(char[i]=='b'):
            j=i
            while(j<len(array)):
                if(char[j]=='b'):
                    if(j==i):
                        ans=aback[i]+array[i]+afront[i]
                        if(ans>maxm):
                            maxm=ans
                    else:
                        ans=aback[i]+array[i]+(bback[j]-bback[i])+afront[j]
                        if(ans>maxm):
                            maxm=ans
                j+=1
        i+=1
    print(maxm)

str = input()
l = len(str)
cnt = 0
dp1 = 0
dp2 = 0
for i in range(l):
    if( str[i] == 'a' ):
        cnt+=1
        dp2 = max(dp1 + 1 , dp2 + 1)
    else:
        dp1 = max(dp1 + 1 , cnt+1 )
print(max(dp1,dp2))

a = input()

dp1 = 0
dp2 = 0
cnt = 0

for i in range(len(a)):
	if a[i] == 'a':
		cnt += 1
		dp2 = max(dp1 + 1, dp2 + 1)
	else:
		dp1 = max(dp1 + 1, cnt + 1)

print(max(dp1,dp2))

a = input()

dp1 = 0
dp2 = 0
cnt = 0

for i in range(len(a)):
	if a[i] == 'a':
		cnt += 1
		dp2 = max(dp1 + 1, dp2 + 1)
	else:
		dp1 = max(dp1 + 1, cnt + 1)

print(max(dp1,dp2))

#!/usr/bin/env python3

S = input()
N = len(S)
T = [[0]*(N+1) for _ in range(3)]
for i in range(1,N+1):
    T[0][i] = T[0][i-1] + int(S[i-1]=='a')
    T[1][i] = max(T[1][i-1] + int(S[i-1]=='b'), T[0][i])
    T[2][i] = max(T[2][i-1] + int(S[i-1]=='a'), T[1][i], T[0][i])
print(T[2][N])


s = input()
a_count = 0
dp1 = 0     # last letter is 'b'
dp2 = 0     # last letter is 'a'
for i in range(len(s)):
    if(s[i]=='a'):
        a_count += 1
        dp2 = max(dp1,dp2)+1
    else:
        dp1 = max(a_count,dp1)+1
print(max(dp1,dp2))

str = input()
l = len(str)
ok = 0
test1 = 0
test2 = 0
for i in range(l):
    if(str[i] == 'a'):
        ok = ok + 1
        test2 = max(test1+1,test2+1)
    else:
        test1 = max(test1+1,ok+1)
print(max(test1,test2))

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
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 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 main():
    s = [1 if c=='a' else 0 for c in S()]
    l = len(s)
    a = [0] * l
    b = [0] * l
    if s[0] == 1:
        a[0] = 1
    else:
        b[0] = 1
    for i in range(1,l):
        if s[i] == 1:
            a[i] = a[i-1] + 1
            b[i] = b[i-1]
        else:
            a[i] = a[i-1]
            b[i] = b[i-1] + 1

    if b[-1] == 0:
        return l

    r = 0
    for i in range(0,l):
        if s[i] == 1:
            continue
        for j in range(i,l):
            if s[j] == 1:
                continue
            tr = a[i] + b[j] - b[i] + 1 + a[-1] - a[j]
            if r < tr:
                r = tr

    return r


print(main())




s=input()
n=len(s)
f=[[0 for i in range(0,3)] for j in range(0,n)]
for i in range(0,n):
	if (i>0):
		f[i][0]=f[i-1][0]
		f[i][1]=f[i-1][1]
		f[i][2]=f[i-1][2]
	if (s[i]=='a'):
		f[i][0]+=1
		f[i][2]+=1
		f[i][2]=max(f[i][2],f[i][1]+1)
	else:
		f[i][1]+=1
		f[i][1]=max(f[i][1],f[i][0]+1)
print(max(f[n-1][0],max(f[n-1][1],f[n-1][2])))


# -*- coding: utf-8 -*-
"""
Created on Wed Oct 25 09:07:32 2017

@author: savit
"""

s=input()
startpos=0
max1=0
temp=0
beststart=[]
for i in range(len(s)):
    if(s[i]=='a'):
        
        temp+=1
        if(temp>max1):
            max1=temp
            startpos=i+1
    else:
        temp-=1
    beststart.append((max1,startpos))
    
beststart.append((0,-1))
max2=0
temp=0
endpos=len(s)
for i in range(len(s)-1,-1,-1):
    if(s[i]=='a'):
        temp+=(1 + (beststart[i-1][0]-beststart[i][0]))
        if(max2<temp):
            max2=temp
            endpos=i
    else:
        temp-=1
ans=0
if(endpos!=len(s)):
    
    startpos=beststart[endpos][1]

if(startpos==-1):
    startpos=0


for i in range(startpos):
    if(s[i]=='b'):
        ans+=1
for i in range(startpos,endpos):
    if(s[i]=='a'):
        ans+=1
for i in range(endpos,len(s)):
    if(s[i]=='b'):
        ans+=1
#print(startpos,endpos)
print(len(s)-ans)


s = input()
f = [0,0,0]
for i in s:
    if i=='a':
        f[2] = max(f)+1
        f[0]+=1
    else:
        f[1] = max(f[0],f[1])+1
print(max(f))    
    


line = input()

dp = 3 * [0]

for c in line:
    if c == 'a':
        dp[2] = max(dp) + 1
        dp[0] += 1
    elif c == 'b':
        dp[1] = max(dp[:2]) + 1

print(max(dp))


s = input()
ar = [0,0,0]
for i in s:
    if i == 'a':
        ar[2] = max(ar)+1
        ar[0] += 1
    else:
        ar[1] = max(ar[0],ar[1]) + 1
print(max(ar))  

# python3
# utf-8

string = input()
prefix___a_nr = [0]
prefix___b_nr = [0]
for sym in string:
    curr_a_nr = prefix___a_nr[-1]
    curr_b_nr = prefix___b_nr[-1]
    if sym == 'a':
        curr_a_nr += 1
    elif sym == 'b':
        curr_b_nr += 1
    prefix___a_nr.append(curr_a_nr)
    prefix___b_nr.append(curr_b_nr)

ans = 5000
for sep1 in range(len(string) + 1):
    for sep2 in range(sep1, len(string) + 1):
        curr_ans = 0
        curr_ans += prefix___b_nr[sep1]
        curr_ans += prefix___a_nr[sep2] - prefix___a_nr[sep1]
        curr_ans += prefix___b_nr[-1] - prefix___b_nr[sep2]
        ans = min(ans, curr_ans)
print(len(string) - ans)