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: Accepted

Solution code:
def put(ret, val):
    if val == 0:
        ret.append('A')
    elif val == 1:
        ret.append('B')
    else:
        ret.append('C')


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

s = []

for i in range(n):
    tmp = input()
    if tmp == 'AB':
        val = 0
    elif tmp == 'BC':
        val = 1
    else:
        val = 2
    s.append(val)

ret = []
if a + b + c == 0:
    print('No')
    exit()
elif a + b + c == 1:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 1:
            t[item] = 0
            t[(item+1) % 3] = 1
            put(ret, (item+1) % 3)
        elif t[(item+1) % 3] == 1:
            t[item] = 1
            t[(item+1) % 3] = 0
            put(ret, item)
        else:
            print('No')
            exit()
elif a + b + c == 2:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, (item+1) % 3)
        elif t[item] < t[(item+1) % 3]:
            t[item] += 1
            t[(item+1) % 3] -= 1
            put(ret, item)
        elif t[item] == 0:
            print('No')
            exit()
        elif t[item] == 2:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, (item+1) % 3)
        else:
            if len(s) > i + 1:
                if s[i+1] == (item + 1) % 3:
                    t[item] -= 1
                    t[(item+1) % 3] += 1
                    put(ret, (item+1) % 3)
                else:
                    t[item] += 1
                    t[(item+1) % 3] -= 1
                    put(ret, item)
            else:
                put(ret, item)

else:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, (item+1) % 3)
        else:
            t[item] += 1
            t[(item+1) % 3] -= 1
            put(ret, item)

print('Yes')
for item in ret:
    print(item)

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
column = [a, b, c]
dict_convert = {"A": 0, "B": 1, "C": 2}
reverse_convert = {0: "A", 1: "B", 2: "C"}
s = list()
for _ in range(n):
  pair = tuple(dict_convert[val] for val in tuple(input()))
  s.append(pair)
choices = list()
def generate_play():
  for i in range(n):
    if column[s[i][0]] == 0 and column[s[i][1]] == 0:
      return "No"
    elif column[s[i][0]] == 1 and column[s[i][1]] == 1 and i<n-1:
      other_index = next(x for x in range(3) if x not in (s[i][0], s[i][1]))
      if s[i+1] == (other_index, s[i][0]) or s[i+1] == (s[i][0], other_index):
        choice = s[i][0]
        column[s[i][1]] -= 1
        column[s[i][0]] += 1
      else:
        choice = s[i][1]
        column[s[i][1]] += 1
        column[s[i][0]] -= 1
      choices.append(choice)
    else:
      if column[s[i][0]] >= column[s[i][1]]:
        choice = s[i][1]
        column[s[i][1]] += 1
        column[s[i][0]] -= 1
      else:
        choice = s[i][0]
        column[s[i][1]] -= 1
        column[s[i][0]] += 1
      choices.append(choice)
  return "Yes"
ans = generate_play()
print(ans)
if ans == "Yes":
  [print(reverse_convert[val]) for val in choices]

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
s=[input() for _ in range(n)]
ans=[]
d={'A':a,'B':b,'C':c}
for i in range(n):
  x,y=list(s[i])
  if d[x]==0 and d[y]==0:
    print('No')
    exit()
  elif d[x]==1 and d[y]==1 and i<n-1:
    if x in s[i+1]:
      d[x]+=1
      d[y]-=1
      ans.append(x)
    else:
      d[x]-=1
      d[y]+=1
      ans.append(y)
  elif set((d[x],d[y]))==set((1,0)):
    d[x],d[y]=d[y],d[x]
    ans.append(x if d[x]>d[y] else y)
  elif d[x]>=d[y]:
    d[y]+=1
    d[x]-=1
    ans.append(y)
  else:
    d[y]-=1
    d[x]+=1
    ans.append(x)
print('Yes')
for ansi in ans:
  print(ansi)

Submission status: Accepted

Solution code:
def main():
    n, a, b, c = map(int, input().split())
    Ss = [input() for _ in range(n)]
    ans = []
    f = True
    if a+b+c == 0:
        print("No")
    elif a+b+c == 1:
        for i in range(n):
            if Ss[i] == "AB":
                if a == 0 and b == 0:
                    f = False
                    break
                else:
                    if a == 1:
                        ans.append("B")
                        a, b = a-1, b+1
                    else:
                        ans.append("A")
                        a, b = a+1, b-1
            elif Ss[i] == "AC":
                if a == 0 and c == 0:
                    f = False
                    break
                else:
                    if a == 1:
                        ans.append("C")
                        a, c = a-1, c+1
                    else:
                        ans.append("A")
                        a, c = a+1, c-1
            else:
                if b == 0 and c == 0:
                    f = False
                    break
                else:
                    if b == 1:
                        ans.append("C")
                        b, c = b-1, c+1
                    else:
                        ans.append("B")
                        b, c = b+1, c-1
        if f:
            print("Yes")
            for v in ans:
                print(v)
        else:
            print("No")
    else:
        for i in range(n):
            if Ss[i] == "AB":
                if a == 0 and b == 0:
                    f = False
                    break
                else:
                    if i < n-1:
                        if Ss[i+1] == "AB":
                            if b == 0:
                                ans.append("B")
                                a, b = a-1, b+1
                            else:
                                ans.append("A")
                                a, b = a+1, b-1
                        elif Ss[i+1] == "AC":
                            if b == 0:
                                ans.append("B")
                                a, b = a-1, b+1
                            else:
                                ans.append("A")
                                a, b = a+1, b-1
                        else:
                            if a == 0:
                                ans.append("A")
                                a, b = a+1, b-1
                            else:
                                ans.append("B")
                                a, b = a-1, b+1
                    else:
                        if a == 0 and b == 0:
                            f = False
                            break
                        elif b == 0:
                            ans.append("B")
                            a, b = a-1, b+1
                        else:
                            ans.append("A")
                            a, b = a+1, b-1
            elif Ss[i] == "AC":
                if a == 0 and c == 0:
                    f = False
                    break
                else:
                    if i < n-1:
                        if Ss[i+1] == "AB":
                            if c == 0:
                                ans.append("C")
                                a, c = a-1, c+1
                            else:
                                ans.append("A")
                                a, c = a+1, c-1
                        elif Ss[i+1] == "AC":
                            if c == 0:
                                ans.append("C")
                                a, c = a-1, c+1
                            else:
                                ans.append("A")
                                a, c = a+1, c-1
                        else:
                            if a == 0:
                                ans.append("A")
                                a, c = a+1, c-1
                            else:
                                ans.append("C")
                                a, c = a-1, c+1
                    else:
                        if a == 0 and c == 0:
                            f = False
                            break
                        elif c == 0:
                            ans.append("C")
                            a, c = a-1, c+1
                        else:
                            ans.append("A")
                            a, c = a+1, c-1
            else:
                if b == 0 and c == 0:
                    f = False
                    break
                else:
                    if i < n-1:
                        if Ss[i+1] == "AB":
                            if c == 0:
                                ans.append("C")
                                b, c = b-1, c+1
                            else:
                                ans.append("B")
                                b, c = b+1, c-1
                        elif Ss[i+1] == "AC":
                            if b == 0:
                                ans.append("B")
                                b, c = b+1, c-1
                            else:
                                ans.append("C")
                                b, c = b-1, c+1
                        else:
                            if b == 0:
                                ans.append("B")
                                b, c = b+1, c-1
                            else:
                                ans.append("C")
                                b, c = b-1, c+1
                    else:
                        if b == 0 and c == 0:
                            f = False
                            break
                        elif c == 0:
                            ans.append("C")
                            b, c = b-1, c+1
                        else:
                            ans.append("B")
                            b, c = b+1, c-1
        if f:
            print("Yes")
            for v in ans:
                print(v)
        else:
            print("No")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,a,b,c=map(int, input().split(' '))
# print(n,a,b,c)
left=[a,b,c]
ans=[]
prev=(-1,-1)
for i in range(0,n):
    s=input()
    # print(s)
    x = {'A':0,'B':1,'C':2}[s[0]]
    y = {'A':0,'B':1,'C':2}[s[1]]
    if left[x]==left[y]==0:
        x0,y0=prev
        if x0!=-1:
            left[x0]-=1
            left[y0]+=1
            if left[x0]==0:
                ans=[]
                break
            left[x0]-=1
            left[y0]+=1
            ans[len(ans)-1]="ABC"[y0]
        else:
            ans=[]
            break
    if left[x]>left[y]:
        x,y=y,x
    left[x]+=1
    left[y]-=1
    ans.append("ABC"[x])
    prev=(x,y)

if len(ans)==0:
    print("No")
else:
    print("Yes")
    for x in ans:
        print(x)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
d = {"A": A, "B": B, "C": C}
l = [input() for _ in range(N)]
v = A+B+C

def solve():
    res = []
    if v==0:
        return []
    elif v==1:
        for i in l:
            a, b = i
            if d[a]==0 and d[b]==0:
                return []
            elif d[a]>d[b]:
                d[a] -= 1
                d[b] += 1
                res.append(b)
            else:
                d[a] += 1
                d[b] -= 1
                res.append(a)
    else:
        a, b = l[0]
        if d[a]==0 and d[b]==0:
            return []
        else:
            for i, j in enumerate(l):
                a, b = j
                if d[a]>d[b]:
                    d[a] -= 1
                    d[b] += 1
                    res.append(b)
                elif d[a]==d[b]==1 and i+1<N:
                    e = set(l[i+1])&set([a, b])
                    if len(e)==1:
                        f = e.pop()
                        g = b if f==a else a
                        d[f] += 1
                        d[g] -= 1
                        res.append(f)
                    else:
                        d[a] -= 1
                        d[b] += 1
                        res.append(b)
                else:
                    d[a] += 1
                    d[b] -= 1
                    res.append(a)
    return res


r = solve()
if r:
    print("Yes")
    for i in r:
        print(i)
else:
    print("No")

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
d = {}
d["A"] = A
d["B"] = B
d["C"] = C
l = [input() for _ in range(N)]
v = A+B+C

def solve():
    res = []
    if v==0:
        return []
    elif v==1:
        for i in l:
            a, b = i
            if d[a]==0 and d[b]==0:
                return []
            elif d[a]>d[b]:
                d[a] -= 1
                d[b] += 1
                res.append(b)
            else:
                d[a] += 1
                d[b] -= 1
                res.append(a)
    else:
        a, b = l[0]
        if d[a]==0 and d[b]==0:
            return []
        else:
            for i, j in enumerate(l):
                a, b = j
                if d[a]>d[b]:
                    d[a] -= 1
                    d[b] += 1
                    res.append(b)
                elif d[a]==d[b]==1 and i+1<N:
                    e = set(l[i+1])&set([a, b])
                    if len(e)==1:
                        f = e.pop()
                        g = b if f==a else a
                        d[f] += 1
                        d[g] -= 1
                        res.append(f)
                    else:
                        d[a] -= 1
                        d[b] += 1
                        res.append(b)
                else:
                    d[a] += 1
                    d[b] -= 1
                    res.append(a)
    return res


r = solve()
if r:
    print("Yes")
    for i in r:
        print(i)
else:
    print("No")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n, *abc = map(int, input().split())
    query = []
    for _ in [0] * n:
        s = input()
        query.append((ord(s[0]) - ord('A'), ord(s[1]) - ord('A')))
    query.append((0, 1)) # 番兵

    ans = ['A'] * n
    for i in range(n):
        a, b = query[i]
        if abc[a] == 0 and abc[b] == 0:
            print("No")
            return
        elif abc[a] == 0:
            abc[a] += 1
            abc[b] -= 1
            ans[i] = chr(ord('A') + a)
        elif abc[b] == 0:
            abc[a] -= 1
            abc[b] += 1
            ans[i] = chr(ord('A') + b)
        else:
            if a in query[i+1]:
                abc[a] += 1
                abc[b] -= 1
                ans[i] = chr(ord('A') + a)
            else:                
                abc[a] -= 1
                abc[b] += 1
                ans[i] = chr(ord('A') + b)
    print("Yes")
    print(*ans, sep=('\n'))

main()

Submission status: Accepted

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10 ** 6)

*I, = open(0).read().split()
N, A, B, C = map(int, I[:4])
S = I[4:]

Cnt = {"A": A, "B": B, "C": C}

ans = [""] * N
def dfs(i):
    if i == N:
        print("Yes")
        print("\n".join(ans))
        quit()

    for L, R in (S[i], reversed(S[i])):
        if Cnt[R] > 0:
            Cnt[L] += 1
            Cnt[R] -= 1

            ans[i] = L
            dfs(i + 1)

            Cnt[L] -= 1
            Cnt[R] += 1


dfs(0)
print("No")

Submission status: Accepted

Solution code:
n,a,b,c = map(int, input().split())
ans = []
number = dict()
number['A'] = a
number['B'] = b
number['C'] = c
query = [input() for i in range(n)]
for i in range(n):
    s = query[i]
    s_1, s_2 = s[0], s[1]
    if number[s_1] == 0 and number[s_2] == 0:
        print("No")
        exit()
    else:
        if number[s_1] > number[s_2]:
            number[s_1] -= 1
            number[s_2] += 1
            ans.append(s_2)
        elif number[s_1] < number[s_2]:
            number[s_2] -= 1
            number[s_1] += 1
            ans.append(s_1)
        else:
            if i == n-1:
                number[s_2] -= 1
                number[s_1] += 1
                ans.append(s_1)
            else:
                if query[i+1] == s_1 + s_2:
                    number[s_2] -= 1
                    number[s_1] += 1
                    ans.append(s_1)
                else:
                    n_1,n_2 = query[i+1]
                    if s_1 in set([n_1,n_2]):
                        number[s_2] -= 1
                        number[s_1] += 1
                        ans.append(s_1)
                    else:
                        number[s_1] -= 1
                        number[s_2] += 1
                        ans.append(s_2)

    #print("A:{} B:{} C:{}".format(number['A'],number['B'],number['C']))
print("Yes")
for i in range(n):
    print(ans[i])

Submission status: Accepted

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

A, B, C = min(A, 2), min(B, 2), min(C, 2)
X = [0, A, B, C]  # 1-indexecの方が楽そう
F = {1: 'A', 2: 'B', 3: 'C'}

ans = []
for k, s in enumerate(S):
    if s == 'AB':
        i, j = 1, 2
    elif s == 'BC':
        i, j = 2, 3
    elif s == 'AC':
        i, j = 1, 3

    if X[i] > X[j]:
        X[i] -= 1
        X[j] += 1
        ans.append(j)

    elif X[i] < X[j]:
        X[i] += 1
        X[j] -= 1
        ans.append(i)

    else:
        if (X[i] == X[j] == 1) and (k < N - 1):
            if ((S[k + 1][0] == F[i]) and (S[k + 1][1] == F[i ^ j])) or ((S[k + 1][0] == F[i ^ j]) and (S[k + 1][1] == F[i])):
                X[i] += 1
                X[j] -= 1
                ans.append(i)
            else:
                X[j] += 1
                X[i] -= 1
                ans.append(j)
        else:
            X[i] -= 1
            X[j] += 1
            ans.append(j)

    if any([x < 0 for x in X]):
        print('No')
        exit()

print('Yes')
for a in ans:
    print(F[a])

Submission status: Accepted

Solution code:
import itertools as it

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

if a+b+c==0:
  print('Yes' if n==0 else 'No'),exit(0)

if a+b+c==1:
  res = []
  for _ in range(n):
    s = input()
    if s == 'AB':
      if a+b==0:print('No'),exit(0)
      if a < b:
        res.append('A')
        a,b = a+1,b-1
      else:
        res.append('B')
        a,b = a-1,b+1
    elif s == 'AC':
      if a+c==0:print('No'),exit(0)
      if a < c:
        res.append('A')
        a,c = a+1,c-1
      else:
        res.append('C')
        a,c = a-1,c+1
    elif s == 'BC':
      if c+b==0:print('No'),exit(0)
      if c < b:
        res.append('C')
        c,b = c+1,b-1
      else:
        res.append('B')
        c,b = c-1,b+1
  print('Yes'),print('\n'.join(res)),exit(0)


if a+b+c==2:
  ops = [input() for _ in range(n)]
  opt = iter(ops); next(opt)
  res = []
  for s,t in it.zip_longest(ops, opt):
    if s == 'AB':
      if a+b==0:print('No'),exit(0)
      if a==0:
        res.append('A')
        a,b = a+1,b-1
      elif b==0:
        res.append('B')
        a,b = a-1,b+1
      else:
        if t=='AC':
          res.append('A')
          a,b = a+1,b-1
        else:
          res.append('B')
          a,b = a-1,b+1
    elif s == 'AC':
      if a+c==0:print('No'),exit(0)
      if a==0:
        res.append('A')
        a,c = a+1,c-1
      elif c==0:
        res.append('C')
        a,c = a-1,c+1
      else:
        if t=='AB':
          res.append('A')
          a,c = a+1,c-1
        else:
          res.append('C')
          a,c = a-1,c+1
    elif s == 'BC':
      if c+b==0:print('No'),exit(0)
      if c==0:
        res.append('C')
        c,b = c+1,b-1
      elif b==0:
        res.append('B')
        c,b = c-1,b+1
      else:
        if t=='AC':
          res.append('C')
          c,b = c+1,b-1
        else:
          res.append('B')
          c,b = c-1,b+1
  print('Yes'),print('\n'.join(res)),exit(0)


s = input()
if  s == 'AB' and a+b==0 or\
    s == 'AC' and a+c==0 or\
    s == 'BC' and c+b==0 :
  print('No'),exit(0)

print('Yes')
for s in it.chain([s], (input() for _ in range(n-1))):
  if s == 'AB':
    if a < b:
      print('A')
      a,b = a+1,b-1
    else:
      print('B')
      a,b = a-1,b+1
  elif s == 'AC':
    if a < c:
      print('A')
      a,c = a+1,c-1
    else:
      print('C')
      a,c = a-1,c+1
  elif s == 'BC':
    if c < b:
      print('C')
      c,b = c+1,b-1
    else:
      print('B')
      c,b = c-1,b+1

Submission status: Accepted

Solution code:
# 実際にシミュレーションしていく方針で解く
N, A, B, C = map(int, input().split())
d = {}
d["A"] = A
d["B"] = B
d["C"] = C
S = [input() for _ in range(N)]
ans = []
for i in range(N):
    s = S[i]
    if s == "AB":
        if d["A"] > d["B"]:
            d["A"] -= 1
            d["B"] += 1
            ans.append("B")
        elif d["A"] < d["B"]:
            d["B"] -= 1
            d["A"] += 1
            ans.append("A")
        else:  # 同じ場合
            if d["A"] == d["B"] == 0:
                print("No")
                exit()
            if N > i+1:
                if "A" in S[i+1]:
                    d["B"] -= 1
                    d["A"] += 1
                    ans.append("A")
                elif "B" in S[i+1]:
                    d["A"] -= 1
                    d["B"] += 1
                    ans.append("B")
                else:
                    d["A"] -= 1
                    d["B"] += 1
                    ans.append("B")
            else:
                if d["A"] > 0:
                    ans.append("A")
                elif d["B"] > 0:
                    ans.append("B")

    if s == "AC":
        if d["A"] > d["C"]:
            d["A"] -= 1
            d["C"] += 1
            ans.append("C")
        elif d["A"] < d["C"]:
            d["C"] -= 1
            d["A"] += 1
            ans.append("A")
        else:
            if d["A"] == d["C"] == 0:
                print("No")
                exit()
            if N > i+1:
                if "A" in S[i+1]:
                    d["C"] -= 1
                    d["A"] += 1
                    ans.append("A")
                elif "C" in S[i+1]:
                    d["A"] -= 1
                    d["C"] += 1
                    ans.append("C")
                else:
                    d["A"] -= 1
                    d["C"] += 1
                    ans.append("C")
            else:
                if d["A"] > 0:
                    ans.append("A")
                elif d["C"] > 0:
                    ans.append("C")

    if s == "BC":
        if d["B"] > d["C"]:
            d["B"] -= 1
            d["C"] += 1
            ans.append("C")
        elif d["B"] < d["C"]:
            d["C"] -= 1
            d["B"] += 1
            ans.append("B")
        else:
            if d["B"] == d["C"] == 0:
                print("No")
                exit()
            if N > i+1:
                if "B" in S[i+1]:
                    d["C"] -= 1
                    d["B"] += 1
                    ans.append("B")
                elif "C" in S[i+1]:
                    d["B"] -= 1
                    d["C"] += 1
                    ans.append("C")
                else:
                    d["B"] -= 1
                    d["C"] += 1
                    ans.append("C")
            else:
                if d["B"] > 0:
                    ans.append("B")
                elif d["C"] > 0:
                    ans.append("C")

    if -1 in d.values():
        print("No")
        exit()

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

Submission status: Accepted

Solution code:
N, *abc = map(int, input().split())
s = [input() for _ in range(N)]

ans = ["Yes"]

for i, si in enumerate(s):
    indices = [ord(sii) - 65 for sii in si]
    if sum(abc[ind] for ind in indices) == 0:
        ans = ["No"]
        break
    if min(abc[ind] for ind in indices) == 0:
        if abc[indices[1]]:
            ans.append(chr(65 + indices[0]))
            abc[indices[0]] += 1
            abc[indices[1]] += -1
        else:
            ans.append(chr(65 + indices[1]))
            abc[indices[1]] += 1
            abc[indices[0]] += -1
    elif i < N - 1 and si != s[i + 1]:
        adding = set(si) & set(s[i + 1])
        if len(adding) == 0:
            adding = si[0]
        else:
            adding = adding.pop()
        ans.append(adding)
        abc[ord(adding) - 65] += 1
        abc[-65 + ord(si[1 - si.index(adding)])] += -1
    else:
        adding = si[0]
        ans.append(adding)
        abc[ord(adding) - 65] += 1
        abc[-65 + ord(si[1 - si.index(adding)])] += -1
    # print(si, *abc)
print(*ans, sep="\n")
pass

Submission status: Accepted

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

if a+b+c == 0:
  print("No")
  sys.exit()
elif a+b+c == 1:
  ans = [None]*n
  for i in range(n):
    s = input()
    if (s == "AB" and c == 1) or (s == "AC" and b == 1) or (s == "BC" and a == 1):
      print("No")
      sys.exit()
    if s == "AB":
      if a == 1:
        ans[i] = "B"
        a, b = 0, 1
      else:
        ans[i] = "A"
        a, b = 1, 0
    if s == "BC":
      if b == 1:
        ans[i] = "C"
        b, c = 0, 1
      else:
        ans[i] = "B"
        b, c = 1, 0
    if s == "AC":
      if c == 1:
        ans[i] = "A"
        c, a = 0, 1
      else:
        ans[i] = "C"
        c, a = 1, 0
  print("Yes")
  for i in range(n):
    print(ans[i])

else:
  ans = [None]*n
  s = [input() for _ in range(n)]
  for i in range(n):
    if (s[i] == "AB" and a == 0 and b == 0) or (s[i] == "AC" and c == 0 and a == 0) or (s[i] == "BC" and b == c == 0):
      print("No")
      sys.exit()
    if s[i] == "AB":
      if a == 0:
        ans[i] = "A"
        a += 1
        b -= 1
      elif b == 0:
        ans[i] = "B"
        a -= 1
        b += 1
      elif i != n-1 and s[i] != s[i+1]:
        if s[i+1] == "BC":
          ans[i] = "B"
          b += 1
          a -= 1
        if s[i+1] == "AC":
          ans[i] = "A"
          b -= 1
          a += 1
      else:
        if b > a:
          ans[i] = "A"
          a += 1
          b -= 1
        else:
          ans[i] = "B"
          a -= 1
          b += 1
          
      
    if s[i] == "BC":
      if b == 0:
        ans[i] = "B"
        b += 1
        c -= 1
      elif c == 0:
        ans[i] = "C"
        b -= 1
        c += 1
      elif i != n-1 and s[i] != s[i+1]:
        if s[i+1] == "AC":
          ans[i] = "C"
          c += 1
          b -= 1
        if s[i+1] == "AB":
          ans[i] = "B"
          c -= 1
          b += 1
      else:
        if b < c:
          ans[i] = "B"
          b += 1
          c -= 1
        else:
          ans[i] = "C"
          b -= 1
          c += 1
      
    if s[i] == "AC":
      if c == 0:
        ans[i] = "C"
        c += 1
        a -= 1
      elif a == 0:
        ans[i] = "A"
        c -= 1
        a += 1
      elif i != n-1 and s[i] != s[i+1]:
        if s[i+1] == "AB":
          ans[i] = "A"
          a += 1
          c -= 1
        if s[i+1] == "BC":
          ans[i] = "C"
          a -= 1
          c += 1
      else:
        if c < a:
          ans[i] = "C"
          c += 1
          a -= 1
        else:
          ans[i] = "A"
          c -= 1
          a += 1
  print("Yes")
  for i in range(n):
    print(ans[i])

Submission status: Accepted

Solution code:
import sys
n,a,b,c = map(int, input().split())
 
s = list()
for i in range(n):
    s.append(input())
 
r = list()
 
for i in range(n):
    if s[i]=='AB':
        if a>b:
            r.append('B')
            a=a-1
            b=b+1
        elif a<b:
            r.append('A')
            a=a+1
            b=b-1
        else:
            if i==n-1 or 'A' in s[i+1]:
                r.append('A')
                a=a+1
                b=b-1
            else: 
                r.append('B')
                a=a-1
                b=b+1
    elif s[i]=='AC':
        if a>c:
            r.append('C')
            a=a-1
            c=c+1
        elif a<c:
            r.append('A')
            a=a+1
            c=c-1
        else:
            if i==n-1 or 'A' in s[i+1]:
                r.append('A')
                a=a+1
                c=c-1
            else: 
                r.append('C')
                a=a-1
                c=c+1
    else:
        if b>c:
            r.append('C')
            b=b-1
            c=c+1
        elif b<c:
            r.append('B')
            b=b+1
            c=c-1
        else:
            if i==n-1 or 'B' in s[i+1]:
                r.append('B')
                b=b+1
                c=c-1
            else: 
                r.append('C')
                b=b-1
                c=c+1
    if min(a,b,c)<0:
        print('No')
        sys.exit()
 
 
print('Yes')
for str in r:
    print(str)