Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

A - 阿吽の呼吸

Problem Statement

時は進んで 2060 年，共に 70 歳を迎える前田さんと後藤さんは長い付き合いの友人であり，大学時代にACM-ICPCで共に戦った仲間でもある．

二人は今でもよく一緒にお茶を飲みつつ，競技プログラミングの話で盛り上がっている．

二人で一緒にお茶を飲む時，前田さんが 1 回Aと言うと，その発言の後に後藤さんがちょうど 1 回Unと返事をする習慣がいつのまにか出来た．

しかし最近後藤さんは物忘れや勘違いをすることが多く，前田さんがAと言っても，後藤さんはたまにUnの返事を忘れたり，余計に返事をしたりする．

ついこの間も前田さんと後藤さんはお茶を飲みながら，二人のお気に入りのデータ構造について話し込んでいたようだ．

この時の会話の中から，Aで表される前田さんの発言と，Unで表される後藤さんの返事のみからなる記録が時系列で与えられたとき，後藤さんが習慣通りに反応したとみなすことが出来るかチェックしてほしい．

注意点として，前田さんの発言に対し，後藤さんの返事が多少遅れても，後藤さんは習慣通りに反応したとみなせる場合がある，ということが挙げられる．
例えば，前田さんが2回連続してAと言った後，後藤さんが 2 回連続してUnと返事をして会話が終了した場合は，後藤さんが習慣通りの返事をしたとみなされる (Sample Input 2 参照)．

また，会話が終了した時点で，前田さんがAと言った回数と，後藤さんがUnと返事した回数が一致しても，後藤さんが習慣通りに返事をしたとはみなされない場合もあるので注意すること．
例えば，前田さんが1回Aと言った後，後藤さんが 2 回連続してUnと返事し，その後で前田さんが 1 回Aと言って会話が終了した場合は，後藤さんが習慣通りの返事をしたとはみなされない (Sample Input 3 参照)．

Input

入力は以下の形式で与えられる．

$N$

$S_1$

$S_2$

$…$

$S_N$

最初の行はひとつの整数からなる． 
$N$ は，記録に含まれる前田さんがAと発言した回数と後藤さんがUnと返事した回数の合計を表し，$1 \leq N \leq 100$ を満たす．
その後 $N$ 行に，文字列 $S_i$ が続き，各 $S_i (1 \leq i \leq N)$ はAかUnのどちらかに一致する．ここでAは前田さんの発言，Unは後藤さんの返事を表す．
$i$ の小さい順に $S_i$ が記録されたものとする．
前田さんと後藤さんが同時に発言することは無かったとする．

Output

後藤さんが習慣通りに反応したとみなすことが出来ればYES，出来なければNOを1行で出力すること．

Sample Input 1

4
A
Un
A
Un

Output for the Sample Input 1

YES

Sample Input 2

4
A
A
Un
Un

Output for the Sample Input 2

YES

Sample Input 3

4
A
Un
Un
A

Output for the Sample Input 3

NO

Sample Input 4

1
Un

Output for the Sample Input 4

NO


Sample input:
4
A
Un
A
Un


Sample output:
YES

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    S = [input() for _ in range(N)]
    a_count = 1 if S[0] == "A" else -1
    if N == 1 and a_count == -1:
        return "NO"
    for i in range(1, N):
        if a_count > 0:
            a_count += 1 if S[i] == "A" else -1
        else:
            return "NO"
    return "YES"


if __name__ == "__main__":
    print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    S = [input() for _ in range(N)]
    a_count = 1 if S[0] == "A" else -1
    if N == 1 and a_count == -1:
        return "NO"
    for i in range(1, N):
        if a_count >= 0:
            a_count += 1 if S[i] == "A" else -1
        else:
            return "NO"
    return "YES"


if __name__ == "__main__":
    print(solve())

Submission status: Wrong Answer

Solution code:
n = int(input())
A_cou = 0
Un_cou = 0
ans = "YES"
for i in range(n):
    sen = input()
    if sen == "A":
        A_cou += 1
    else:
        Un_cou += 1
        if A_cou < Un_cou:
            ans = "NO"
            break
print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    n = int(input())
    data = []
    for _ in range(n):data.append(input())
    state =-1
    for i in data:
        if state == -1 and i =='A':state = 0
        elif state == 0 and i =='Un':state=1
        elif state ==1 and i =='A':state = 0
    if state == 1:print("YES")
    else:print("NO")

Submission status: Wrong Answer

Solution code:
N = int(input())
src = [input() for i in range(N)]
ans = a = 0
for s in src:
    if s == 'A': a += 1
    elif a > 0:
        a -= 1
    else:
        print('NO')
        break
else:
    print('YES')

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
S = [raw_input() for _ in range(N)]

cnt = 0
flag = True

for i in S:
    if i == "A":
        cnt += 1
    else:
        cnt -= 1;
    
    if cnt < 0:
        flag = False
        break

print "Yes" if flag else "No"

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
S = [raw_input() for _ in range(N)]

cnt = 0
flag = True

for i in S:
    if i == "A":
        cnt += 1
    else:
        cnt -= 1;
    
    if cnt < 0:
        flag = False
        break

print "YES" if flag else "NO"

Submission status: Wrong Answer

Solution code:
a = 0
for _ in range(int(input())):
    if input() == "A":a += 1
    else:
        a -= 1
        if a < 0:
            print("NO")
            break
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
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**10
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():
    n = I()
    a = [S() for _ in range(n)]
    t = 0
    f = False
    for c in a:
        if c == 'A':
            t += 1
        elif t > 0:
            f = True
            t -= 1
    if t == 0 and f:
        return 'YES'


    return 'NO'


print(main())

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    ls = []
    for i in range(n):
        ls.append(input())
    a = 0
    un = 0
    i = 0

    if ls[0] != 'A':
        print('NO')
        return
    if ls[-1] != 'Un':
        print('NO')
        return

    last = ''
    for l in ls:
        if last == 'Un' and last != l and a != un:
            print('NO')
            return
        if l == 'A':
            a += 1
        if l == 'Un':
            un += 1
        if a < un:
            print('NO')
            return
        last = l
    print('YES')

main()

Submission status: Wrong Answer

Solution code:
def finish():
    print('NO')
    exit()

N = int(input())
cnt = 0
vec = -1
for i in range(N):
    w = input()
    if  w == 'A':
        if vec == -1 and cnt != 0:
            finish()
        cnt += 1
        vec = 1
    else:
        if cnt == 0:
            finish()

        cnt -= 1
        vec = -1
if cnt != 0:
    finish()
print('YES')

Submission status: Wrong Answer

Solution code:
from collections import Counter as c
l=[input() for _ in range(int(input()))]
m=c(l)
if l[-1]!="Un":
    print("NO")
elif m["A"]==m["Un"]:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
from collections import Counter as c
l=[input() for _ in range(int(input()))]
m=c(l)
if l[-1]!="Un":
    print("NO")
elif m["A"]==m["Un"] and l[0]=="A":
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
from collections import Counter as c
l=[input() for _ in range(int(input()))]
m=c(l)
if l[-1]!="Un":
    print("NO")
elif m["A"]==m["Un"] and l[0]=="A":
    y=True
    ca=0
    cu=0
    for i in l:
        if i=="A":
            ca+=1
        elif i=="Un":
            cu+=1
        if ca!=cu:
            y=False
            break
        else:
            ca=0
            cu=0
    if y:print("YES")
    else:print("NO")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
from collections import Counter as c
l=[input() for _ in range(int(input()))]
m=c(l)
if l[-1]!="Un":
    print("NO")
elif m["A"]==m["Un"] and l[0]=="A":
    y=True
    ca=0
    cu=0
    for i in l:
        if i=="A":
            ca+=1
        elif i=="Un":
            cu+=1
        if ca!=cu and (ca and cu):
            y=False
            break
        else:
            ca=0
            cu=0
    if y and not ca and not cu:print("YES")
    else:print("NO")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
from collections import Counter as c
l=[input() for _ in range(int(input()))]
m=c(l)
if l[-1]!="Un":
    print("NO")
elif m["A"]==m["Un"] and l[0]=="A":
    y=True
    ca=0
    cu=0
    for i in l:
        if i=="A":
            ca+=1
        elif i=="Un":
            cu+=1
        if ca!=cu and (ca and cu):
            y=False
            break
        elif ca==cu:
            ca=0
            cu=0
    if y and not ca and not cu:print("YES")
    else:print("NO")
else:
    print("NO")

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    S = [input() for _ in range(N)]
    a_count = 1 if S[0] == "A" else -1
    # if N == 1 and a_count == -1:
    #     return "NO"
    for i in range(1, N):
        if a_count >= 0:
            a_count += 1 if S[i] == "A" else -1
        else:
            return "NO"
    if a_count == 0:
        return "YES"
    else:
        return "NO"


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

Solution code:
n=int(raw_input())
c=0
isValid = True
for _ in xrange(n):
    if raw_input()=="A": c+=1
    else: c-=1
    if c<0: isValid=False
if c!=0: isValid=False
if isValid: print "YES"
else: print "NO"

Submission status: Accepted

Solution code:
c = {'A': 0, 'Un': 0}
r = 'NO'
for _ in range(int(input())):
    c[input()] += 1
    if c['A']<c['Un']: break
else: r = 'YES' if c['A']==c['Un'] else 'NO'
print(r)

Submission status: Accepted

Solution code:
n = int(input())
A_cou = 0
Un_cou = 0
ans = "YES"
for i in range(n):
    sen = input()
    if sen == "A":
        A_cou += 1
    else:
        Un_cou += 1
        if A_cou < Un_cou:
            ans = "NO"
            break
if A_cou != Un_cou:
    ans = "NO"
print(ans)

Submission status: Accepted

Solution code:
n=int(input());c=0
for i in range(n):
	c+=1 if input()=='A'else -1
	if c<0:break
print("NO"if c else"YES")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
n=int(input())
a=0
for i in range(n):
    say=input()
    if(say=='A'):
         a+=1
    else:
        a-=1
        if(a<0):
            print("NO")
            sys.exit()
if a:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
n = input()
cnt = 0
while n:
    s = raw_input()
    cnt += 1-2*(s!='A')
    if cnt<0:break
    n-=1
else:
    if cnt==0:print "YES";exit()
print "NO"

Submission status: Accepted

Solution code:
a=0
for _ in range(int(input())):
    a+=[-1,1][input()=='A']
    if a<0:break
print('NO'if a else'YES')

Submission status: Accepted

Solution code:
a=0
for _ in range(int(input())):
    a+=[-1,1][input()=='A']
    if a<0:break
print(['YES','NO'][a!=0])

Submission status: Accepted

Solution code:
N = int(input())

a = 0
for i in range(N):
  S = input()
  if S == 'A':
    a += 1
  else :
    a -= 1

  if a < 0:
    print('NO')
    exit()
  
if a == 0:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
cnt = 0
for _ in range(int(input())):
    cnt += [-1, 1][input() == "A"]
    if cnt < 0:
        print("NO")
        quit()
print("YES" if cnt == 0 else "NO")

Submission status: Accepted

Solution code:
n = int(input())
lst = []
b = 0
for i in range(n):
    s = str(raw_input())
    lst.append(s)
    if s == "Un":
        if ("A" in lst) == True:
            lst.remove("A")
            lst.remove("Un")
        else:
            b = 1
if b != 0:
    print "NO"
else:
    if len(lst) != 0:
        print "NO"
    else:
        print "YES"

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    data = []
    for _ in range(n):data.append(input())
    p =[]
    state = True
    for i in data:
        if len(p) ==0 and i =='Un':state = False
        elif len(p) !=0 and i == 'Un': p.pop()
        if i =='A':p.append(1)
    if len(p) !=0:state = False
    if state :print("YES")
    else:print("NO")

Submission status: Accepted

Solution code:
N = int(input())
src = [input() for i in range(N)]
ans = a = 0
for s in src:
    if s == 'A': a += 1
    elif a > 0:
        a -= 1
    else:
        print('NO')
        break
else:
    print('YES' if a == 0 else 'NO')

Submission status: Accepted

Solution code:
N = int(input())
cnt = 0
flag = True
for i in range(N):
    S = input()
    if(S == 'A'):
        cnt += 1
    elif(S == 'Un'):
        cnt -= 1
        if(cnt < 0):
            flag = False
if(flag == False):
    print('NO')
else:
    if(cnt == 0):
        print('YES')
    else:
        print('NO')

Submission status: Accepted

Solution code:
N = int(raw_input())
S = [raw_input() for _ in range(N)]

cnt = 0
flag = True

for i in S:
    if i == "A":
        cnt += 1
    else:
        cnt -= 1;
    
    if cnt < 0:
        flag = False
        break

if cnt != 0:
    flag = False

print "YES" if flag else "NO"

Submission status: Accepted

Solution code:
n = int(input())
talk = [input() for i in range(n)]
a = 0
judge = True
for s in talk:
    if s == "A":
        a += 1
    else:
        if a == 0:
            judge = False
        else:
            a -= 1
if a > 0:
    judge = False

if judge:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
a = 0
for _ in range(int(input())):
    if input() == "A":a += 1
    else:
        a -= 1
        if a < 0:
            print("NO")
            break
else:
    print("YES" if a == 0 else "NO")

Submission status: Accepted

Solution code:
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**10
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():
    n = I()
    a = [S() for _ in range(n)]
    t = 0
    f = False
    for c in a:
        if c == 'A':
            t += 1
        elif t > 0:
            f = True
            t -= 1
        else:
            f = False
            break
    if t == 0 and f:
        return 'YES'


    return 'NO'


print(main())

Submission status: Accepted

Solution code:
N = int(input())

temp=0
dia_break=0

for _ in range(N):
    x = input()    
    if x=="A":
        temp += 1
    elif x=='Un':
        temp -= 1
    if temp < 0:
        dia_break = 1

if dia_break==0 and temp==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
def finish():
    print('NO')
    exit()

N = int(input())
cnt = 0
vec = -1
for i in range(N):
    w = input()
    if  w == 'A':
        #if vec == -1 and cnt != 0:
        #    finish()
        cnt += 1
        vec = 1
    else:
        if cnt == 0:
            finish()

        cnt -= 1
        vec = -1
if cnt != 0:
    finish()
print('YES')

Submission status: Accepted

Solution code:
ge = 0
be = False
for i in range(int(input())):
    if input() == "A":
        ge += 1
    else:
        ge -= 1
    if ge < 0:
        be = True
if be or ge != 0:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
a = int(input())

aa = 0

un = 0

p = 0

for i in range(a):
  k = input()
  if k == 'A':
    aa += 1
  elif k == 'Un':
    un += 1
  if un > aa:
    p = 1

if aa > un:
  p = 1
  
if p == 0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())
A = 0
for i in range(N):
	S = input()
	if S == "A":
		A += 1
		if i == N-1 or 2*A < i:
			print("NO")
			exit()

if N%2 == 1 or 2*A != N:
	print("NO")
else:
	print("YES")

Submission status: Accepted

Solution code:
n = int(input())
acc = 0
flag = False
for _ in range(n):
  s = input()
  if s == "A":
    acc += 1
  else:
    acc -= 1
  if acc < 0:
    flag = True
if acc != 0:
  flag = True

if flag:
  print("NO")
else:
  print("YES")

Submission status: Accepted

Solution code:
l=[input() for _ in range(int(input()))]
s=[]

isno=False
for i in l:
    if i=="A":
        s.append(1)
    else:
        try:
            _=s.pop()
        except:
            print("NO")
            isno=True
            break

if not isno and not s:print("YES")
elif not isno and s:print("NO")

Submission status: Accepted

Solution code:
def AUN(x):
    a = 0
    un = 0
    for i in range(len(x)):
        if x[i] == 'A':
            a += 1
        else:
            un += 1
    if a >= un:
        return True
def p(x):
    a = 0
    un = 0
    for i in range(len(x)):
        if x[i] == 'A':
            a += 1
        else:
            un += 1
    if a == un:
        return True
N = int(input())
b = []
d = []
e = []
for i in range(N):
    b.append(input())
if p(b):
    for i in range(N):
        d.append(b[i])
        e.append(AUN(d))
    if all(e) :
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
d = 0
isOK = True
for i in range(n):
	s = input()
	if s == "A": d += 1
	else: d -= 1
	if d < 0: isOK = False
if d != 0: isOK = False
print("YES" if isOK else "NO")

Submission status: Accepted

Solution code:
n = int(input())
s = [str(input()) for i in range(n)]
cnt = 0
for i in s:
    if i == "A":
        cnt += 1
    else:
        cnt -= 1
    if cnt < 0:
        print("NO")
        exit()
if cnt != 0:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
BEF_STR= "A"
AFT_STR = "Un"

def get_data():
    data = input()
    return data

def make_data_list(num):
    data_list = [None] * num
    for i in range(num):
        data_list[i] = get_data()
    return data_list

#A-Unでワンセットとなるかを確認する関数を作りたい
def check_STR1_to_STR2_correspondence(data_list):
    count = 0
    if len(data_list) % 2 != 0 or data_list[0] == AFT_STR:
        return False
    for s in data_list:
        if s == BEF_STR:
            count += 1
        else:
            count -= 1
            if count < 0:
                return False
    if count != 0:
        return False
    return True

if __name__ == "__main__":
    speak_sum = int(get_data())
    record_list = make_data_list(speak_sum)
    is_A_Un = check_STR1_to_STR2_correspondence(record_list)
    if is_A_Un is False:
        print("NO")
    else:
        print("YES")

Submission status: Accepted

Solution code:
a = 0
c = True
for i in range(int(input())):
    b = input()
    if b == "A":
        a += 1
    else:
        a -= 1
    if a == -1:
        print("NO")
        c = False
        break
if c == True:
    if a == 0:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
n = int(input())
ans = 'YES'
if n%2 == 1:
    ans = 'NO'
cnt = 0
for _ in range(n):
    s = input()
    if s == 'A':
        cnt += 1
    elif cnt == 0:
        ans='NO'
    else:
        cnt -= 1
if cnt != 0:
    ans = 'NO'
print(ans)

Submission status: Accepted

Solution code:
N= int(input())
count = 0
for i in range(N):
	s = input()
	if s == "A":
		count += 1
	if s == "Un":
		count -= 1
	if count < 0 :
		break
if count ==0:
	print("YES")
else:
	print("NO")

Submission status: Accepted

Solution code:
N = int(input())
a = 0
f = True
for _ in range(N):
  s = input()
  if s=="A":
    a += 1
  else:
    if a==0:
      f = False
    else:
      a -= 1
if f and a==0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for _ in range(N)]
first = 0
second = 0
ans = "YES"
for s in S:
    if s == "A":
        first += 1
    elif s == "Un":
        second += 1
    if first < second:
        ans = "NO"
        break
if first != second:
    ans = "NO"
print(ans)

Submission status: Accepted

Solution code:
a=u=0
for _ in range(int(input())):
    s=input()
    if s=='A':
        a+=1
    else:
        u+=1
    if a<u:
        print('NO')
        exit()
if a==u:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
talk = []
ans = "YES"
for i in range(n):
        a = input()
        if i == 0:
                talk.append(a)
                continue
        if a == "A":
                if len(talk) != 0 and talk[-1] == "Un":
                        ans = "NO"
                talk.append(a)
        else:
                if len(talk) == 0 or talk.pop() != "A":
                        ans = "NO"

if len(talk) != 0:
        ans = "NO"
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    count = {"A":0, "Un":0}
    for _ in range(n):
        s = input()
        count[s] += 1
        if count["A"] < count["Un"]:
            return False
    if count["A"] == count["Un"]:
        return True
    else:
        return False

if __name__ == '__main__':
    if main():
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
N = int(input())
S = list()
for i in range(N) :
	s = input()
	S.append(s)
		
while True :
	if(len(S) == 0) :
		print("YES")
		break
	elif("A" in S and "Un" in S) :
		if(S.index("A") < S.index("Un")) :
			del S[S.index("A")]
			del S[S.index("Un")]
		else :
			print("NO")
			break
	else :
		print("NO")
		break

Submission status: Accepted

Solution code:
N = int(input())

total = 0
for n in range(N):
    s = input()
    if s == 'A':
        total += 1
    else:
        total -= 1

    if total < 0:
        print("NO")
        exit()

if total == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
a = 0
un = 0
no = 0
for i in range(N):
    s = input()
    if s == "A":
        a += 1
    else:
        un += 1
    if un > a:
        print("NO")
        no = 1
        break
if a != un and not no:
    print("NO")
elif not no:
    print("YES")

Submission status: Accepted

Solution code:
n = int(input())
acnt = 0
ok = True
for _ in range(n):
    if input() == 'A':
        acnt += 1
    else:
        acnt -= 1
        if acnt < 0:
            ok = False
if acnt != 0:
    ok = False
print("YES" if ok else "NO")

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for i in range(n)]
cnt = 0
for e in s:
  if e=="A":
    cnt += 1
  else:
    cnt -= 1
  if cnt<0:
    print("NO")
    exit()
if cnt==0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()
    s = 0
    ans = "YES"
    for i in range(n):
        k = input()
        if k == "Un":
            s -= 1
        else:
            s += 1
        if s < 0:
            ans = "NO"
    if s:
        print("NO")
    else:
        print(ans)
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#I
def I_():
    return

#J
def J():
    return

#Solve
if __name__ == "__main__":
    A()

Submission status: Accepted

Solution code:
n=int(input())
A_c=0
Un_c=0
flag=0
for i in range(n):
    s=input()
    if s=="A":
        A_c+=1
    elif s=="Un":
        Un_c+=1
    if A_c<Un_c:flag=1
if A_c!=Un_c:flag=1
if flag==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())

remarks = []
for _ in range(n):
    remarks.append(input())

a = 0
flag = True
for remark in remarks:
    if remark == "A":
        a += 1
    else:
        if a == 0:
            flag = False
            break
        else:
            a -= 1
else:
    if a != 0:
        flag = False

if flag:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
c=0
for _ in range(int(input())):
    c+=2*(input()=="A")-1
    if c<0:
        print("NO")
        break
if c==0:
    print("YES")
elif c>0:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
s_list = []
for ii in range(n):
    ss = input()
    s_list.append(ss)
c=0
for s in s_list:
    if(s=="A"):
        c+=1     
    else:
        c-=1
        if(c<0):
            break
if(c==0):
    print("YES")
else:
    print("NO")

Submission status: Runtime Error

Solution code:
while 1:
    n = int(input())
    if n == 0:
        break
    lst = []
    b = 0
    for i in range(n):
        s = str(raw_input())
        lst.append(s)
        if s == "Un":
            if ("A" in lst) == True:
                lst.remove("A")
                lst.remove("Un")
            else:
                b = 1
    if b != 0:
        print "NO"
    else:
        if len(lst) != 0:
            print "NO"
        else:
            print "YES"

Submission status: Runtime Error

Solution code:
a = int(input())

aa = 0

un = 0

p = 0

for i in range(a):
  k = input()
  if k == 'A':
    aa += 1
  elif k == 'Un':
    un += 1
  if un > aa:
    p = 1

if aa > un:
  p = 1
  
if p = 0:
  print("YES")
else:
  print("NO")

Submission status: Runtime Error

Solution code:
from collections import Counter as c
l=[input() for _ in range(int(input()))]
m=c(l)
if l[-1]!="Un":
    print("NO")
elif m["A"]==m["Un"] and l[0]=="A":
    y=True
    for i in l:
        if i=="A":
            ca+=1
	elif i=="Un":
	    cu+=1
	if ca!=cu:
	    y=False
	    break
	else:
	    ca=0
	    cu=0
    if y:print("YES")
    else:print("NO")
else:
    print("NO")

Submission status: Runtime Error

Solution code:
from collections import Counter as c
l=[input() for _ in range(int(input()))]
m=c(l)
if l[-1]!="Un":
    print("NO")
elif m["A"]==m["Un"] and l[0]=="A":
    y=True
    ca=0
    cu=0
    for i in l:
        if i=="A":
            ca+=1
	elif i=="Un":
	    cu+=1
	if ca!=cu:
	    y=False
	    break
	else:
	    ca=0
	    cu=0
    if y:print("YES")
    else:print("NO")
else:
    print("NO")

Submission status: Runtime Error

Solution code:
l=[int(input()) for _ in range(int(input()))]
s=[]

isno=False
for i in l:
    if i=="A":
        s.append(1)
    else:
        try:
            _=s.pop()
        except:
            print("NO")
            isno=True
            break

if not isno and not s:print("YES")
elif not isno and s:print("NO")