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 = list(map(int, input().split()))
s = [list(input()) for _ in range(n)]
d = {"A": a, "B": b, "C": c}
def calc(add, sub):
    d[add] += 1
    d[sub] -= 1
    ans.append(add)
def disp():
    print("Yes")
    for i in ans:
        print(i)
ans = []
su = a + b + c
if su == 0:
    print("No")
elif su == 1:
    for i, j in s:
        if d[i] == 0 and d[j] == 0:
            print("No")
            break
        else:
            if d[i] > d[j]:
                calc(j, i)
            else:
                calc(i, j)
    else:
        disp()
else:
    for i, [j, k] in enumerate(s):
        if d[j] == 0 and d[k] == 0:
            print("No")
            break
        elif d[j] == 0:
            calc(j, k)
        elif d[k] == 0:
            calc(k, j)
        elif su == 2 and d[j] == 1 and d[k] == 1 and i + 1 != n:
            if s[i + 1][0] == j:
                calc(j, k)
            else:
                calc(k, j)
        else:
            if d[j] < d[k]:
                calc(j, k)
            else:
                calc(k, j)
    else:
        disp()

Submission status: Wrong Answer

Solution code:
import sys
l = sys.stdin.readline().strip()
N, A, B, C = map(int, l.split())

history = []
for _ in range(N):
    cmd = sys.stdin.readline().strip()
    history.append(cmd)

if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    fail = False
    path = ""

    for one in history:
        if one == "AB":
            if A == 0 and B==0:
                fail = True
                break
            else:
                if A == 0:
                    A = 1
                    B = 0
                    path += "A"
                else:
                    A = 0
                    B = 1
                    path += "B"

        if one == "AC":
            if A== 0 and C == 0:
                fail = True
                break
            else:
                if A==0:
                    A = 1
                    C = 0
                    path += "A"
                else:
                    A = 0
                    C = 1
                    path += "C"
        if one == "BC":
            if B == 0 and C == 0:
                fail = True
                break
            else:
                if B == 0:
                    B = 1
                    C = 0
                    path += "B"
                else:
                    C,B = 1,0
                    path += "C"
        
    if fail:
        print("No")
    else:
        print ("Yes")
        for one in path:
            print(one)
else:
    fail = False
    path = ""
    for idx, one in enumerate(history):
        if A < 0 or B < 0 or C<0:
            fail = True
            break
        if one == "AB":
            if A <= 0 and B<=0:
                fail = True
                break
            else:
                if idx == N-1 or history[idx+1] == "AB":
                    if A > B:
                        A, B,path = A-1, B+1, path +"B"
                    else:
                        A, B, path = A+1, B-1, path+"A"
                else:
                    if "A" in history[idx+1]:
                        if B > 0:
                            A, B, path = A+1, B-1, path+"A"
                        else:
                            A, B, path = A-1, B+1, path+"A"
                    else:
                        if A > 0:
                            A, B,path = A-1, B+1, path +"B"
                        else:
                            A, B, path = A+1, B-1, path+"A"
        elif one =="AC":
            if A<=0 and C<=0:
                fail =True
                break
            else:
                if idx == N-1 or history[idx+1] == "AC":
                    if A > C:
                        A, C, path = A -1, C+1, path+"C"
                    else:
                        A,C,path = A+1, C-1, path +"A"
                else:
                    if "A" in history[idx+1]:
                        if C > 0:
                            A,C,path = A+1, C-1, path +"A"
                        else:
                            A, C, path = A -1, C+1, path+"C"
                    else:
                        if A > 0:
                            A, C, path = A -1, C+1, path+"C"
                        else:
                            A,C,path = A+1, C-1, path +"A"
        else:
            if B<=0 and C<=0:
                fail = True
                break
            else:
                if idx == N-1 or history[idx+1] == "BC":
                    if B > C:
                        B,C,path = B-1,C+1, path+"C"
                    else:
                        B,C,path = B+1,C-1,path+"B"
                else:
                    if "B" in history[idx+1]:
                        if C > 0:
                            B,C,path = B+1,C-1,path+"B"
                        else:
                            B,C,path = B-1,C+1, path+"C"
                    else:
                        if B > 0:
                            B,C,path = B-1,C+1, path+"C"
                        else:
                            B,C,path = B+1,C-1,path+"B"
        
    if fail:
        print("No")
    else:
        print("Yes")
        for one in path:
            print(one)

Submission status: Wrong Answer

Solution code:
import sys
l = sys.stdin.readline().strip()
N, A, B, C = map(int, l.split())

history = []
for _ in range(N):
    cmd = sys.stdin.readline().strip()
    history.append(cmd)

if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    fail = False
    path = ""

    for one in history:
        if one == "AB":
            if A == 0 and B==0:
                fail = True
                break
            else:
                if A == 0:
                    A = 1
                    B = 0
                    path += "A"
                else:
                    A = 0
                    B = 1
                    path += "B"

        if one == "AC":
            if A== 0 and C == 0:
                fail = True
                break
            else:
                if A==0:
                    A = 1
                    C = 0
                    path += "A"
                else:
                    A = 0
                    C = 1
                    path += "C"
        if one == "BC":
            if B == 0 and C == 0:
                fail = True
                break
            else:
                if B == 0:
                    B = 1
                    C = 0
                    path += "B"
                else:
                    C,B = 1,0
                    path += "C"
        
    if fail:
        print("No")
    else:
        print ("Yes")
        for one in path:
            print(one)
else:
    fail = False
    path = ""
    for idx, one in enumerate(history):
        if one == "AB":
            if A+B==0:
                fail = True
                break
            else:
                if idx == N-1 or history[idx+1] == "AB":
                    if A > B:
                        A, B,path = A-1, B+1, path +"B"
                    else:
                        A, B, path = A+1, B-1, path+"A"
                else:
                    if "A" in history[idx+1]:
                        A, B, path = A+1, B-1, path+"A"
                    else:
                        A, B,path = A-1, B+1, path +"B"
        elif one =="AC":
            if A + C==0:
                fail =True
                break
            else:
                if idx == N-1 or history[idx+1] == "AC":
                    if A > C:
                        A, C, path = A -1, C+1, path+"C"
                    else:
                        A,C,path = A+1, C-1, path +"A"
                else:
                    if "A" in history[idx+1]:
                        A,C,path = A+1, C-1, path +"A"
                    else:
                        A, C, path = A -1, C+1, path+"C"
        else:
            if B+C ==0:
                fail = True
                break
            else:
                if idx == N-1 or history[idx+1] == "BC":
                    if B > C:
                        B,C,path = B-1,C+1, path+"C"
                    else:
                        B,C,path = B+1,C-1,path+"B"
                else:
                    if "B" in history[idx+1]:
                        B,C,path = B+1,C-1,path+"B"
                    else:
                        B,C,path = B-1,C+1, path+"C"
        
    if fail:
        print("No")
    else:
        print("Yes")
        for one in path:
            print(one)

Submission status: Wrong Answer

Solution code:
import sys
l = sys.stdin.readline().strip()
N, A, B, C = map(int, l.split())

history = []
for _ in range(N):
    cmd = sys.stdin.readline().strip()
    history.append(cmd)

if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    fail = False
    path = ""

    for one in history:
        if one == "AB":
            if A == 0 and B==0:
                fail = True
                break
            else:
                if A == 0:
                    A = 1
                    B = 0
                    path += "A"
                else:
                    A = 0
                    B = 1
                    path += "B"

        if one == "AC":
            if A== 0 and C == 0:
                fail = True
                break
            else:
                if A==0:
                    A = 1
                    C = 0
                    path += "A"
                else:
                    A = 0
                    C = 1
                    path += "C"
        if one == "BC":
            if B == 0 and C == 0:
                fail = True
                break
            else:
                if B == 0:
                    B = 1
                    C = 0
                    path += "B"
                else:
                    C,B = 1,0
                    path += "C"
        
    if fail:
        print("No")
    else:
        print ("Yes")
        for one in path:
            print(one)
else:
    fail = False
    path = ""
    for one in history:
        if one == "AB":
            if A+B==0:
                fail = True
                break
            else:
                if A > B:
                    A, B,path = A-1, B+1, path +"B"
                else:
                    A, B, path = A+1, B-1, path+"A"
        elif one =="AC":
            if A + C==0:
                fail =True
                break
            else:
                if A > C:
                    A, C, path = A -1, C+1, path+"C"
                else:
                    A,C,path = A+1, C-1, path +"A"
        else:
            if B+C ==0:
                fail = True
                break
            else:
                if B >C:
                    B,C,path = B-1,C+1, path+"C"
                else:
                    B,C,path = B+1,C-1,path+"B"
        
    if fail:
        print("No")
    else:
        print("Yes")
        for one in path:
            print(one)

Submission status: Wrong Answer

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

def main():
  N, a, b, c = map(int, readline().split())
  A = {"A":a, "B":b, "C":c}
  S = []
  for i in range(N):
    l = readline().strip()
    S.append([l[0], l[1]])

  ans = ""
  ret = False
  for i in range(N):
    if A[S[i][0]] == 0 and A[S[i][1]] == 0:
      break
    if i == N-1:
      if A[S[i][0]] > 0:
        ans += S[i][1]
      else:
        ans += S[i][0]
      ret = True
      break
    if A["A"] + A["B"] + A["C"] == 1:
      if A[S[i][0]] == 1:
        if S[i][0] in S[i+1]:
          break
        else:
          A[S[i][0]] -= 1
          A[S[i][1]] += 1
          ans += S[i][1]
          continue
      else:
        if S[i][1] in S[i+1]:
          break
        else:
          A[S[i][0]] += 1
          A[S[i][1]] -= 1
          ans += S[i][0]
          continue
    if A[S[i][0]] == 0:
      A[S[i][0]] += 1
      A[S[i][1]] -= 1
      ans += S[i][0]
      continue
    elif A[S[i][1]] == 0:
      A[S[i][0]] -= 1
      A[S[i][1]] += 1
      ans += S[i][1]
      continue
    elif S[i][0] in S[i+1]:
      A[S[i][0]] += 1
      A[S[i][1]] -= 1
      ans += S[i][0]
      continue
    else:
      A[S[i][0]] -= 1
      A[S[i][1]] += 1
      ans += S[i][1]
      continue

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

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

print("Yes")
print("\n".join(ans))

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
s=[0]*n
for i in range(n):
    s[i]=input()
l=[0]*n
for i in range(n):
    if s[i]=="AB":
        if a==b==0:
            print("No")
            exit()
        if a>=b:
            a-=1
            b+=1
            l[i]="B"
        else:
            a+=1
            b-=1
            l[i]="A"
    if s[i]=="BC":
        if c==b==0:
            print("No")
            exit()
        if b>=c:
            b-=1
            c+=1
            l[i]="C"
        else:
            b+=1
            c-=1
            l[i]="B"
    if s[i]=="AC":
        if a==c==0:
            print("No")
            exit()
        if a>=c:
            a-=1
            c+=1
            l[i]="C"
        else:
            a+=1
            c-=1
            l[i]="A"
if l[-1]!=0:
    print("Yes")
    for i in range(n):
        print(l[i])

Submission status: Wrong Answer

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

si = [input() for i in range(n)]

if a == 0 and b == 0 and c== 0:
    print("No")
    exit()

s1 = 0
s2 = 0

abc = [a,b,c]
moji = ['A','B','C']

def checkmoji(s):
    if si[i] == "AB":
        s1 = 0
        s2 = 1
    elif si[i] == 'AC':
        s1 = 0
        s2 = 2
    elif si[i] == 'BC':
        s1 = 1
        s2 = 2
    return s1,s2

ans = []

for i in range(n):
    s1,s2 = checkmoji(si[i])
    if abc[s1] == 0 and abc[s2] == 0:
        print('No')
        exit()
    if abc[s2] == 0:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(moji[s2])
    elif abc[s1] == 0:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(moji[s1])
    else:
        
        if i == n-1:
            ans.append(moji[s1])
            break
        
        s1_a,s2_a = checkmoji(si[i+1])
        if s1 == s1_a or s1 == s2_a:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(moji[s1])
        else:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(moji[s2])
print('Yes')
for i in range(len(ans)):
    print(ans[i])

Submission status: Wrong Answer

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

si = [input() for i in range(n)]

if a == 0 and b == 0 and c== 0:
    print("No")
    exit()

s1 = 0
s2 = 0

abc = [a,b,c]
moji = ['A','B','C']

def checkmoji(s):
    if si[i] == "AB":
        s1 = 0
        s2 = 1
    elif si[i] == 'AC':
        s1 = 0
        s2 = 2
    elif si[i] == 'BC':
        s1 = 1
        s2 = 2
    return s1,s2

ans = []
print('No')
exit()

for i in range(n):
    s1,s2 = checkmoji(si[i])
    if abc[s1] <= 0 and abc[s2] <= 0:
        print('No')
        exit()
    if abc[s2] == 0:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(moji[s2])
    elif abc[s1] == 0:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(moji[s1])
    else:
        
        if i == n-1:
            ans.append(moji[s1])
            break
        
        s1_a,s2_a = checkmoji(si[i+1])
        if s1 == s1_a or s1 == s2_a:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(moji[s1])
        else:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(moji[s2])
print('Yes')
for i in range(len(ans)):
    print(ans[i])

Submission status: Wrong Answer

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

si = [input() for i in range(n)]

if a == 0 and b == 0 and c== 0:
    print("No")
    exit()

s1 = 0
s2 = 0

abc = [a,b,c]
moji = ['A','B','C']

def checkmoji(s):
    if si[i] == "AB":
        s1 = 0
        s2 = 1
    elif si[i] == 'AC':
        s1 = 0
        s2 = 2
    elif si[i] == 'BC':
        s1 = 1
        s2 = 2
    return s1,s2

ans = []

for i in range(n):
    s1,s2 = checkmoji(si[i])
    if abc[s1] <= 0 and abc[s2] <= 0:
        print('No')
        exit()
    if abc[s2] == 0:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(moji[s2])
    elif abc[s1] == 0:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(moji[s1])
    else:
        
        if i == n-1:
            ans.append(moji[s1])
            break
        
        s1_a,s2_a = checkmoji(si[i+1])
        if s1 == s1_a or s1 == s2_a:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(moji[s1])
        else:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(moji[s2])
print('Yes')
for i in range(len(ans)):
    print(ans[i])

Submission status: Wrong Answer

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

si = [input() for i in range(n)]

if a == 0 and b == 0 and c== 0:
    print("No")
    exit()

s1 = 0
s2 = 0

abc = [a,b,c]
moji = ['A','B','C']

def checkmoji(s):
    if si[i] == "AB":
        s1 = 0
        s2 = 1
    elif si[i] == 'AC':
        s1 = 0
        s2 = 2
    elif si[i] == 'BC':
        s1 = 1
        s2 = 2
    return s1,s2

ans = []

for i in range(n):
    s1,s2 = checkmoji(si[i])
    if abc[s1] <= 0 and abc[s2] <= 0:
        print('No')
        exit()
    if abc[s2] == 0:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(moji[s2])
    elif abc[s1] == 0:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(moji[s1])
    else:
        
        if i == n-1:
            ans.append(moji[s1])
            break
        
        s1_a,s2_a = checkmoji(si[i+1])
        if s1 == s1_a or s1 == s2_a:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(moji[s2])
        else:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(moji[s1])
print('Yes')
for i in range(len(ans)):
    print(ans[i])

Submission status: Wrong Answer

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

si = [input() for i in range(n)]

if a == 0 and b == 0 and c== 0:
    print("No")
    exit()

s1 = 0
s2 = 0

abc = [a,b,c]
moji = ['A','B','C']

def checkmoji(s):
    if si[i] == "AB":
        s1 = 0
        s2 = 1
    elif si[i] == 'AC':
        s1 = 0
        s2 = 2
    elif si[i] == 'BC':
        s1 = 1
        s2 = 2
    return s1,s2

ans = []

for i in range(n):
    s1,s2 = checkmoji(si[i])
    if abc[s1] <= 0 and abc[s2] <= 0:
        print('No')
        exit()
    if abc[s1] > abc[s2]:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(moji[s2])
    elif abc[s2] > abc[s1]:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(moji[s1])
    else:
        if i == n-1:
            ans.append(moji[s1])
            break
        s1_a,s2_a = checkmoji(si[i+1])
        if s1 != s1_a and s1 != s2_a:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(moji[s2])
        elif s2 != s1_a and s2 != s2_a:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(moji[s1])
print('Yes')
for i in range(len(ans)):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from functools import reduce

class ABC:
    def __init__(self, A,B,C, cmds):
        self.current = dict(A=A,B=B,C=C)
        self.selected = []
        self.cmds = cmds
        self.answer = None

    def inc(self, X):
        self.current[X] += 1
    
    def dec(self, X):
        self.current[X] -= 1
    
    def select(self, X, Y):
        self.inc(X)
        self.dec(Y)
        self.selected.append(X)
        #print("select", X, "current", self.current)
    
    def get(self, X):
        return self.current[X]
    
    def abc(self):
        return sum(self.current.values())

    def run_commands(self):
        cmds = self.cmds
        for i, cmd in enumerate(cmds):
            if sum([self.get(c) for c in cmd]) == 0:
                self.answer = "No"
                return
            
            elif self.get(cmd[0]) ==  self.get(cmd[1]) == 1:
                another = (set("ABC") - set(cmd)).pop()
                if self.get(another) == 0 and i < len(cmds) -1:
                    next_cmds = cmd[i+1:]
                    for j, next_cmd in enumerate(next_cmds):
                        if next_cmd == cmd:
                            continue
                        else:
                            X = (set(cmd) & set(next_cmd)).pop()
                            XX = (set(cmd) - set([X])).pop()
                            if j % 2 == 0:
                                self.select(X, XX)
                            else:
                                self.select(XX, X)

                else:
                    self.select(cmd[0], cmd[1])
            else:
                small, big = sorted(cmd, key=lambda key: self.get(key))
                self.select(small, big)

            if list(map(lambda x: x < 0, self.current.values())).count(True) > 0:
                self.answer = "No"
                return
                
        self.answer = "Yes"

    def print_answer(self):
        print(self.answer)
        if self.answer == 'Yes':
            print("\n".join(self.selected))

def test():
    A,B,C = 2, 3, 4
    import numpy as np
    XX = ["AB", "BC", "CA"]
    cmds = [XX[i] for i in np.random.randint(0,3, [10])]
    resolver = ABC(A,B,C, cmds)
    resolver.run_commands()
    
    print(resolver.selected)
    print(resolver.current)
    resolver.print_answer()



def main():

    #test()

    N, A ,B, C = map(int, input().split())
    S = [input() for i in range(N)]
    
    resolver = ABC(A,B,C, S)
    resolver.run_commands()
    resolver.print_answer()



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve(writer):
    N, A, B, C = map(int, input().split())
    variables = {
        'A': A,
        'B': B,
        'C': C
    }

    queries = (list(input()) for _ in range(N))

    buffer = list()
    b = ''

    for i in range(N):
        if not buffer:
            t1, t2 = next(queries)
        else:
            b = buffer.pop()
            if b == 'pass':
                continue
            else:
                t1, t2 = list(b)

        v1 = variables[t1]
        v2 = variables[t2]

        if v1 == v2 == 0:
            return False
        elif v1 == v2 == 1:
            if i == N-1:
                return True

            nt1, nt2 = next(queries)
            if len({t1, t2, nt1, nt2}) == 2:
                buffer.append('pass')
                writer.append(t1)
                writer.append(t2)
                continue

            if t1 in {nt1, nt2}:
                variables[t1] += 1
                variables[t2] -= 1
                writer.append(t1)
            else:
                variables[t1] -= 1
                variables[t2] += 1
                writer.append(t2)

            buffer.append(''.join([nt1, nt2]))

        elif v1 > v2:
            variables[t1] -= 1
            variables[t2] += 1
            writer.append(t2)
        elif v2 > v1:
            variables[t1] += 1
            variables[t2] -= 1
            writer.append(t1)
        else:
            variables[t1] += 1
            variables[t2] -= 1
            writer.append(t1)

    return True


def main():
    writer = list()
    ok = solve(writer)
    if ok:
        print('Yes')
        for w in writer:
            print(w)
    else:
        print('No')


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def F(S,curr,D,res):
    #if D["A"]<0 or D["B"]<0 or D["C"]<0:
    #    return False
    if len(S)==curr:
        print("Yes")
        for c in res:
            print(c)
        #print("".join(res))
        return True
    s=S[curr]
    
    a=False
    b=False
    if D[s[0]]>=1:
        D[s[0]]-=1
        D[s[1]]+=1
        res.append(s[0])
        a=F(S,curr+1,D,res)
        res.pop(-1)
        D[s[0]]+=1
        D[s[1]]-=1
    if a:
        return a

    if D[s[1]]>=1:
            D[s[1]]-=1
            D[s[0]]+=1
            res.append(s[0])
            b=F(S,curr+1,D,res)
            res.pop()
            D[s[1]]+=1
            D[s[0]]-=1
    return a or b


N,A,B,C=map(int,input().strip().split())
D={"A":A,"B":B,"C":C}
S=[]
res=[]
for i in range(N):
    S.append(input())
if not (F(S,0,D,res)):
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def F(S,curr,D,res):
    
    if len(S)==curr:
        print("Yes")
        for c in res:
            print(c)
        #print("".join(res))
        return True
    s=S[curr]
    
    a=False
    b=False
    if D[s[0]]>=1:
        D[s[0]]-=1
        D[s[1]]+=1
        res.append(s[0])
        a=F(S,curr+1,D,res)
        res.pop(-1)
        D[s[0]]+=1
        D[s[1]]-=1
    if a:
        return a

    if D[s[1]]>=1:
            D[s[1]]-=1
            D[s[0]]+=1
            res.append(s[1])
            b=F(S,curr+1,D,res)
            res.pop()
            D[s[1]]+=1
            D[s[0]]-=1
    return a or b


N,A,B,C=map(int,input().strip().split())
D={"A":A,"B":B,"C":C}
S=[]
res=[]
for i in range(N):
    S.append(input())
if not (F(S,0,D,res)):
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def F(S,curr,D,res):
    
    if len(S)==curr:
        print("Yes")
        for c in res:
            print(c)
        #print("".join(res))
        return True
    s=S[curr]
    
    a=False
    b=False
    if D[s[0]]>=1:
        D[s[0]]-=1
        D[s[1]]+=1
        res.append(s[0])
        a=F(S,curr+1,D,res)
        res.pop(-1)
        D[s[0]]+=1
        D[s[1]]-=1
    if D[s[1]]>=1:
            D[s[1]]-=1
            D[s[0]]+=1
            res.append(s[1])
            b=F(S,curr+1,D,res)
            res.pop()
            D[s[1]]+=1
            D[s[0]]-=1
    return a or b


N,A,B,C=map(int,input().strip().split())
D={"A":A,"B":B,"C":C}
S=[]
res=[]
for i in range(N):
    S.append(input())
if not (F(S,0,D,res)):
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

def F(S,curr,D,res):
    
    if len(S)==curr:
        print("Yes")
        for c in res:
            print(c)
        #print("".join(res))
        return True
    s=S[curr]
    
    a=False
    b=False
    if D[s[0]]>=1:
        D[s[0]]-=1
        D[s[1]]+=1
        res.append(s[0])
        a=F(S,curr+1,D,res)
        res.pop(-1)
        D[s[0]]+=1
        D[s[1]]-=1
    else:
        if D[s[1]]>=1:
            D[s[1]]-=1
            D[s[0]]+=1
            res.append(s[1])
            b=F(S,curr+1,D,res)
            res.pop()
            D[s[1]]+=1
            D[s[0]]-=1
    return a or b


N,A,B,C=map(int,input().strip().split())
D={"A":A,"B":B,"C":C}
S=[]
res=[]
for i in range(N):
    S.append(input())
if not (F(S,0,D,res)):
    print("No")

Submission status: Wrong Answer

Solution code:
import sys

def str_to_int(ch):
	if ch =='A':
		return 0
	if ch =='B':
		return 1
	if ch =='C':
		return 2
def int_to_str(ch):
	if ch ==0:
		return 'A'
	if ch ==1:
		return 'B'
	if ch ==2:
		return 'C'

N, A, B, C = list(map(int, input().split()))
ABC =[0, 0, 0]
ABC[0] = A
ABC[1] = B
ABC[2] = C

ops = []
for _ in range(N):
	l =list(map(str_to_int, list(input())))
	ops.append(l)

if sum(ABC) == 0:
	print("No")
	sys.exit(0)

ans = []
for idx, op in enumerate(ops):
	if ABC[op[0]] == 0 and ABC[op[1]] == 0:
		print("No")
		sys.exit(0)
	elif ABC[op[1]] == 0:
		inc = op[1]
		dec = op[0]
	elif ABC[op[0]] == 0:
		inc = op[0]
		dec = op[1]
	elif idx +1 < len(ops) and op[0] in ops[idx+1]:
		inc = op[0]
		dec = op[1]
	else:
		inc = op[0]
		dec = op[1]
	ans.append(inc)
	ABC[dec]-=1
	ABC[inc]+=1
print("Yes")
for i in map(int_to_str, ans):
	print(i)
sys.exit(0)