Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

Solution code:
#import sys
#input = sys.stdin.readline
def one(D, S):
    d = {"A":0, "B":1, "C":2}
    ANS = []
    for a, b in S:
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        else:
            D[x] -= 1
            D[y] += 1
            ANS.append(b)
    print("\n".join(ANS))
            
def two(D,S,N):
    d = {"A":0, "B":1, "C":2}
    e = {0:"A", 1:"B",2:"C"}
    ANS = []
    for i in range(N):
        a, b = S[i]
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        elif D[y] == 0: 
            D[x] -= 1
            D[y] += 1
            ANS.append(b)
        else: #(1,1)
            if i+1 > N-1:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
                continue
            if S[i] == S[i+1]:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
                continue
            if not a in S[i+1]:
                x,y = y, x
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
                
    print("\n".join(ANS))
    
def main():
    N, A, B, C = map( int, input().split())
    S = [ tuple( input()) for _ in range(N)]
    d = {"A":0, "B":1, "C":2}
    D = [A,B,C]
    ANS = []
    if sum(D) == 1:
        one(D, S)
        return
    if sum(D) == 2:
        two(D,S,N)
        return
    for a, b in S:
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] <= 1:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        else:
            D[x] -= 1
            D[y] += 1
            ANS.append(b)

    print("Yes")
    print("\n".join(ANS))
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
d = {'A':a, 'B':b, 'C':c}
ns = []
for i in range(n):
    ns.append(input())
path = []
flag = True
for i in ns:
    x, y = i
    if d[x] >= d[y]:
        d[x] -= 1
        d[y] += 1
        if d[x] < 0:
            print('No')
            flag = False
            break
        path.append(y)
    else:
        d[x] += 1
        d[y] -= 1
        path.append(x)
if flag:
    print('Yes')
    for i in path:
        print(i)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int, input().split( ))
ans=[]
for _ in range(n):
    s=input()
    if s=="AB":
        if a>b:
            a-=1;b+=1
            ans.append("B")
        else:
            a+=1;b-=1
            ans.append("A")
    elif s=="BC":
        if b>c:
            b-=1;c+=1
            ans.append("C")
        else:
            b+=1;c-=1
            ans.append("B")
    else:
        if c>a:
            c-=1;a+=1
            ans.append("A")
        else:
            c+=1;a-=1
            ans.append("C")
    if a<0 or b<0 or c<0:
        print("No")
        exit()
print("Yes")
for ai in ans:
    print(ai)

Submission status: Wrong Answer

Solution code:
n, a, b, c = (int(i) for i in input().split())
poss = True
ans = ""
for i in range(n):
	s = input()
	if s=="AB":
		if a>b:
			b+=1
			a-=1
			ans += "B"
		else:
			b-=1
			a+=1
			ans+= "A"
		if a<0 or b<0:
			poss = False
			break
	if s=="AC":
		if a>c:
			c+=1
			a-=1
			ans += "C"
		else:
			c-=1
			a+=1
			ans+= "A"
		if a<0 or c<0:
			poss = False
			break
	if s=="BC":
		if b>c:
			c+=1
			b-=1
			ans += "C"
		else:
			c-=1
			b+=1
			ans+= "B"
		if b<0 or c<0:
			poss = False
			break
if poss:
	print("Yes")
	for c in ans:
		print(c)
else:
	print("No")

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
S = [0]*N
X = [A,B,C]
result = 'Yes'
tmp2 = []
for i in range(N):
	S[i] = input()
tmp = ['A','B','C']


for i in range(N):
	tmp2.append(tmp[X.index(min(X[tmp.index(S[i][0])],X[tmp.index(S[i][1])]))])
	X[X.index(min(X[tmp.index(S[i][0])],X[tmp.index(S[i][1])]))] += 1 
	X[X.index(max(X[tmp.index(S[i][0])],X[tmp.index(S[i][1])]))] -= 1
      
for i in X:
	if i <0:
		result ='No'
if result == 'Yes':
	print(result)
	for i in range(N):
		print(tmp2[i])
else :print('No')

Submission status: Wrong Answer

Solution code:
N,A,B,C = [int(hoge) for hoge in input().split()]
ans = []
for i in range(N):
    q = input()
    if q == "AB":#AB
        if A > B:
            B += 1
            A -= 1
            ans.append("B")
        else:
            B -= 1
            A += 1
            ans.append("A")
    elif q == "AC":
        if A > C:
            A -= 1
            C += 1
            ans.append("C")
        else:
            A += 1
            C -= 1
            ans.append("A")
    else:
        if B > C:
            B -= 1
            C += 1
            ans.append("C")
        else:
            B += 1
            C -= 1
            ans.append("B")
    if A<0 or B<0 or C<0:
        print("No")
        exit()
print("Yes")
for a in ans:print(a)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
S=[]
for i in range(n):
  S.append(input())
if a==0 and b==0:
  if S[0]=="AB":
    print("No")
    exit()
if a==0 and c==0:
  if S[0]=="AC":
    print("No")
    exit()
if c==0 and b==0:
  if S[0]=="BC":
    print("No")
    exit()
ans=[]
f=0
i=0
print("Yes")
for i in range(n):
  if [a,b,c]==[1,1,0] and S[i]=="AB":
    if i+1<n:
      if S[i+1]=="AB":
        print("A")
        a=a+1
        b=b-1
      elif S[i+1]=="AC":
        print("A")
        a=a+1
        b=b-1
      else:
        print("B")
        a=a-1
        b=b+1
    else:
      print("A")
      a=a+1
      b=b-1
  elif [a,b,c]==[1,0,1] and S[i]=="AC":
    if i+1<n:
      if S[i+1]=="AC":
        print("A")
        a=a+1
        c=c-1
      elif S[i+1]=="AB":
        print("A")
        a=a+1
        c=c-1
      else:
        print("C")
        a=a-1
        c=c+1
    else:
      print("A")
      a=a+1
      c=c-1
  elif [a,b,c]==[0,1,1] and S[i]=="BC":
    if i+1<n:
      if S[i+1]=="BC":
        print("C")
        c=c+1
        b=b-1
      elif S[i+1]=="AC":
        print("C")
        c=c+1
        b=b-1
      else:
        print("B")
        b=b+1
        c=c-1
    else:
      print("C")
      c=c+1
      b=b-1
  elif S[i]=="AB":
    if a>=b:
      print("B")
      a=a-1
      b=b+1
    else:
      print("A")
      a=a+1
      b=b-1
  elif S[i]=="AC":
    if a>=c:
      print("C")
      a=a-1
      c=c+1
    else:
      print("A")
      a=a+1
      c=c-1
  elif S[i]=="BC":
    if c>=b:
      print("B")
      c=c-1
      b=b+1
    else:
      print("C")
      c=c+1
      b=b-1

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())

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

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        omake = list(set(list(s)) - set(list(before)))[0]
        for i in range(co):
            if co % 2 == 0:
                yusen.append(target)
            else:
                yusen.append(omake)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")

ansl = []
for s in sl:
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if yusen == "A":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if yusen == "B":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if yusen == "C":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)

Submission status: Wrong Answer

Solution code:

N,A,B,C = map(int,input().split())

nums = [A,B,C]

prev = [((A,B,C),None)]


res = []
flag = True
for _ in range(N):
    s = input()
    if s == 'AB':
        i,j = 0,1
    elif s == 'BC':
        i,j = 1,2
    else:
        i,j = 2,0

    best_min = -1
    best_k = None
    for k,(l,choice) in enumerate(prev):
        nl = list(l)
        nl[i] -= 1
        nl[j] += 1
        m = min(nl)
        if best_min < m:
            best_min = m
            best_k = k
        nl[i] += 2
        nl[j] -= 2
        m = min(nl)
        if best_min < m:
            best_min = m
            best_k = k
    if best_min < 0:
        flag = False
        break

    nprev = []
    l,choice = prev[k]
    res.append(choice)
    nl = list(l)
    nl[i] -= 1
    nl[j] += 1
    nprev.append((tuple(nl), j))
    nl[i] += 2
    nl[j] -= 2
    nprev.append((tuple(nl), i))
    prev = nprev

if not flag:
    print('No')
else:
    print('Yes')
    if min(prev[0][0]) >= 0:
        res.append(prev[0][1])
    else:
        res.append(prev[1][1])
    res = tuple(str(chr(v+ord('A'))) for v in res[1:])
    print(*res,sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, a, b, c = map(int, input().split())
nums = [a, b, c]
queries = []
for i in range(n):
    s = input()
    if 'AB' in s:
        queries.append((0, 1))
    elif 'AC' in s:
        queries.append((0, 2))
    else:
        queries.append((1, 2))

choices = []
for i in range(n):
    p, q = queries[i]
    if nums[p] > nums[q]:
        nums[p] -= 1
        nums[q] += 1
        if min(nums) < 0:
            break
        choices.append(q)
    else:
        nums[q] -= 1
        nums[p] += 1
        if min(nums) < 0:
            break
        choices.append(p)

if min(nums) < 0:
    print('No')
else:
    print('Yes')
    for choice in choices:
        print('ABC'[choice])

Submission status: Wrong Answer

Solution code:

n,a,b,c=map(int,input().split())
l=[]
flag='Yes'
for i in range(n):
	s=input()
	if s=='AB' or s=='BA':
		if a==0 and b==0:
			flag='No'
		else:
			if a>b:
				a=a-1
				b+=1
				l.append('B')
			else:
				a+=1
				b-=1
				l.append('A')
	elif s=='BC' or s=='CB':
		if c==0 and b==0:
			flag='No'
		else:
			if c>b:
				c=c-1
				b+=1
				l.append('B')
			else:
				c+=1
				b-=1
				l.append('C')
	if s=='AC' or s=='CA':
		if a==0 and c==0:
			flag='No'
		else:
			if a>c:
				a=a-1
				c+=1
				l.append('C')
			else:
				a+=1
				c-=1
				l.append('A')
if flag=='Yes':
	print(flag)
	for i in l:
		print(i)
else:
	print(flag)

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
S = [input() for i in range(N)]

ans = []
for s in S:
    if s=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        if A==0:
            ans.append('A')
            A += 1
            B -= 1
        elif B==0:
            ans.append('B')
            B += 1
            A -= 1
        else:
            if A>B:
                ans.append('B')
                B += 1
                A -= 1
            else:
                ans.append('A')
                A += 1
                B -= 1
    elif s=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        if A==0:
            ans.append('A')
            A += 1
            C -= 1
        elif C==0:
            ans.append('C')
            C += 1
            A -= 1
        else:
            if A>C:
                ans.append('C')
                C += 1
                A -= 1
            else:
                ans.append('A')
                A += 1
                C -= 1
    else:
        if C==0 and B==0:
            print('No')
            exit()
        if C==0:
            ans.append('C')
            C += 1
            B -= 1
        elif B==0:
            ans.append('B')
            B += 1
            C -= 1
        else:
            if C>B:
                ans.append('B')
                B += 1
                C -= 1
            else:
                ans.append('C')
                C += 1
                B -= 1
print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
q = [input() for i in range(N)]

ans = []
possible = True
for i in range(N):
    s = q[i]
    if s=='AB':
        if B>1:
            A += 1
            B -= 1
            ans.append('A')
        else:
            A -= 1
            B += 1
            ans.append('B')
            if A<0:
                possible = False
    elif s=='AC':
        if C>1:
            A += 1
            C -= 1
            ans.append('A')
        else:
            A -= 1
            C += 1
            ans.append('C')
            if A<0:
                possible = False
    else:
        if B==0 and C==0:
            possible = False
        if B>=C:
            B -= 1
            C += 1
            ans.append('C')
        else:
            B += 1
            C -= 1
            ans.append('B')

if possible:
    print('Yes')
    for i in range(N):
        print(ans[i])
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
D = {"AB": 2, "AC": 1, "BC": 0}
N, a, b, c = map(int, input().split())
X = [a, b, c]
S = [D[input()] for _ in range(N)]
ANS = []
if max(X) == 0:
    print("No")
    exit()

if sum(X) == 1:
    t = 0 if a else 1 if b else 2
    for s in S:
        if X[s]:
            print("No")
            exit()
        t ^= s ^ 3
        ANS.append(t)
        X[s-1] ^= 1
        X[s-2] ^= 1
    print("Yes")
    print("\n".join([chr(t+65) for t in ANS]))
    exit()

s = S[0]
if X[s-1] + X[s-2] == 0:
    print("No")
    exit()

print("No")
exit()
for i, s in enumerate(S):
    if X[s-1] > X[s-2]:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    elif X[s-1] < X[s-2]:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1
    elif i == N - 1 or S[i+1] == s-1:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    else:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1

print("Yes")
print("\n".join([chr(t+65) for t in ANS]))

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    ab = [0]*N
    ac = [0]*N
    bc = [0]*N
    l = []
    for i in range(N):
        s = input()
        ab[i] = ab[i-1]
        ac[i] = ac[i-1]
        bc[i] = bc[i-1]
        if s == 'AB':
            ab[i] += 1
            l.append('AB')
        elif s == 'AC':
            ac[i] += 1
            l.append('AC')
        else:
            bc[i] += 1
            l.append('BC')
    AB = ab[-1]
    BC = bc[-1]
    AC = ac[-1]
    r = []
    for i in range(N):
        if l[i] == 'AB':
            if A == 0 and B == 0:
                print('No')
                return
            elif A == 0:
                B -= 1
                r.append('B')
            elif B == 0:
                A -= 1
                r.append('A')
            else:
                if BC - bc[i] - B > AC - ac[i] - A:
                    A -= 1
                    r.append('A')
                else:
                    B -= 1
                    r.append('B')
        if l[i] == 'AC':
            if A == 0 and C == 0:
                print('No')
                return
            elif A == 0:
                C -= 1
                r.append('C')
            elif C == 0:
                A -= 1
                r.append('A')
            else:
                if BC - bc[i] - C > AB - ab[i] - A:
                    A -= 1
                    r.append('A')
                else:
                    C -= 1
                    r.append('C')
        if l[i] == 'BC':
            if C == 0 and B == 0:
                print('No')
                return
            elif C == 0:
                B -= 1
                r.append('B')
            elif B == 0:
                C -= 1
                r.append('C')
            else:
                if AB - ab[i] - B> AC - ac[i] - C:
                    C -= 1
                    r.append('C')
                else:
                    B -= 1
                    r.append('B')
    print('Yes')
    for i in r:
        print(i)
main()

Submission status: Wrong Answer

Solution code:
def no():
    print("No")
    exit()

n, a, b, c = map(int, input().split())
ans = []
s = [input() for _ in range(n)]
for i in range(n):
    if s[i] == "AB":
        if a == b == 1 and not i == n - 1:
            if (not c == 0) or (s[i + 1] == "AB"):
                a += 1
                b -= 1
                ans.append("A")
            else:
                if s[i + 1] == "AC":
                    a += 1
                    b -= 1
                    ans.append("A")
                else:
                    b += 1
                    a -= 1
                    ans.append("B")
        elif a + b > 0:
            if a <= b:
                a += 1
                b -= 1
                ans.append("A")
            else:
                b += 1
                a -= 1
                ans.append("B")
        else:
            no()
    elif s[i] == "BC":
        if b == c == 1 and not i == n - 1:
            if (not a == 0) or (s[i + 1] == "BC"):
                b += 1
                c -= 1
                ans.append("B")
            else:
                if s[i + 1] == "AB":
                    b += 1
                    c -= 1
                    ans.append("B")
                else:
                    c += 1
                    b -= 1
                    ans.append("C")
        elif b + c > 0:
            if b <= c:
                b += 1
                c -= 1
                ans.append("B")
            else:
                c += 1
                b -= 1
                ans.append("C")
        else:
            no()
    elif s[i] == "AC":
        if c == a == 1 and not i == n - 1:
            if (not b == 0) or (s[i + 1] == "AB"):
                c += 1
                a -= 1
                ans.append("C")
            else:
                if s[i + 1] == "BC":
                    c += 1
                    a -= 1
                    ans.append("C")
                else:
                    a += 1
                    c -= 1
                    ans.append("A")
        elif c + a > 0:
            if c <= a:
                c += 1
                a -= 1
                ans.append("C")
            else:
                a += 1
                c -= 1
                ans.append("A")
        else:
            no()
print("Yes")
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())

i = 0
ans = []
tower = [a,b,c]
s2num = {"AB":[0,1],"BC":[1,2],"AC":[0,2]}
num2s = {0:"A",1:"B",2:"C"}
flag =True

def check_large(l,r,t):
    if t[l]>t[r]:
        return l,r
    else:
        return r,l

for i in range(n):
    s = input()
    l,r = s2num[s]
    big,small = check_large(l,r,tower)
    if tower[big] <= 0:
        flag = False
        break
    else:
        tower[big]-=1
        tower[small]+=1
        ans.append(num2s[small])

if flag:
    print("Yes")
    for i in range(n):
        print(ans[i])
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N, A, B, C = map(int, input().split())
x = y = z = 0
ans = []
aa = bb = cc = 0
for _ in range(N):
    s = input().rstrip()
    if s == 'AB': x += 1
    if s == 'BC': y += 1
    if s == 'AC': z += 1
    a = (z + x - A + 1) // 2
    b = (x + y - B + 1) // 2
    c = (y + z - C + 1) // 2
    if max(a, 0) + max(b, 0) + max(c, 0) > x + y + z:
        print('No')
        exit()
    if a > aa:
        aa += 1
        ans.append('A')
    elif b > bb:
        bb += 1
        ans.append('B')
    elif c > cc:
        cc += 1
        ans.append('C')
    else:
        if s == 'AB':
            if a > b:
                aa += 1
                ans.append('A')
            else:
                bb += 1
                ans.append('B')
        if s == 'BC':
            if b > c:
                bb += 1
                ans.append('B')
            else:
                cc += 1
                ans.append('C')
        if s == 'AC':
            if a > c:
                aa += 1
                ans.append('A')
            else:
                cc += 1
                ans.append('C')
print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
l=[]
flag='YES'
for i in range(n):
	s=input()
	if s=='AB' or s=='BA':
		if a==0 and b==0:
			flag='NO'
		else:
			if a>b:
				a=a-1
				b+=1
				l.append('B')
			else:
				a+=1
				b-=1
				l.append('A')
	elif s=='BC' or s=='CB':
		if c==0 and b==0:
			flag='NO'
		else:
			if c>b:
				c=c-1
				b+=1
				l.append('B')
			else:
				c+=1
				b-=1
				l.append('C')
	if s=='AC' or s=='CA':
		if a==0 and c==0:
			flag='NO'
		else:
			if a>c:
				a=a-1
				c+=1
				l.append('C')
			else:
				a+=1
				c-=1
				l.append('A')
if flag=='YES':
	print(flag)
	for i in l:
		print(i)
else:
	print(flag)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
ans = []
D = {'A':a, 'B':b, 'C':c}
sx = SR(n)
for i in range(n):
    s = sx[i]
    s1 = s[0]
    s2 = s[1]
    if D[s1] == D[s2] == 0:
        print('No')
        exit()
    elif D[s[0]] == D[s[1]] == 1:
        if i + 1 < n:
            if s1 in sx[i + 1]:
                D[s1] -= 1
                D[s2] += 1
                ans += [s2]
            else:
                D[s1] += 1
                D[s1] -= 1
                ans += [s1]
    else:
        if D[s1] == 0:
            ans += [s1]
        else:
            ans += [s2]

print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
n,a,b,c = map(int, input().split())
s=[input() for _ in range(n)]
num = [a,b,c]
ans = ''

def f(st):
    if st == 'A':
        return 0
    elif st == 'B':
        return 1
    else:
        return 2

if a+b+c == 0:
    print('No')
    sys.exit()
    
if a+b+c == 1:
    for i in range(n):
        x,y=f(s[i][0]),f(s[i][1])
        if num[x] == 0 and num[y] == 0:
            print('No')
            sys.exit()
        else:
            if num[x] == 0:
                ans += (s[i][0] + '\n')
                num[x] += 1
                num[y] -= 1
                
            else:
                ans += (s[i][1] + '\n')
                num[y] += 1
                num[x] -= 1
        #print(ans)
    print('Yes')
    print(ans[:-1],end='')
    sys.exit()

if (a+b+c==a and s[0]=='BC') or (a+b+c==b and s[0]=='AC') or (a+b+c==c and s[0]=='AC'):
    print('No')
    sys.exit()
    

for i in range(n):
    x,y=f(s[i][0]),f(s[i][1])
    if num[x] == 0 and num[y] == 0:
        print('No')
        sys.exit()
    
    else:
        if num[x] == 0:
            ans += (s[i][0] + '\n')
            num[x] += 1
            num[y] -= 1
            
        elif num[y] == 0:
            ans += (s[i][1] + '\n')
            num[y] += 1
            num[x] -= 1
        
        elif num[x] == 1:
            ans += (s[i][0] + '\n')
            num[x] += 1
            num[y] -= 1
            
        else:
            ans += (s[i][1] + '\n')
            num[y] += 1
            num[x] -= 1
    
print('Yes')
print(ans[:-1],end='')
sys.exit()

Submission status: Wrong Answer

Solution code:
import sys
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
score = {"A":a, "B":b, "C":c}

result = []
for g in s:
    x, y = list(g)
    if score[x] <= score[y]:
        score[y] -= 1
        score[x] += 1
        result.append(x)
    else:
        score[x] -= 1
        score[y] += 1
        result.append(y)
    if score[x] < 0 or score[y] < 0:
        print("No")
        sys.exit()
print("Yes")
for i in result:
    print(i)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N,A,B,C = map(int, input().split())
list_choice = [input() for i in range(N)]
list_choice.append('AB')

def operation(A,B,C,S,nextS):
  if S=='AB':
    if max(A,B)==0:
      return ('No','',0,0,0)
    elif A==1 and B==1:
      if nextS=='AB' or nextS=='AC':
        return ('Yes','A',A+1,B-1,C)
      else:
        return ('Yes','B',A-1,B+1,C)  
    elif A>=B:
      return ('Yes','B',A-1,B+1,C)
    else: # A<B
      return ('Yes','A',A+1,B-1,C)
  elif S=='AC':
    if max(A,C)==0:
      return ('No','',0,0,0)
    elif A==1 and C==1:
      if nextS=='AB' or nextS=='AC':
        return ('Yes','A',A+1,B,C-1)
      else:
        return ('Yes','C',A-1,B,C+1)  
    elif A>=C:
      return ('Yes','C',A-1,B,C+1)
    else: # A<C
      return ('Yes','A',A+1,B,C-1)
  else: # S=='BC'
    if max(A,C)==0:
      return ('No','',0,0,0)
    elif B==1 and C==1:
      if nextS=='AB' or nextS=='BC':
        return ('Yes','B',A,B+1,C-1)
      else:
        return ('Yes','C',A,B-1,C+1)  
    elif B>=C:
      return ('Yes','C',A,B-1,C+1)
    else: # B<C
      return ('Yes','B',A,B+1,C-1)
  
def func(A,B,C,list_choice):
  nonzero = min(1,A)+min(1,B)+min(1,C)
  if nonzero<=1:
    return ('No', [])
  else:
    list_add = []
    for i in range(N):
      answer,add,A,B,C = operation(A,B,C,list_choice[i],list_choice[i+1])
      if answer=='No':
        return ('No', [])
      else:
        list_add.append(add)
    return ('Yes',list_add)
    
  
answer, list_add = func(A,B,C,list_choice)
print(answer)
if answer == 'Yes':
  for add in list_add:
    print(add)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())

i = 0
ans = []
tower = [a,b,c]
s2num = {"AB":[0,1],"BC":[1,2],"AC":[0,2]}
num2s = {0:"A",1:"B",2:"C"}
flag =True

def check_large(l,r,t):
    if t[l]>t[r]:
        return l,r
    else:
        return r,l

for i in range(n):
    s = input()
    l,r = s2num[s]
    big,small = check_large(l,r,tower)
    if tower[big] <= 0:
        flag = False
        break
    else:
        tower[big]-=1
        tower[small]+=1
        ans.append(num2s[small])

if flag:
    print("YES")
    for i in range(n):
        print(ans[i])
else:
    print("NO")