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())
l = [[*input()] for _ in range(n)]
l2 = l[1:]+["AB"]
d = {"A":a, "B":b, "C":c}
isNo = 0
if (a>0)+(b>0)+(c>0)>= 1:
  if d[l[0][0]] == 0 and d[l[0][1]] == 0:
    isNo = 1
  elif a+b+c == 1:
    for s,s2 in zip(l,l2):
      if [d[s[0]]<d[s[1]]]:
        if s[0] not in s2:
          isNo = 1
          break
      else:
        if s[1] not in s2:
          isNo = 1
          break
  else:isNo = 0
else:isNo = 1

if isNo:print("No")
else:
  print("Yes")
  for s,s2 in zip(l,l2):
    if d[s[0]] > d[s[1]]:
      d[s[0]]+=-1
      d[s[1]]+=1
      print(s[1])
    elif d[s[0]] > d[s[1]]:
      d[s[1]]+=-1
      d[s[0]]+=1
      print(s[0])
    else:
      if s[0] in s2:
        d[s[1]]+=-1
        d[s[0]]+=1
        print(s[0])
      else:
        d[s[0]]+=-1
        d[s[1]]+=1
        print(s[1])

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
now={"A":0,"B":1,"C":2}
j=[a,b,c]
first=str(input())
if j[now[first[0]]]==j[now[first[1]]]==0 :
  print("No")
elif a+b+c==1:
  noo=0
  for i in range(n-1):
    ha=str(input())
    if ha!=first:
      noo=1
  if noo==0:
    if j[now[first[0]]]>j[now[first[1]]]:
      for i in range(n):
        if i%2==0:
          print(first[0])
        else:
          print(first[1])
    else:
      for i in range(n):
        if i%2==0:
          print(first[1])
        else:
          print(first[0])
  else:
    print("No")
      
else:
  print("Yes")
  #print(first,j)
  if j[now[first[0]]]>j[now[first[1]]]:
    print(str(first[1]))
    j[now[first[1]]]+=1
    j[now[first[0]]]-=1
  else:
    j[now[first[0]]]+=1
    j[now[first[1]]]-=1
    print(str(first[0]))
  for i in range(n-1):
    first=str(input())
    #print(first,j)
    if j[now[first[0]]]>j[now[first[1]]]:
      j[now[first[0]]]-=1
      j[now[first[1]]]+=1
      print(str(first[1]))
    else:
      j[now[first[0]]]+=1
      j[now[first[1]]]-=1
      print(first[0])

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
now={"A":0,"B":1,"C":2}
j=[a,b,c]
first=str(input())
if j[now[first[0]]]==j[now[first[1]]]==0 :
  print("No")
elif a+b+c==1:
  noo=0
  for i in range(n-1):
    ha=str(input())
    if ha!=first:
      noo=1
  if noo==0:
    if j[now[first[0]]]>j[now[first[1]]]:
      for i in range(n):
        if i%2==0:
          print(first[0])
        else:
          print(first[1])
    else:
      for i in range(n):
        if i%2==0:
          print(first[1])
        else:
          print(first[0])
      
else:
  print(first,j)
  if j[now[first[0]]]>j[now[first[1]]]:
    print(str(first[1]))
    j[now[first[1]]]+=1
    j[now[first[0]]]-=1
  else:
    j[now[first[0]]]+=1
    j[now[first[1]]]-=1
    print(str(first[0]))
  for i in range(n-1):
    first=str(input())
    print(first,j)
    if j[now[first[0]]]>j[now[first[1]]]:
      j[now[first[0]]]-=1
      j[now[first[1]]]+=1
      print(str(first[1]))
    else:
      j[now[first[0]]]+=1
      j[now[first[1]]]-=1
      print(first[0])

Submission status: Wrong Answer

Solution code:
# import sys
#
# sys.setrecursionlimit(1000000)
#
#
# def recur1(a, b, c, ls: [], ans: []):
#     s = ls[0]
#     if s == "AB":
#         if a == 0 and b == 0:
#             return False, []
#         elif a == 0:
#             ans.append("A")
#             if len(ls) == 1:
#                 return True, ans
#             return recur1(a + 1, b - 1, c, ls[1:], ans)
#         else:
#             ans.append("B")
#             if len(ls) == 1:
#                 return True, ans
#             return recur1(a - 1, b + 1, c, ls[1:], ans)
#     elif s == "AC":
#         if a == 0 and c == 0:
#             return False, []
#         elif a == 0:
#             ans.append("A")
#             if len(ls) == 1:
#                 return True, ans
#             return recur1(a + 1, b, c - 1, ls[1:], ans)
#         else:
#             ans.append("C")
#             if len(ls) == 1:
#                 return True, ans
#             return recur1(a - 1, b, c + 1, ls[1:], ans)
#     else:
#         if b == 0 and c == 0:
#             return False, []
#         elif b == 0:
#             ans.append("B")
#             if len(ls) == 1:
#                 return True, ans
#             return recur1(a, b + 1, c - 1, ls[1:], ans)
#         else:
#             ans.append("C")
#             if len(ls) == 1:
#                 return True, ans
#             return recur1(a, b - 1, c + 1, ls[1:], ans)
#
#
# def recur2(a, b, c, ls: [], ans: []):
#     s = ls[0]
#     ln = len(ls)
#     if s == "AB":
#         if a == 0 and b == 0:
#             return False, []
#         elif a == 0:
#             ans.append("A")
#             if ln == 1:
#                 return True, ans
#             return recur2(1, b - 1, c, ls[1:], ans)
#         elif b == 0:
#             ans.append("B")
#             if ln == 1:
#                 return True, ans
#             return recur2(a - 1, 1, c, ls[1:], ans)
#         else:
#             if ln == 1:
#                 ans.append("A")
#                 return True, ans
#             nex = ls[1]
#             if nex == "BC":
#                 ans.append("B")
#                 ans.append("C")
#                 if ln == 2:
#                     return True, ans
#                 return recur2(0, 1, 1, ls[2:], ans)
#             elif nex == "AC":
#                 ans.append("A")
#                 ans.append("C")
#                 if ln == 2:
#                     return True, ans
#                 return recur2(1, 0, 1, ls[2:], ans)
#             else:
#                 ans.append("A")
#                 ans.append("B")
#                 if ln == 2:
#                     return True, ans
#                 return recur2(1, 1, 0, ls[2:], ans)
#     elif s == "AC":
#         if a == 0 and c == 0:
#             return False, []
#         elif a == 0:
#             ans.append("A")
#             if ln == 1:
#                 return True, ans
#             return recur2(a + 1, b, c - 1, ls[1:], ans)
#         elif c == 0:
#             ans.append("C")
#             if ln == 1:
#                 return True, ans
#             return recur2(a - 1, b, c + 1, ls[1:], ans)
#         else:
#             if ln == 1:
#                 ans.append("A")
#                 return True, ans
#             nex = ls[1]
#             if nex == "AB":
#                 ans.append("A")
#                 ans.append("B")
#                 if ln == 2:
#                     return True, ans
#                 return recur2(1, 1, 0, ls[2:], ans)
#             elif nex == "BC":
#                 ans.append("C")
#                 ans.append("B")
#                 if ln == 2:
#                     return True, ans
#                 return recur2(0, 1, 1, ls[2:], ans)
#             else:
#                 ans.append("A")
#                 ans.append("C")
#                 if ln == 2:
#                     return True, ans
#                 return recur2(1, 0, 1, ls[2:], ans)
#     else:
#         if c == 0 and b == 0:
#             return False, []
#         elif c == 0:
#             ans.append("C")
#             if ln == 1:
#                 return True, ans
#             return recur2(a, b - 1, c + 1, ls[1:], ans)
#         elif b == 0:
#             ans.append("B")
#             if ln == 1:
#                 return True, ans
#             return recur2(a, b + 1, c - 1, ls[1:], ans)
#         else:
#             if ln == 1:
#                 ans.append("C")
#                 return True, ans
#             nex = ls[1]
#             if nex == "AB":
#                 ans.append("B")
#                 ans.append("A")
#                 if ln == 2:
#                     return True, ans
#                 return recur2(1, 1, 0, ls[2:], ans)
#             if nex == "AC":
#                 ans.append("C")
#                 ans.append("A")
#                 if ln == 2:
#                     return True, ans
#                 return recur2(1, 0, 1, ls[2:], ans)
#             else:
#                 ans.append("B")
#                 ans.append("C")
#                 return recur2(0, 1, 1, ls[2:], ans)
#
#
# def recur3(a, b, c, ls: [], ans: []):
#     s = ls[0]
#     ln = len(ls)
#     if s == "AB":
#         if a == 0 and b == 0:
#             return False, []
#         else:
#             if a >= b:
#                 ans.append("B")
#                 if ln == 1:
#                     return True, ans
#                 return recur3(a - 1, b + 1, c, ls[1:], ans)
#             else:
#                 ans.append("A")
#                 if ln == 1:
#                     return True, ans
#                 return recur3(a + 1, b - 1, c, ls[1:], ans)
#     elif s == "BC":
#         if b == 0 and c == 0:
#             return False, []
#         else:
#             if b >= c:
#                 ans.append("C")
#                 if ln == 1:
#                     return True, ans
#                 return recur3(a, b - 1, c + 1, ls[1:], ans)
#             else:
#                 ans.append("B")
#                 if ln == 1:
#                     return True, ans
#                 return recur3(a, b + 1, c - 1, ls[1:], ans)
#     else:
#         if a == 0 and c == 0:
#             return False, []
#         else:
#             if a >= c:
#                 ans.append("C")
#                 if ln == 1:
#                     return True, ans
#                 return recur3(a - 1, b, c + 1, ls[1:], ans)
#             else:
#                 ans.append("A")
#                 if ln == 1:
#                     return True, ans
#                 return recur3(a + 1, b, c - 1, ls[1:], ans)


def solve(n, a, b, c, s_ls):
    ans = []
    for i in range(n):
        s = s_ls[i]
        if s == "AB":
            if a == 0 and b == 0:
                return False, []
            elif a == 0:
                ans.append("A")
                a += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                a -= 1
                b += 1
            elif a == b == 1 and c == 0:
                if i == n - 1:
                    ans.append("A")
                    a += 1
                    b -= 1
                else:
                    nex = s_ls[i + 1]
                    if nex == "BC":
                        ans.append("B")
                        a -= 1
                        b += 1
                    else:
                        ans.append("A")
                        a += 1
                        b -= 1
            else:
                if a >= b:
                    ans.append("B")
                    a -= 1
                    b += 1
                else:
                    ans.append("A")
                    a += 1
                    b -= 1
        elif s == "BC":
            if b == 0 and c == 0:
                return False, []
            elif c == 0:
                ans.append("C")
                c += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                c -= 1
                b += 1
            elif c == b == 1 and a == 0:
                if i == n - 1:
                    ans.append("C")
                    c += 1
                    b -= 1
                else:
                    nex = s_ls[i + 1]
                    if nex == "BA":
                        ans.append("B")
                        c -= 1
                        b += 1
                    else:
                        ans.append("C")
                        c += 1
                        b -= 1
            else:
                if c >= b:
                    ans.append("B")
                    c -= 1
                    b += 1
                else:
                    ans.append("C")
                    c += 1
                    b -= 1
        elif s == "AC":
            if a == 0 and c == 0:
                return False, []
            elif a == 0:
                ans.append("A")
                a += 1
                c -= 1
            elif c == 0:
                ans.append("C")
                a -= 1
                c += 1
            elif a == c == 1 and b == 0:
                if i == n - 1:
                    ans.append("A")
                    a += 1
                    c -= 1
                else:
                    nex = s_ls[i + 1]
                    if nex == "AB":
                        ans.append("A")
                        a += 1
                        c -= 1
                    else:
                        ans.append("C")
                        a -= 1
                        c += 1
            else:
                if a >= c:
                    ans.append("C")
                    a -= 1
                    c += 1
                else:
                    ans.append("A")
                    a += 1
                    c -= 1
    return True, ans


def main():
    n, a, b, c = map(int, input().split())
    s_ls = []
    for i in range(n):
        s_ls.append(input())

    res, ans = solve(n, a, b, c, s_ls)
    if not res:
        print("No")
    else:
        print("Yes")
        for x in ans:
            print(x)


main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(1000000)


def recur1(a, b, c, ls: [], ans: []):
    s = ls[0]
    if s == "AB":
        if a == 0 and b == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if len(ls) == 1:
                return True, ans
            return recur1(a + 1, b - 1, c, ls[1:], ans)
        else:
            ans.append("B")
            if len(ls) == 1:
                return True, ans
            return recur1(a - 1, b + 1, c, ls[1:], ans)
    elif s == "AC":
        if a == 0 and c == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if len(ls) == 1:
                return True, ans
            return recur1(a + 1, b, c - 1, ls[1:], ans)
        else:
            ans.append("C")
            if len(ls) == 1:
                return True, ans
            return recur1(a - 1, b, c + 1, ls[1:], ans)
    else:
        if b == 0 and c == 0:
            return False, []
        elif b == 0:
            ans.append("B")
            if len(ls) == 1:
                return True, ans
            return recur1(a, b + 1, c - 1, ls[1:], ans)
        else:
            ans.append("C")
            if len(ls) == 1:
                return True, ans
            return recur1(a, b - 1, c + 1, ls[1:], ans)


def recur2(a, b, c, ls: [], ans: []):
    s = ls[0]
    ln = len(ls)
    if s == "AB":
        if a == 0 and b == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if ln == 1:
                return True, ans
            return recur2(1, b - 1, c, ls[1:], ans)
        elif b == 0:
            ans.append("B")
            if ln == 1:
                return True, ans
            return recur2(a - 1, 1, c, ls[1:], ans)
        else:
            if ln == 1:
                ans.append("A")
                return True, ans
            nex = ls[1]
            if nex == "BC":
                ans.append("B")
                ans.append("C")
                if ln == 2:
                    return True, ans
                return recur2(0, 1, 1, ls[2:], ans)
            elif nex == "AC":
                ans.append("A")
                ans.append("C")
                if ln == 2:
                    return True, ans
                return recur2(1, 0, 1, ls[2:], ans)
            else:
                ans.append("A")
                ans.append("B")
                if ln == 2:
                    return True, ans
                return recur2(1, 1, 0, ls[2:], ans)
    elif s == "AC":
        if a == 0 and c == 0:
            return False, []
        elif a == 0:
            ans.append("A")
            if ln == 1:
                return True, ans
            return recur2(a + 1, b, c - 1, ls[1:], ans)
        elif c == 0:
            ans.append("C")
            if ln == 1:
                return True, ans
            return recur2(a - 1, b, c + 1, ls[1:], ans)
        else:
            if ln == 1:
                ans.append("A")
                return True, ans
            nex = ls[1]
            if nex == "AB":
                ans.append("A")
                ans.append("B")
                if ln == 2:
                    return True, ans
                return recur2(1, 1, 0, ls[2:], ans)
            elif nex == "BC":
                ans.append("C")
                ans.append("B")
                if ln == 2:
                    return True, ans
                return recur2(0, 1, 1, ls[2:], ans)
            else:
                ans.append("A")
                ans.append("C")
                if ln == 2:
                    return True, ans
                return recur2(1, 0, 1, ls[2:], ans)
    else:
        if c == 0 and b == 0:
            return False, []
        elif c == 0:
            ans.append("C")
            if ln == 1:
                return True, ans
            return recur2(a, b - 1, c + 1, ls[1:], ans)
        elif b == 0:
            ans.append("B")
            if ln == 1:
                return True, ans
            return recur2(a, b + 1, c - 1, ls[1:], ans)
        else:
            if ln == 1:
                ans.append("C")
                return True, ans
            nex = ls[1]
            if nex == "AB":
                ans.append("B")
                ans.append("A")
                if ln == 2:
                    return True, ans
                return recur2(1, 1, 0, ls[2:], ans)
            if nex == "AC":
                ans.append("C")
                ans.append("A")
                if ln == 2:
                    return True, ans
                return recur2(1, 0, 1, ls[2:], ans)
            else:
                ans.append("B")
                ans.append("C")
                return recur2(0, 1, 1, ls[2:], ans)


def recur3(a, b, c, ls: [], ans: []):
    s = ls[0]
    ln = len(ls)
    if s == "AB":
        if a == 0 and b == 0:
            return False, []
        else:
            if a >= b:
                ans.append("B")
                if ln == 1:
                    return True, ans
                return recur3(a - 1, b + 1, c, ls[1:], ans)
            else:
                ans.append("A")
                if ln == 1:
                    return True, ans
                return recur3(a + 1, b - 1, c, ls[1:], ans)
    elif s == "BC":
        if b == 0 and c == 0:
            return False, []
        else:
            if b >= c:
                ans.append("C")
                if ln == 1:
                    return True, ans
                return recur3(a, b - 1, c+1, ls[1:], ans)
            else:
                ans.append("B")
                if ln == 1:
                    return True, ans
                return recur3(a, b + 1, c-1, ls[1:], ans)
    else:
        if a == 0 and c == 0:
            return False, []
        else:
            if a >= c:
                ans.append("C")
                if ln == 1:
                    return True, ans
                return recur3(a - 1, b, c+1, ls[1:], ans)
            else:
                ans.append("A")
                if ln == 1:
                    return True, ans
                return recur3(a + 1, b, c-1, ls[1:], ans)


def solve(n, a, b, c, s_ls):
    ans = []
    for i in range(n):
        s = s_ls[i]
        if s == "AB":
            if a == 0 and b == 0:
                return False, []
            elif a == 0:
                ans.append("A")
                a += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                a -= 1
                b += 1
            elif a == b == 1 and c == 0:
                if i == n-1:
                    ans.append("A")
                    a += 1
                    b -= 1
                else:
                    nex = s_ls[i+1]
                    if nex == "BC":
                        ans.append("B")
                        a -= 1
                        b += 1
                    else:
                        ans.append("A")
                        a += 1
                        b -= 1
            else:
                if a >= b:
                    ans.append("B")
                    a -= 1
                    b += 1
                else:
                    ans.append("A")
                    a += 1
                    b -= 1
        elif s == "BC":
            if b == 0 and c == 0:
                return False, []
            elif c == 0:
                ans.append("C")
                c += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                c -= 1
                b += 1
            elif c == b == 1 and a == 0:
                if i == n-1:
                    ans.append("C")
                    c += 1
                    b -= 1
                else:
                    nex = s_ls[i+1]
                    if nex == "BA":
                        ans.append("B")
                        c -= 1
                        b += 1
                    else:
                        ans.append("C")
                        c += 1
                        b -= 1
            else:
                if c >= b:
                    ans.append("B")
                    c -= 1
                    b += 1
                else:
                    ans.append("C")
                    c += 1
                    b -= 1
        elif s == "AC":
            if a == 0 and c == 0:
                return False, []
            elif a == 0:
                ans.append("A")
                a += 1
                c -= 1
            elif c == 0:
                ans.append("C")
                a -= 1
                c += 1
            elif a == c == 1 and b == 0:
                if i == n-1:
                    ans.append("A")
                    a += 1
                    c -= 1
                else:
                    nex = s_ls[i+1]
                    if nex == "AB":
                        ans.append("A")
                        a += 1
                        c -= 1
                    else:
                        ans.append("C")
                        a -= 1
                        c += 1
            else:
                if a >= c:
                    ans.append("C")
                    a -= 1
                    c += 1
                else:
                    ans.append("A")
                    a += 1
    return True, ans


def main():
    n, a, b, c = map(int, input().split())
    s_ls = []
    for i in range(n):
        s_ls.append(input())

    res, ans = solve(n, a, b, c, s_ls)
    if not res:
        print("No")
    else:
        print("Yes")
        for x in ans:
            print(x)

main()

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
now={"A":0,"B":1,"C":2}
j=[a,b,c]
first=str(input())
if j[now[first[0]]]==j[now[first[1]]]==0 or a+b+c==1:
  print("No")
else:
  if j[now[first[0]]]>j[now[first[1]]]:
    print(str(first[1]))
    j[now[first[1]]]+=1
    j[now[first[0]]]-=1
  else:
    j[now[first[0]]]+=1
    j[now[first[1]]]-=1
    print(str(first[0]))
  for i in range(n-1):
    first=str(input())
    #print(first,j)
    if j[now[first[0]]]>j[now[first[1]]]:
      j[now[first[0]]]-=1
      j[now[first[1]]]+=1
      print(str(first[1]))
    else:
      j[now[first[0]]]+=1
      j[now[first[1]]]-=1
      print(first[0])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3

import sys
from collections import defaultdict


(n, a, b, c) = map(int, input().split())
ss = []
for i in range(n):
    ss.append(input())


def psum(pair):
    if pair == 'AB':
        return a + b
    elif pair == 'AC':
        return a + c
    elif pair == 'BC':
        return b + c

def select(pair, npair):
    global a, b, c
    added = None
    if pair == 'AB':
        if a < b:
            a += 1
            b -= 1
            added = 'A'
        elif a > b:
            a -= 1
            b += 1
            added = 'B'            
        else:
            # a == b
            if a + b == 2 and c == 0 and npair:
                if npair == 'AB':
                    a += 1
                    b -= 1
                    added = 'A'
                elif npair == 'AC':
                    a += 1
                    b -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    b += 1
                    added = 'B'                    
            else:
                a += 1
                b -= 1
                added = 'A'
            
    elif pair == 'AC':
        if a < c:
            a += 1
            c -= 1
            added = 'A'
        elif a > c:
            a -= 1
            c += 1
            added = 'C'            
        else:
            # a == c
            if a + c == 2 and b == 0 and npair:
                if npair == 'AB':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'AC':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    c += 1
                    added = 'C'
            else:
                a += 1
                c -= 1
                added = 'A'                
        
    elif pair == 'BC':
        if b < c:
            b += 1
            c -= 1
            added = 'B'            
        elif b > c:
            b -= 1
            c += 1
            added = 'C'            
        else:
            # b == c
            if b + c == 2 and a == 0 and npair:
                if npair == 'AB':
                    b += 1
                    c -= 1
                    added = 'B'                    
                elif npair == 'AC':
                    b -= 1
                    c += 1
                    added = 'C'                    
                elif npair == 'BC':
                    b += 1
                    c -= 1
                    added = 'B'
            else:
                b += 1
                c -= 1
                added = 'B'
    return added

    
ans = []
if a + b + c == 0:
    print('No')
else:
    for i, s in enumerate(ss):
        if psum(s) == 0:
            print('No')
            exit()
        else:
            ns = None
            if i < len(ss) - 1:
                ns = ss[i + 1]
            ans.append(select(s, ns))

        
print('Yes')
for l in ans:
    print(l)

Submission status: Wrong Answer

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

path = []

for _ in range(N):
	target = input()
	if target == 'AB':
		if A > B:
			A -= 1
			B += 1
			path.append('B')
		else:
			A += 1
			B -= 1
			path.append('A')
	if target == 'AC':
		if A > C:
			A -= 1
			C += 1
			path.append('C')
		else:
			A += 1
			C -= 1
			path.append('A')
	if target == 'BC':
		if B > C:
			B -= 1
			C += 1
			path.append('C')
		else:
			B += 1
			C -= 1
			path.append('B')
	if A < 0 or B < 0 or C < 0:
		print('No')
		exit(0)

print('Yes')
for i in path:
	print(i)