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:
n,a,b,c = map(int,input().split())
abc = {'A':a,'B':b,'C':c}
ans = []

if a+b+c==0:
    for _ in range(n):
        s = input()
    print('No')
elif a+b+c==1:
    for _ in range(n):
        s = input()
        x,y = s[0],s[1]
        if abc[x]==abc[y]:
            print('No')
            break
        elif abc[x]==1:
            abc[x] -=1
            abc[y] +=1
            ans.append(y)
        else:
            abc[x] +=1
            abc[y] -=1
            ans.append(x)
elif a+b+c==2:
    s = input()
    x,y = s[0],s[1]
    if abc[x]==0 and abc[y]==0:
        print('No')
    else:
        for _ in range(n-1):
            s_ = input()
            x_,y_ = s_[0],s_[1]
            if abc[x]==abc[y]:
                if x==x_ or (x!=x_ and y!=y_ and x == 'B'):
                    abc[x] +=1
                    abc[y] -=1
                    ans.append(x)
                else:
                    abc[x] -=1
                    abc[y] +=1
                    ans.append(y)
            elif abc[x]==2:
                abc[x] -=1
                abc[y] +=1
                ans.append(y)
            else:
                abc[x] +=1
                abc[y] -=1
                ans.append(x)
            x,y=x_,y_
        if abc[x]>=abc[y]:
            ans.append(y)
        else:
            ans.append(x)
else:
    s = input()
    x,y = s[0],s[1]
    if abc[x]==0 and abc[y]==0:
        print('No')
    else:
        for _ in range(n-1):
            if abc[x]>=abc[y]:
                abc[x] -=1
                abc[y] +=1
                ans.append(y)
            else:
                abc[x] +=1
                abc[y] -=1
                ans.append(x)
            s = input()
            x,y = s[0],s[1]
        if abc[x]>=abc[y]:
            ans.append(y)
        else:
            ans.append(x)
                
if len(ans)==n:
    print('Yes')
    print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
#F
N,A,B,C = map(int,input().split())
S =[]
for x in range(N):
    S.append(input())
ans =[]
f =True

for x in range(N):
    
    if A==0 and B==0 and C ==0:
        f =False
        break
    
    elif A+B+C ==1:
        if S[x] =="AB" and A==1:
            ans.append("B")
            A -=1
            B +=1
        elif S[x] =="AB" and B==1:
            ans.append("A")
            A +=1
            B -=1
        elif S[x] =="AC" and A==1:
            ans.append("C")
            A -=1
            C +=1
        elif S[x] =="AC" and C==1:
            ans.append("A")
            A +=1
            C -=1
        elif S[x] =="BC" and B==1:
            ans.append("C")
            B -=1
            C +=1
        elif S[x] =="BC" and C==1:
            ans.append("B")
            B +=1
            C -=1
        else:
            f =False
            break
    elif A +B+C >= 2:
        if A==0 or B ==0 or C==0:
            if S[x]=="AB" and A ==0 and B == 0:
                f =False
                break
            elif S[x]=="AC" and A ==0 and C == 0:
                f =False
                break
            elif S[x]=="BC" and C ==0 and B == 0:
                f =False
                break
        if S[x] =="AB" and B == 0:
                ans.append("B")
                A -=1
                B +=1
        elif S[x] =="AB" and A ==0:
                ans.append("A")
                A +=1
                B =1
        elif S[x] =="AC" and C ==0:
                ans.append("C")
                A -=1
                C +=1
        elif S[x] =="AC" and A ==0:
                ans.append("A")
                A +=1
                C -=1    
        elif S[x] =="BC" and C ==0:
                ans.append("C")
                B -=1
                C +=1
        elif S[x] =="BC" and B == 0 :
                ans.append("B")
                B +=1
                C -=1 

        elif A+B+C ==2 and x <N-1 and S[x]!=S[x+1]:
            if S[x+1] =="AC":
                ans.append("A")
                A +=1
                B -=1
            elif S[x+1] =="BC":
                ans.append("B")
                A -=1
                B +=1
            elif S[x+1] =="BC":
                ans.append("C")
                A -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("A")
                A +=1
                C -=1
            elif S[x+1] =="AC":
                ans.append("C")
                B -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("B")
                B +=1
                C -=1
                
        else:
            if S[x] =="AB":
                ans.append("A")
                A +=1
                B -=1
            elif S[x] =="AC":
                ans.append("A")
                A +=1
                C -=1    
            elif S[x] =="BC":
                ans.append("B")
                B +=1
                C -=1

            
    
if f ==True:
    print("Yes")
    for x in ans:
        print(x)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, raw_input().split())
cmds = []

def getidx(c):
    return 'ABC'.index(c)

g = [ [], [], [] ]
for i in xrange(n):
    s = raw_input()
    cmds.append(s)
    g[getidx(s[0])].append(i)
    g[getidx(s[1])].append(i)

def getnext(c, idx):
    v = g[getidx(c)]
    l, r = 0, len(v) - 1
    while l <= r:
        m = (l + r) / 2
        if v[m] < idx:
            l = m + 1
        else:
            r = m - 1
    return v[l]

cnt = [a, b, c]
res = []


for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1 or cnt[p1] == 0 or cnt[p2] == 0:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        if min(cnt) < 0:
            res = []
            break
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        idx1 = getnext(cmd[0], i)
        idx2 = getnext(cmd[1], i)

        if idx1 < idx2:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
    assert min(cnt) >= 0

if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    n,A,B,C = map(int, readline().split())
    N = A + B + C
    S = [readline().strip() for _ in range(n)]

    ans = []
    possible = True
    for i, s in enumerate(S):
        if s == "AB":
            if A + B == 0:
                print("No")
                possible = False
                break
            
            if i < n - 1 and N == 2 and A == B == 1:
                if S[i+1] == "AC":
                    A += 1
                    B -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    B += 1
                    ans.append("B")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            else:
                if A < B:
                    A += 1
                    B -= 1
                    ans.append("A")
                else:
                    A -= 1
                    B += 1
                    ans.append("B")

        elif s == "AC":
            if A + C == 0:
                print("No")
                possible = False
                break
            
            if i < n - 1 and N == 2 and A == C == 1:
                if S[i+1] == "AB":
                    A += 1
                    B -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    C += 1
                    ans.append("C")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            else:
                if A < C:
                    A += 1
                    C -= 1
                    ans.append("A")
                else:
                    A -= 1
                    C += 1
                    ans.append("C")

        else:
            if B + C == 0:
                print("No")
                possible = False
                break
            
            if i < n - 1 and N == 2 and B == C == 1:
                if S[i+1] == "AB":
                    B += 1
                    C -= 1
                    ans.append("B")
                elif S[i+1] == "AC":
                    B -= 1
                    C += 1
                    ans.append("C")
                else:
                    B += 1
                    C -= 1
                    ans.append("B")
            else:
                if B < C:
                    B += 1
                    C -= 1
                    ans.append("B")
                else:
                    B -= 1
                    C += 1
                    ans.append("C")

    if possible:
        print("Yes")
        print("\n".join(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

S = ['']*N
for i in range(N):
  S[i] = input()
  
play = [''] * N
flag = True
for i, s in enumerate(S):
  next = ''
  if i < len(S)-1:
    next = S[i+1]
  if s == 'AB':
    if A == 0 and B == 0:
      flag = False
      break
    elif A > B and 'B' in next:
      A -= 1
      B += 1
      play[i] = 'B'
    else:
      A += 1
      B -= 1
      play[i] = 'A'
  elif s == 'AC':
    if A == 0 and C == 0:
      flag = False
      break
    elif A > C and 'C' in next:
      A -= 1
      C += 1
      play[i] = 'C'
    else:
      A += 1
      C -= 1
      play[i] = 'A'
  else:
    if B == 0 and C == 0:
      flag = False
      break
    elif B > C and 'C' in next:
      B -= 1
      C += 1
      play[i] = 'C'
    else:
      B += 1
      C -= 1
      play[i] = 'B'

if flag:
  print('Yes')
  for i in play:
    print(i)
else:
  print('No')

Submission status: Wrong Answer

Solution code:
import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, raw_input().split())
cmds = []

def getidx(c):
    return 'ABC'.index(c)

g = [ [], [], [] ]
for i in xrange(n):
    s = raw_input()
    cmds.append(s)
    g[getidx(s[0])].append(i)
    g[getidx(s[1])].append(i)

def getnext(c, idx):
    l, r = 0, len(g[getidx(c)]) - 1
    while l <= r:
        m = (l + r) / 2
        if m < idx:
            l = m + 1
        else:
            r = m - 1
    return l

cnt = [a, b, c]
res = []


for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1 or cnt[p1] == 0 or cnt[p2] == 0:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        if min(cnt) < 0:
            res = []
            break
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        idx1 = getnext(cmd[0], i)
        idx2 = getnext(cmd[1], i)

        if idx1 < idx2:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
    assert min(cnt) >= 0

if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
ss=['' for _ in range(n)]
ans=['' for _ in range(n)]
wa=a+b+c
for i in range(n):
    s=input()
    ss[i]=s
i=0
if wa==0:
    print('No')
    exit()
elif wa==1:
    for s in ss:
        if s=='AB':
            if c:
                print('No')
                exit()
            elif b:
                a+=1
                b-=1
                ans[i]='A'
            elif a:
                b+=1
                a-=1
                ans[i]='B'
        elif s=='AC':
            if b:
                print('No')
                exit()
            elif a:
                c+=1
                a-=1
                ans[i]='C'
            elif c:
                a+=1
                c-=1
                ans[i]='A'
        elif s=='BC':
            if a:
                print('No')
                exit()
            elif b:
                c+=1
                b-=1
                ans[i]='C'
            elif c:
                b+=1
                c-=1
                ans[i]='B'
        i+=1
elif wa==2:
    for s in ss:
        if s=='AB':
            if a==0 and b==0:
                print('No')
                exit()
            elif a==0:
                a+=1
                b-=1
                ans[i]='A'
            elif b==0:
                b+=1
                a-=1
                ans[i]='B'
            elif a==1 and b==1:
                if i==n-1:
                    ans[i]='A'
                else:
                    if ss[i+1]=='AC' or ss[i+1]=='AB':
                        a+=1
                        b-=1
                        ans[i]='A'
                    elif ss[i+1]=='BC':
                        b+=1
                        a-=1
                        ans[i]='B'
        elif s=='AC':
            if a==0 and c==0:
                print('No')
                exit()
            elif a==0:
                a+=1
                c-=1
                ans[i]='A'
            elif c==0:
                c+=1
                a-=1
                ans[i]='C'
            elif a==1 and c==1:
                if i==n-1:
                    ans[i]='A'
                else:
                    if ss[i+1]=='AC' or ss[i+1]=='AB':
                        a+=1
                        c-=1
                        ans[i]='A'
                    elif ss[i+1]=='BC':
                        c+=1
                        a-=1
                        ans[i]='C'
        elif s=='BC':
            if b==0 and c==0:
                print('No')
                exit()
            elif b==0:
                b+=1
                c-=1
                ans[i]='B'
            elif c==0:
                c+=1
                b-=1
                ans[i]='C'
            elif b==1 and c==1:
                if i==n-1:
                    ans[i]='B'
                else:
                    if ss[i+1]=='BC' or ss[i+1]=='AB':
                        b+=1
                        c-=1
                        ans[i]='B'
                    elif ss[i+1]=='AC':
                        c+=1
                        b-=1
                        ans[i]='C'
        i+=1
elif wa>2:
    for i in range(n):
        s=ss[i]
        if s=='AB':
            if a==0 and b==0:
                print('No')
                exit()
            elif a==0:
                a+=1
                b-=1
                ans[i]='A'
            elif b==0:
                b+=1
                a-=1
                ans[i]='B'
            else:
                if i==n-1:
                    ans[i]='A'
                    a+=1
                    b-=1
                else:
                    if b>=a:
                        a+=1
                        b-=1
                        ans[i]='A'
                    else:
                        b+=1
                        a-=1
                        ans[i]='B'
        elif s=='AC':
            if a==0 and c==0:
                print('No')
                exit()
            elif a==0:
                a+=1
                c-=1
                ans[i]='A'
            elif c==0:
                c+=1
                a-=1
                ans[i]='C'
            else:
                if i==n-1:
                    ans[i]='A'
                    a+=1
                    c-=1
                else:
                    if c>=a:
                        a+=1
                        c-=1
                        ans[i]='A'
                    else:
                        c+=1
                        a-=1
                        ans[i]='C'
        elif s=='BC':
            if b==0 and c==0:
                print('No')
                exit()
            elif b==0:
                b+=1
                c-=1
                ans[i]='B'
            elif c==0:
                c+=1
                b-=1
                ans[i]='C'
            else:
                if i==n-1:
                    ans[i]='B'
                    b+=1
                    c-=1
                else:
                    if c>=b:
                        b+=1
                        c-=1
                        ans[i]='B'
                    else:
                        c+=1
                        b-=1
                        ans[i]='C'
        print(i,a,b,c)
print('Yes')
for x in ans:
    print(x)

Submission status: Wrong Answer

Solution code:
import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, raw_input().split())
cmds = []

def getidx(c):
    return 'ABC'.index(c)

g = [ [], [], [] ]
for i in xrange(n):
    s = raw_input()
    cmds.append(s)
    g[getidx(s[0])].append(i)
    g[getidx(s[1])].append(i)

def getnext(c, idx):
    l, r = 0, len(g[getidx(c)]) - 1
    while l <= r:
        m = (l + r) / 2
        if m < idx:
            l = m + 1
        else:
            r = m - 1
    return l

cnt = [a, b, c]
res = []


for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1 or cnt[p1] == 0 or cnt[p2] == 0:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        if min(cnt) < 0:
            res = []
            break
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        if cmd == nxt or sum(cnt) > 2:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            idx1 = getnext(cmd[0], i)
            idx2 = getnext(cmd[1], i)

            if idx1 < idx2:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
    assert min(cnt) >= 0

if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Wrong Answer

Solution code:
#F
N,A,B,C = map(int,input().split())
S =[]
for x in range(N):
    S.append(input())
ans =[]
f =True

for x in range(N):
    
    if A==0 and B==0 and C ==0:
        f =False
        break
    
    elif A+B+C ==1:
        if S[x] =="AB" and A==1:
            ans.append("B")
            A -=1
            B +=1
        elif S[x] =="AB" and B==1:
            ans.append("A")
            A +=1
            B -=1
        elif S[x] =="AC" and A==1:
            ans.append("C")
            A -=1
            C +=1
        elif S[x] =="AC" and C==1:
            ans.append("A")
            A +=1
            C -=1
        elif S[x] =="BC" and B==1:
            ans.append("C")
            B -=1
            C +=1
        elif S[x] =="BC" and C==1:
            ans.append("B")
            B +=1
            C -=1
        else:
            f =False
            break
    elif A +B+C >= 2:
        if A==0 or B ==0 or C==0:
            if S[x]=="AB" and A ==0 and B == 0:
                f =False
                break
            elif S[x]=="AC" and A ==0 and C == 0:
                f =False
                break
            elif S[x]=="BC" and C ==0 and B == 0:
                f =False
                break
        if S[x] =="AB" and B == 0:
                ans.append("B")
                A -=1
                B +=1
        elif S[x] =="AB" and A ==0:
                ans.append("A")
                A +=1
                B =1
        elif S[x] =="AC" and C ==0:
                ans.append("C")
                A -=1
                C +=1
        elif S[x] =="AC" and A ==0:
                ans.append("A")
                A +=1
                C -=1    
        elif S[x] =="BC" and C ==0:
                ans.append("C")
                B -=1
                C +=1
        elif S[x] =="BC" and B == 0 :
                ans.append("B")
                B +=1
                C -=1 

        elif A+B+C ==2 and x <N-1 and S[x]!=S[x+1]:
            if S[x+1] =="AC":
                ans.append("A")
                A +=1
                B -=1
            elif S[x+1] =="BC":
                ans.append("B")
                A -=1
                B +=1
            elif S[x+1] =="BC":
                ans.append("C")
                A -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("A")
                A +=1
                C -=1
            elif S[x+1] =="AC":
                ans.append("C")
                B -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("B")
                B +=1
                C -=1
                
        else:
            if S[x] =="AB":
                ans.append("A")
                A +=1
                B =1
            elif S[x] =="AC":
                ans.append("A")
                A +=1
                C -=1    
            elif S[x] =="BC":
                ans.append("B")
                B +=1
                C -=1

            
    
if f ==True:
    print("Yes")
    for x in ans:
        print(x)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    n,A,B,C = map(int, readline().split())
    N = A + B + C
    S = [readline().strip() for _ in range(n)]

    ans = []
    possible = True
    for i, s in enumerate(S):
        if s == "AB":
            if A + B == 0:
                print("No")
                possible = False
                break
            
            if i < N-1 and N == 2 and A == B == 1:
                if S[i+1] == "AC":
                    A += 1
                    B -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    B += 1
                    ans.append("B")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            else:
                if A < B:
                    A += 1
                    B -= 1
                    ans.append("A")
                else:
                    A -= 1
                    B += 1
                    ans.append("B")

        elif s == "AC":
            if A + C == 0:
                print("No")
                possible = False
                break
            
            if i < N-1 and N == 2 and A == C == 1:
                if S[i+1] == "AB":
                    A += 1
                    B -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    C += 1
                    ans.append("C")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            else:
                if A < C:
                    A += 1
                    C -= 1
                    ans.append("A")
                else:
                    A -= 1
                    C += 1
                    ans.append("C")

        else:
            if B + C == 0:
                print("No")
                possible = False
                break
            
            if i < N-1 and N == 2 and B == C == 1:
                if S[i+1] == "AB":
                    B += 1
                    C -= 1
                    ans.append("B")
                elif S[i+1] == "AC":
                    B -= 1
                    C += 1
                    ans.append("C")
                else:
                    B += 1
                    C -= 1
                    ans.append("B")
            else:
                if B < C:
                    B += 1
                    C -= 1
                    ans.append("B")
                else:
                    B -= 1
                    C += 1
                    ans.append("C")

        #print(A, B, C)
        if A < 0 or B < 0 or C < 0:
            print("No")
            exit()

    if possible:
        print("Yes")
        print("\n".join(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    n,A,B,C = map(int, readline().split())
    N = A + B + C
    S = [readline().strip() for _ in range(n)]

    ans = []
    possible = True
    for i, s in enumerate(S):
        if s == "AB":
            if A + B == 0:
                print("No")
                possible = False
                break
            
            if i < N-1 and N == 2 and A == B == 1:
                if S[i+1] == "AC":
                    A += 1
                    B -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    B += 1
                    ans.append("B")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            else:
                if A < B:
                    A += 1
                    B -= 1
                    ans.append("A")
                else:
                    A -= 1
                    B += 1
                    ans.append("B")

        elif s == "AC":
            if A + C == 0:
                print("No")
                possible = False
                break
            
            if i < N-1 and N == 2 and A == C == 1:
                if S[i+1] == "AB":
                    A += 1
                    B -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    C += 1
                    ans.append("C")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            else:
                if A < C:
                    A += 1
                    C -= 1
                    ans.append("A")
                else:
                    A -= 1
                    C += 1
                    ans.append("C")

        else:
            if B + C == 0:
                print("No")
                possible = False
                break
            
            if i < N-1 and N == 2 and B == C == 1:
                if S[i+1] == "AB":
                    B += 1
                    C -= 1
                    ans.append("B")
                elif S[i+1] == "AC":
                    B -= 1
                    C += 1
                    ans.append("C")
                else:
                    B += 1
                    C -= 1
                    ans.append("B")
            else:
                if B < C:
                    B += 1
                    C -= 1
                    ans.append("B")
                else:
                    B -= 1
                    C += 1
                    ans.append("C")

    if possible:
        print("Yes")
        print("\n".join(ans))


if __name__ == "__main__":
    main()