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())

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    q_copy = q[:]
    while len(q_copy) > 0:
        a, b, c, steps = q_copy.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b:
                    q.append((a-1, b+1, c, steps+"B"))
                elif b > a:
                    q.append((a+1, b-1, c, steps+"A"))
                else:
                    q.append((a-1, b+1, c, steps+"B"))
                    q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if max(a, c) > 0:
                if a > c:
                    q.append((a-1, b, c+1, steps+"C"))
                elif c > a:
                    q.append((a+1, b, c-1, steps+"A"))
                else:
                    q.append((a-1, b, c+1, steps+"C"))
                    q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if max(b, c) > 0:
                if b > c:
                    q.append((a, b-1, c+1, steps+"C"))
                elif b > a:
                    q.append((a, b+1, c-1, steps+"B"))
                else:
                    q.append((a, b+1, c-1, steps+"B"))
                    q.append((a, b-1, c+1, steps+"C"))

for i in q:
    if all(i[:-1]) and len(i[-1])==n:
        print("Yes")
        for char in i[-1]:
            print(char)
        break
else: print("No")

Submission status: Wrong Answer

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

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    q_copy = q[:]
    while len(q_copy) > 0:
        a, b, c, steps = q_copy.pop(0)
        if x == "AB":
            if max(b, c) > 0:
                if a > b:
                    q.append((a-1, b+1, c, steps+"B"))
                elif b > a:
                    q.append((a+1, b-1, c, steps+"A"))
                else:
                    q.append((a-1, b+1, c, steps+"B"))
                    q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if max(b, c) > 0:
                if a > c:
                    q.append((a-1, b, c+1, steps+"C"))
                elif c > a:
                    q.append((a+1, b, c-1, steps+"A"))
                else:
                    q.append((a-1, b, c+1, steps+"C"))
                    q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if max(b, c) > 0:
                if b > c:
                    q.append((a, b-1, c+1, steps+"C"))
                elif b > a:
                    q.append((a, b+1, c-1, steps+"B"))
                else:
                    q.append((a, b+1, c-1, steps+"B"))
                    q.append((a, b-1, c+1, steps+"C"))

for i in q:
    if all(i[:-1]) and len(i[-1])==n:
        print("Yes")
        for char in i[-1]:
            print(char)
        break
else: print("No")

Submission status: Wrong Answer

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

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    q_copy = q[:]
    while len(q_copy) > 0:
        a, b, c, steps = q_copy.pop(0)
        if x == "AB":
            if a > b:
                q.append((a-1, b+1, c, steps+"B"))
            elif b > a:
                q.append((a+1, b-1, c, steps+"A"))
            else:
                q.append((a-1, b+1, c, steps+"B"))
                q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if a > c:
                q.append((a-1, b, c+1, steps+"C"))
            elif c > a:
                q.append((a+1, b, c-1, steps+"A"))
            else:
                q.append((a-1, b, c+1, steps+"C"))
                q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if b > c:
                q.append((a, b-1, c+1, steps+"C"))
            elif b > a:
                q.append((a, b+1, c-1, steps+"B"))
            else:
                q.append((a, b+1, c-1, steps+"B"))
                q.append((a, b-1, c+1, steps+"C"))

for i in q:
    if all(i[:-1]):
        print("Yes")
        for char in i[-1]:
            print(char)
        break
else: print("No")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

S = []
for _ in range(N):
  s = input()
  S.append(s)

ans = "Yes"
turn = []  

if A+B+C == 0:
  ans = "No"
elif A+B+C == 1:
  if ans == "Yes":
    for s in S:
      if ans != "Yes":
      	break 
      if s == "AB":
        if A + B == 0:
          ans = "No"
        elif A == 0:
          A = 1
          B = 0
          turn.append("A")
        else:
          B = 1
          A = 0
          turn.append("B")
      elif s == "BC":
        if C + B == 0:
          ans = "No"
        elif C == 0:
          C = 1
          B = 0
          turn.append("C")
        else:
          B = 1
          C = 0
          turn.append("B")
      else:
        if A + C == 0:
          ans = "No"
        elif A == 0:
          A = 1
          C = 0
          turn.append("A")
        else:
          C = 1
          A = 0
          turn.append("C")
else:
  if S[0] == "AB" and A+B == 0:
    ans = "No"
  elif S[0] == "BC" and B+C==0:
    ans = "No"
  elif S[0] == "AC" and A+C==0:
    ans = "No"
  if ans == "Yes":
    for s in S:
      if ans != "Yes":
        break
      if s == "AB":
        if A <= B or A==0:
          A += 1
          B -= 1
          turn.append("A")
        else:
          B += 1
          A -= 1
          turn.append("B")
      if s == "BC":
        if C <= B or C==0:
          C += 1
          B -= 1
          turn.append("C")
        else:
          B += 1
          C -= 1
          turn.append("B")
      if s == "AC":
        if A <= C or A==0:
          A += 1
          C -= 1
          turn.append("A")
        else:
          C += 1
          A -= 1
          turn.append("C")

if ans == "No":
  print("No")
else:
  print("Yes")
  for t in turn:
    print(t)

Submission status: Wrong Answer

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

if a+b+c==0:
  print('Yes' if n==0 else 'No')
else:
  ans,res = True,[]
  for _ in range(n):
    s = input()
    if s == 'AB':
      if a+b==0:ans=False
      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:ans=False
      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:ans=False
      if c < b:
        res.append('C')
        c,b = c+1,b-1
      else:
        res.append('B')
        c,b = c-1,b+1
  if ans:
    print('Yes')
    print('\n'.join(res))
  else:
    print('No')

Submission status: Wrong Answer

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

if a+b+c==0:
  print('No')
  exit(0)

# if a+b+c < 3:
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))

Submission status: Wrong Answer

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)


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))

Submission status: Wrong Answer

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 < 3:
  ans, res = True, []
  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')
  for x in res: print(x)
  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: Wrong Answer

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

choices = ""
posible = True

for _ in range(n):
	x = input()
	if x == "AB":
		if max(a, b)-1 < 0:
			posible = False
			break
		if a > b:
			choices += "B"
			b += 1
			a -= 1
		else:
			choices += "A"
			b -= 1
			a += 1
	elif x == "AC":
		if max(a, c)-1 < 0:
			posible = False
			break
		if a > c:
			choices += "C"
			c += 1
			a -= 1
		else:
			choices += "A"
			c -= 1
			a += 1
	elif x == "BC":
		if max(b, c)-1 < 0:
			posible = False
			break
		if b > c:
			choices += "C"
			c += 1
			b -= 1
		else:
			choices += "B"
			c -= 1
			b += 1

if posible:
	print("Yes")
	for i in choices:
		print(i)
else:
	print("No")

Submission status: Wrong Answer

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")

    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")
                
    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")

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

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

Submission status: Wrong Answer

Solution code:
# 実際にシミュレーションしていく方針で解く
N, A, B, C = map(int, input().split())
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 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"] == d["B"] == 0:
                    print("No")
                    exit()
    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 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"] == d["C"] == 0:
                    print("No")
                    exit()
    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 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"] == d["C"] == 0:
                    print("No")
                    exit()

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

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

Submission status: Wrong Answer

Solution code:
# 実際にシミュレーションしていく方針で解く
N, A, B, C = map(int, input().split())
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 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"] == d["B"] == 0:
                    print("No")
                    exit()
    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 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"] == d["C"] == 0:
                    print("No")
                    exit()
    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 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["A"] == d["B"] == 0:
                    print("No")
                    exit()

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

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

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
d = {'A':a, 'B':b, 'C':c}
cnt = 0
ans = []

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


for i, x in enumerate(s):
    if x == 'AB':
        if d['A'] > d['B']:
            if (d['A'] - 1 >= 0):
                if (d['B'] + 1 >= 0):
                    cnt += 1
                    ans.append('B')
                    d['A'] -= 1
                    #print(d['A'])
                    d['B'] += 1
                    #print(d['B'])
        elif d['A'] < d['B']:
            if (d['A'] + 1 >= 0):
                if (d['B'] - 1 >= 0):
                    cnt += 1
                    ans.append('A')
                    d['A'] += 1
                    #print(d['A'])
                    d['B'] -= 1
                    #print(d['B'])
        else:
            if (i % 2) == 0:
                if (d['A'] - 1 >= 0):
                    if (d['B'] + 1 >= 0):
                        cnt += 1
                        ans.append('B')
                        d['A'] -= 1
                        #print(d['A'])
                        d['B'] += 1
                        #print(d['B'])
            else:
                if (d['A'] + 1 >= 0):
                    if (d['B'] - 1 >= 0):
                        cnt += 1
                        ans.append('A')
                        d['A'] += 1
                        #print(d['A'])
                        d['B'] -= 1
                        #print(d['B'])   
            
    elif x == 'AC':
        if d['A'] > d['C']:
            if (d['A'] - 1 >= 0):
                if (d['C'] + 1 >= 0):
                    cnt += 1
                    ans.append('C')
                    d['A'] -= 1
                    #print(d['A'])
                    d['C'] += 1
                    #print(d['C'])
        elif d['A'] < d['C']:
            if (d['A'] + 1 >= 0):
                if (d['C'] - 1 >= 0):
                    cnt += 1
                    ans.append('A')
                    d['A'] += 1
                    #print(d['A'])
                    d['C'] -= 1
                    #print(d['C'])
        else:
            if (i % 2) == 0:
                if (d['A'] - 1 >= 0):
                    if (d['C'] + 1 >= 0):
                        cnt += 1
                        ans.append('C')
                        d['A'] -= 1
                        #print(d['A'])
                        d['C'] += 1
                        #print(d['B'])
            else:
                if (d['A'] + 1 >= 0):
                    if (d['C'] - 1 >= 0):
                        cnt += 1
                        ans.append('A')
                        d['A'] += 1
                        #print(d['A'])
                        d['C'] -= 1
                        #print(d['B'])   
    else:
        if d['B'] > d['C']:
            if (d['B'] - 1 >= 0):
                if (d['C'] + 1 >= 0):
                    cnt += 1
                    ans.append('C')
                    d['B'] -= 1
                    #print(d['B'])
                    d['C'] += 1
                    #print(d['C'])
        elif d['B'] < d['C']:
            if (d['B'] + 1 >= 0):
                if (d['C'] - 1 >= 0):
                    cnt += 1
                    ans.append('B')
                    d['B'] += 1
                    #print(d['B'])
                    d['C'] -= 1
                    #print(d['C'])
        else:
            if (i % 2) == 0:
                if (d['B'] - 1 >= 0):
                    if (d['C'] + 1 >= 0):
                        cnt += 1
                        ans.append('C')
                        d['B'] -= 1
                        #print(d['A'])
                        d['C'] += 1
                        #print(d['B'])
            else:
                if (d['B'] + 1 >= 0):
                    if (d['C'] - 1 >= 0):
                        cnt += 1
                        ans.append('B')
                        d['B'] += 1
                        #print(d['A'])
                        d['C'] -= 1
                        #print(d['B'])   
        

if cnt == n:
    print('Yes')
    for y in ans:
        print(y)
else:
    print('No')

Submission status: Wrong Answer

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[1 - si.index(adding)] += -1
    # print(si, *abc)
print(*ans, sep="\n")
pass

Submission status: Wrong Answer

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[1 - si.index(adding)] += -1
    else:
        adding = si[0]
        ans.append(adding)
        abc[ord(adding) - 65] += 1
        abc[1 - si.index(adding)] += -1

print(*ans, sep="\n")
pass

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
S = [input() for i in range(N)]
l = []
for i in range(N):
  if A < 0 or B < 0 or C < 0:
    print('No')
    break
  elif S[i] == 'AB':
    if A >= B:
      A = A - 1
      B = B + 1
      l.append('B')
    else:
      A = A + 1
      B = B - 1
      l.append('A')
  elif S[i] == 'BC':
    if B >= C:
      B = B - 1
      C = C + 1
      l.append('C')
    else:
      B = B + 1
      C = C - 1
      l.append('B')
  elif S[i] == 'AC':
    if A > C:
      A = A - 1
      C = C + 1
      l.append('C')
    else:
      A = A + 1
      C = C - 1
      l.append('A')
      
if A < 0 or B < 0 or C < 0:
  print('No')

elif len(l) == N:
  print('Yes')
  for i in range(N):
    print(l[i])

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
m = {'A':a, 'B':b, 'C':c}

ss = [input() for _ in range(n)]


if a+b+c == 0:
    print('No')
    exit(0)


ans = []
def _t(s0, s1):
    m[s0] = m[s0] + 1
    m[s1] = m[s1] - 1
    ans.append(s0)


if a+b+c == 1:
    for k, v in m.items():
        if v == 1:
            o = k

    ans = []
    for s in ss:
        if o not in s:
            print('No')
            exit(0)
        o = s.strip(o)
        ans.append(o)

elif a+b+c == 2:
    for i, s in enumerate(ss):
        if m[s[0]] + m[s[1]] == 0:
            print('No')
            exit(0)
        if m[s[0]] > m[s[1]]:
            _t(s[1], s[0])
        elif m[s[0]] < m[s[1]]:
            _t(s[0], s[1])
        else:
            if i < len(ss)-1 and s[0] in ss[i+1]:
                _t(s[0], s[1])
            else:
                _t(s[1], s[0])

else:
    for s in ss:
        if m[s[0]] + m[s[1]] == 0:
            print('No')
            exit(0)
        if m[s[0]] > m[s[1]]:
            _t(s[1], s[0])
        else:
            _t(s[0], s[1])

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

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
m = {'A':a, 'B':b, 'C':c}

ss = [input() for _ in range(n)]


if a+b+c == 0:
    print('No')
    exit(0)


ans = []
def _t(s0, s1):
    m[s0] = m[s0] + 1
    m[s1] = m[s1] - 1
    ans.append(s0)


if a+b+c == 1:
    for k, v in m.items():
        if v == 1:
            o = k

    ans = []
    for s in ss:
        if o not in s:
            print('No')
            exit(0)
        o = s.strip(o)
        ans.append(o)

elif a+b+c == 2:
    for i, s in enumerate(ss):
        if m[s[0]] + m[s[1]] == 0:
            print('No')
            exit(0)
        if m[s[0]] > m[s[1]]:
            _t(s[0], s[1])
        elif m[s[0]] < m[s[1]]:
            _t(s[1], s[0])
        else:
            if i < len(ss)-1 and s[0] in ss[i+1]:
                _t(s[0], s[1])
            else:
                _t(s[1], s[0])

else:
    for s in ss:
        if m[s[0]] + m[s[1]] == 0:
            print('No')
            exit(0)
        if m[s[0]] > m[s[1]]:
            _t(s[1], s[0])
        else:
            _t(s[0], s[1])

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