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:
def main():
    N, A, B, C = map(int, input().split())
    ab = [0]*N
    ac = [0]*N
    bc = [0]*N
    l = []
    for i in range(N):
        s = input()
        ab[i] = ab[i-1]
        ac[i] = ac[i-1]
        bc[i] = bc[i-1]
        if s == 'AB':
            ab[i] += 1
            l.append('AB')
        elif s == 'AC':
            ac[i] += 1
            l.append('AC')
        else:
            bc[i] += 1
            l.append('BC')
    AB = ab[-1]
    BC = bc[-1]
    AC = ac[-1]
    r = []
    for i in range(N):
        if l[i] == 'AB':
            if A == 0 and B == 0:
                print('No')
                return
            elif A == 0:
                B -= 1
                r.append('B')
            elif B == 0:
                A -= 1
                r.append('A')
            else:
                if B < A:
                    A -= 1
                    r.append('A')
                else:
                    B -= 1
                    r.append('B')
        if l[i] == 'AC':
            if A == 0 and C == 0:
                print('No')
                return
            elif A == 0:
                C -= 1
                r.append('C')
            elif C == 0:
                A -= 1
                r.append('A')
            else:
                if C < A:
                    A -= 1
                    r.append('A')
                else:
                    C -= 1
                    r.append('C')
        if l[i] == 'BC':
            if C == 0 and B == 0:
                print('No')
                return
            elif C == 0:
                B -= 1
                r.append('B')
            elif B == 0:
                C -= 1
                r.append('C')
            else:
                if B < C:
                    C -= 1
                    r.append('C')
                else:
                    B -= 1
                    r.append('B')
    print('Yes')
    for i in r:
        print(i)
main()

Submission status: Wrong Answer

Solution code:
import numpy as np
nabc=input()
[n, a, b, c] = [int(comp) for comp in nabc.split()]
ans = ''
flag = 1
for i in range(n):
    temp = input()
    if(temp == 'AB'):
        if(a > b):
            a -= 1
            b += 1
            ans += 'B'
        else:
            a += 1
            b -= 1
            ans += 'A'
    elif(temp == 'AC'):
        if(a > c):
            a -= 1
            c += 1
            ans += 'C'
        else:
            a += 1
            c -= 1
            ans += 'A'
    else:
        if(b > c):
            b -= 1
            c += 1
            ans += 'C'
        else:
            b += 1
            c -= 1
            ans += 'B'
    if(i != n -1):
        1
        ans += '\n'
    if(a < 0 or b < 0 or c < 0):
        flag = 0


if(flag):
    print(ans)

else:
    print('NO')

Submission status: Wrong Answer

Solution code:
import sys

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

S = []
m, t = min(A, B, C), (A, B, C)
for _ in range(N):
    s = input()
    if s == "AB":
        A, B, C = t[0], t[1], t[2]

        mt1, tt1, c1 = min(A-1, B+1, C), (A-1, B+1, C), "B"
        mt2, tt2, c2 = min(A+1, B-1, C), (A+1, B-1, C), "A"
    if s == "BC":
        A, B, C = t[0], t[1], t[2]

        mt1, tt1, c1 = min(A, B-1, C+1), (A, B-1, C+1), "C"
        mt2, tt2, c2 = min(A, B+1, C-1), (A, B+1, C-1), "B"
    if s == "AC":
        A, B, C = t[0], t[1], t[2]

        mt1, tt1, c1 = min(A-1, B, C+1), (A-1, B, C+1), "C"
        mt2, tt2, c2 = min(A+1, B, C-1), (A+1, B, C-1), "A"

    if mt1 > mt2:
        mt, tt, c = mt1, tt1, c1
    else:
        mt, tt, c = mt2, tt2, c2

    if mt == -1:
        print("No")
        sys.exit(0)

    m, t = mt, tt
    S += [c]

for c in S:
    print(c)

Submission status: Wrong Answer

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

ans = []
for s in S:
    if s == 'AB':
        if A > B:
            A -= 1
            B += 1
            ans.append('B')
        else:
            A += 1
            B -= 1
            ans.append('A')

    if s == 'AC':
        if A > C:
            A -= 1
            C += 1
            ans.append('C')
        else:
            A += 1
            C -= 1
            ans.append('A')

    if s == 'BC':
        if B > C:
            B -= 1
            C += 1
            ans.append('C')
        else:
            B += 1
            C -= 1
            ans.append('B')

    if A < 0 or B < 0 or C < 0:
        print('No')
        exit()

print('Yes')
print(*ans, sep="\n")

Submission status: Wrong Answer

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

if (a==0 and  b==1 and c==1) or (a==1 and  b==0 and c==1) or (a==1 and  b==1 and c==0):
    que = deque([[a, b, c, []]])
    for i in range(n):
        if not que:
            print("No")
            exit()
        len_que = len(que)
        for _ in range(len_que):
            a, b, c, ans = que.popleft()
            if s[i] == "AB":
                if a>=1 and b==0:
                    a -= 1
                    b += 1
                    ans.append("B")
                elif b>=1 and a==0:
                    a += 1
                    b -= 1
                    ans.append("A")
                elif a==0 and b==0: continue
            elif s[i] == "AC":
                if a>=1 and c==0:
                    a -= 1
                    c += 1
                    ans.append("C")
                elif c>=1and a==0:
                    a += 1
                    c -= 1
                    ans.append("A")
                elif a==0 and c==0: continue
            else:
                if b >= 1 and c==0:
                    b -= 1
                    c += 1
                    ans.append("C")
                elif c >= 1and b==0:
                    b += 1
                    c -= 1
                    ans.append("B")
                elif b==0 and c==0: continue  
            que.append([a, b, c, ans])
    a, b, c, ans = que.popleft()
    for i in ans:
        print(i)

if a==0 and  b==0:
    if s[0] == "AB":
        print("No")
        exit()
elif a==0 and  c==0:
    if s[0] == "AC":
        print("No")
        exit()
elif b==0 and  c==0:
    if s[0] == "BC":
        print("No")
        exit()

print("Yes")
for i in range(n):
    if s[i] == "AB":
        if a >= b:
            a -= 1
            b += 1
            print("B")
        else:
            a += 1
            b -= 1
            print("A")
    elif s[i] == "AC":
        if a >= c:
            a -= 1
            c += 1
            print("C")
        else:
            a += 1
            c -= 1
            print("A")
    else:
        if b >= c:
            b -= 1
            c += 1
            print("C")
        else:
            b += 1
            c -= 1
            print("B")

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
k = 0
if a == 0:
  k += 1
if b == 0:
  k += 1
if c == 0:
  k += 1

order = [0]*n
answer = [0]*n
for i in range(n):
  s = input()
  if s == "AB":
    order[i] = 1
  elif s == "BC":
    order[i] = 2
  else:
    order[i] = 3
ans = True
if k == 3:
  ans = False
if k == 2:
  for i in range(n):
    key =  order[i]
    if key == 1:
      if a == 0 and b == 0:
        ans = False
        break
      elif a == 0:
        a += 1
        b -= 1
        answer[i] = "A"
      elif b == 0:
        answer[i] = "B"
        a -= 1
        b += 1
      elif i < n-1 and a == 1 and (order[i+1] == 1 or order[i+1] == 3):
        a += 1
        b -= 1
        answer[i] = "A"
      elif i < n-1 and b == 1 and (order[i+1] == 2 or order[i+1] == 1):
        b += 1
        a -= 1
        answer[i] = "B"
      else:
        if a < b:
          a += 1
          b -= 1
          answer[i] = "A"
        else:
          a -= 1
          b += 1
          answer[i] = "B"
    if key == 2:
      if c == 0 and b == 0:
        ans = False
        break
      elif b == 0:
        b += 1
        c -= 1
        answer[i] = "B"
      elif c == 0:
        c += 1
        b -= 1
        answer[i] = "C"
      elif i < n-1 and c == 1 and (order[i+1] == 2 or order[i+1] == 3):
        c += 1
        b -= 1
        answer[i] = "C"
      elif i < n-1 and b == 1 and (order[i+1] == 1 or order[i+1] == 2):
        b += 1
        c -= 1
        answer[i] = "B"
      else:
        if b < c:
          b += 1
          c -= 1
          answer[i] = "B"
        else:
          b -= 1
          c += 1
          answer[i] = "C"
    if key == 3:
      if a == 0 and c == 0:
        ans = False
        break
      elif a == 0:
        a += 1
        c -= 1
        answer[i] = "A"
      elif c == 0:
        c += 1
        a -= 1
        answer[i] = "C"
      elif i < n-1 and a == 1 and (order[i+1] == 1 or order[i+1] == 3):
        a += 1
        c -= 1
        answer[i] = "A"
      elif i < n-1 and c == 1 and (order[i+1] == 2 or order[i+1] == 3):
        c += 1
        a -= 1
        answer[i] = "C"
      else:
        if a < c:
          a += 1
          c -= 1
          answer[i] = "A"
        else:
          a -= 1
          c += 1
          answer[i] = "C"
if k <= 1:
  for i in range(n):
    if order[i] == 1:
      if a == 0:
        a += 1
        b -= 1
        answer[i] = "A"
      elif b == 0:
        b += 1
        a -= 1
        answer[i] = "B"
      else:
        if a < b:
          a += 1
          b -= 1
          answer[i] = "A"
        else:
          answer[i] = "B"
          b += 1
          a -= 1
    if order[i] == 2:
      if c == 0:
        c += 1
        b -= 1
        answer[i] = "C"
      elif b == 0:
        b += 1
        c -= 1
        answer[i] = "B"
      else:
        if c < b:
          c += 1
          b -= 1
          answer[i] = "C"
        else:
          answer[i] = "B"
          b += 1
          c -= 1  
    if order[i] == 3:
      if a == 0:
        a += 1
        c -= 1
        answer[i] = "A"
      elif c == 0:
        c += 1
        a -= 1
        answer[i] = "C"
      else:
        if a < c:
          a += 1
          c -= 1
          answer[i] = "A"
        else:
          answer[i] = "C"
          c += 1
          a -= 1
if ans:
  print("Yes")
  for v in answer:
    print(v)
else:
  print("No")

Submission status: Wrong Answer

Solution code:
import sys
import bisect
inf = 10**15
mod = 10**9+7
ans = []
n,a,b,c = map(int, input().split())
for i in range(n):
    s = input()
    if s == 'AB':
        if a > b:
            a-=1
            b+=1
            ans.append('B')
        else:
            a+=1
            b-=1
            ans.append('A')
    elif s == 'BC':
        if b > c:
            b-=1
            c+=1
            ans.append('C')
        else:
            b+=1
            c-=1
            ans.append('B')
    else:
        if c > a:
            c-=1
            a+=1
            ans.append('A')
        else:
            c+=1
            a-=1
            ans.append('C')
    if a < 0 or b < 0 or c < 0:
        print('No')
        sys.exit()
if a >= 0 and b >= 0 and c >= 0:
    print('Yes')
    for num in ans:
        print(num)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c = [int(x) for x in input().split()]
li = []
for i in range(n):
    s = input()
    if s=="AC":
        if a==0 and c==0:
            print("No")
            break
        if a<c:
            c-=1
            a+=1
            li.append("A")
        else:
            c+=1
            a-=1
            li.append("C")
    elif s=="AB":
        if a==0 and b==0:
            print("No")
            break
        if a<b:
            b-=1
            a+=1
            li.append("A")
        else:
            b+=1
            a-=1
            li.append("B")
    else:
        if b==0 and c==0:
            print("No")
            break
        if b<c:
            c-=1
            b+=1
            li.append("B")
        else:
            c+=1
            b-=1
            li.append("C")
else:
    print("Yes")
    for i in li:
        print(i)

Submission status: Wrong Answer

Solution code:
n , a, b , c = map(int, input().split())
dic = { "A" : a, "B" : b , "C" : c}
ans = []
for i in range(n):
	s = input()
	if dic[s[0]] > dic[s[1]]:
		dic[s[0]] -=1
		dic[s[1]] +=1
		ans.append(s[1])
	else:
		dic[s[0]] +=1
		dic[s[1]] -=1
		ans.append(s[0])
	if dic[s[0]] < 0 or dic[s[1]] < 0:
		exit(print("No"))

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

Submission status: Wrong Answer

Solution code:
def solve():
    n, a, b, c = map(int, input().split())
    s = [input() for _ in range(n)]
    out = [[0, 0, 0] for _ in range(n)]
    tmp = [[0, 0, 0] for _ in range(n)]

    if s[0] == "AB":
        out[0] = [-1, 1, 0]
    elif s[0] == "BC":
        out[0] = [0, -1, 1]
    elif s[0] == "AC":
        out[0] = [-1, 0, 1]

    for i in range(n):
        if s[i] == "AB":
            tmp[i] = [1, 1, 0]
        if s[i] == "BC":
            tmp[i] = [0, 1, 1]
        if s[i] == "AC":
            tmp[i] = [1, 0, 1]

    for i in range(1, n):
        for j in range(3):
            out[i][j] = out[i-1][j] * tmp[i][j] * -1

        for j in range(3):
            if sum(out[i]) != 0 and tmp[i][j] != 0 and out[i][j] == 0:
                out[i][j] = -(sum(out[i]))
                break

    ans = []
    original = [a, b, c]
    for i in out:
        a += i[0]
        b += i[1]
        c += i[2]

    if 0 <= a and 0 <= b and 0 <= c:
        print("Yes")
        check = 1
        for i in out:
            for j in range(3):
                if i[j] == 1:
                    print(chr(ord("A") + j))
        return

    [a, b, c] = original
    for i in out:
        a -= i[0]
        b -= i[1]
        c -= i[2]

    if 0 <= a and 0 <= b and 0 <= c:
        print("Yes")
        check = 1
        for i in out:
            for j in range(3):
                if i[j] == -1:
                    print(chr(ord("A") + j))
        return
    print("No")

solve()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,A,B,C=MI()
    D = { "A":A,"B":B,"C":C }
    ans = []
    S=[ST() for _ in range(N)]
    for i,s in enumerate(S):
        if(D[s[0]]==D[s[1]]==0):
            print("No")
            return
        if(D[s[0]]<D[s[1]]):
            ans.append(s[0])
            D[s[0]]+=1
            D[s[1]]-=1
        elif(D[s[0]]<D[s[1]] or i==N-1):
            ans.append(s[1])
            D[s[0]]-=1
            D[s[1]]+=1
        else:
            ns = S[i+1]
            if(s[0] in ns):
                ans.append(s[0])
                D[s[0]]+=1
                D[s[1]]-=1
            else:
                ans.append(s[1])
                D[s[0]]-=1
                D[s[1]]+=1


    print("Yes")
    print(*ans,sep="\n")
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

SL = []
nonZero = 0
nonZero += A>0
nonZero += B>0
nonZero += C>0

for i in range(N):
    s = input()
    SL.append(s)


def op2(f, t):
    global A, B, C
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1

def op(f, t):
    global A, B, C
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1
    print(t)

def show():
    print("Yes")
    global A, B, C
    for s in SL:
        if s=="AB":
            if A > B:
                op("A", "B")
            else: op("B", "A")
        elif s=="BC":
            if B >C:
                op("B", "C")
            else: op("C", "B")
        elif s=="AC":
            if A >C:
                op("A", "C")
            else: op("C", "A")


def fight():
    global A, B, C
    pA = A
    pB = B
    pC = C
    for s in SL:
        if s=="AB":
            if A > B:
                op2("A", "B")
            else: op2("B", "A")
        elif s=="BC":
            if B >C:
                op2("B", "C")
            else: op2("C", "B")
        elif s=="AC":
            if A >C:
                op2("A", "C")
            else: op2("C", "A")
        print("fight: ", A, B, C)
        if min(A, B, C) <= -1:
            print("No")
            exit()
    else:
        A = pA
        B = pB
        C = pC
        show()



# main


if nonZero >=2:
    show()


elif nonZero == 1:
    if max(A, B, C) >= 2:
        s = SL[0]
        if A!=0:
            if s == "BC":
                print("No")
                exit()
        if B!=0:
            if s == "AC":
                print("No")
                exit()
        if C!=0:
            if s == "AB":
                print("No")
                exit()
        show()
    else:
        fight()
else:
    print("No")

Submission status: Wrong Answer

Solution code:

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


def score(start, queries):
    def rec(nums, d):
        if d >= len(queries):
            return min(nums)
        i,j = queries[d]
        nums[i] += 1
        nums[j] -= 1
        x = rec(nums, d+1)
        nums[i] -= 2
        nums[j] += 2
        y = rec(nums, d+1)
        nums[i] += 1
        nums[j] -= 1
        return max(x,y)
    return rec(start, 0)



Q = []
for _ in range(N):
    s = input()
    if s == 'AB':
        i,j = 0,1
    elif s == 'BC':
        i,j = 1,2
    else:
        i,j = 2,0

    Q.append((i,j))

nums = [A,B,C]
res = []
flag = True
for q,(i,j) in enumerate(Q):
    queries = Q[q+1:min(q+3, len(Q))]
    nums[i] += 1
    nums[j] -= 1
    x = score(nums, queries)
    nums[i] -= 2
    nums[j] += 2
    y = score(nums, queries)
    nums[i] += 1
    nums[j] -= 1
    if x > y:
        res.append(i)
        nums[i] += 1
        nums[j] -= 1
    else:
        res.append(j)
        nums[i] -= 1
        nums[j] += 1
    if max(x,y) < 0:
        flag = False
        break


if not flag:
    print('No')
else:
    print('Yes')
    res = tuple(str(chr(v+ord('A'))) for v in res)
    print(*res,sep='\n')

Submission status: Wrong Answer

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

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

    if s == "AB":
        if a > b:
            a -= 1
            b += 1
            ans.append("B")
        else:
            a += 1
            b -= 1
            ans.append("A")
    elif s == "AC":
        if a > c:
            a -= 1
            c += 1
            ans.append("C")
        else:
            a += 1
            c -= 1
            ans.append("A")
    else:
        if b > c:
            b -= 1
            c += 1
            ans.append("C")
        else:
            b += 1
            c -= 1
            ans.append("B")

if a < 0 or b < 0 or c < 0:
    print("No")
    exit(0)
else:
    print("Yes")

for c in ans:
    print(c)

Submission status: Wrong Answer

Solution code:
n,a,b,c = [int(j) for j in input().split()]
l = []
for i in range(n):
	s = input()
	l.append(s)
d = {"A":[0]*n, "B":[0]*n, "C":[0]*n}
# for j in l[-1]:
	# d[j].append(1)
for i in range(n-1, -1, -1):
	for j in l[i]:
		d[j][i] = 1
for i in range(n-2, -1, -1):
	for j in ["A", "B", "C"]:
		d[j][i]+=d[j][i+1]
ct = {"A":a, "B":b, "C":c}
ans = []
flag = True
# print(ct, d)
for i in range(n):
	# print(ct)
	if flag==False:
		break
	tmp = min([ct[j]-d[j][i] for j in l[i]])
	# print(tmp, "tmp")
	tflag = True
	for j in l[i]:
		if (ct[j]-d[j][i]) == tmp and tflag:
			ans.append(j)
			tflag = False
			ct[j]+=1
		else:
			# print(ct[j], j)
			ct[j]-=1
			if ct[j]<0:
				flag = False
				print("No")
				break

else:
	if flag:
		print("Yes")
		for i in ans:
			print(i)

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
v = [A, B, C]
s = [-1 for i in range(N)]
for i in range(N):
    t = input()
    if t == "AB":
        s[i] = 0
    if t == "BC":
        s[i] = 1
    if t == "CA":
        s[i] = 2

ans = [-1 for i in range(N)]
OK = True

for i in range(N):
    p = s[i]
    q = (s[i]+1)%3
    if v[p] < v[q]:
        ans[i] = p
        v[p] += 1
        v[q] -= 1
    elif v[p] > v[q]:
        ans[i] = q
        v[p] -= 1
        v[q] += 1
    elif v[p] > 0:
        if i == N-1:
            ans[i] = p
            v[p] += 1
            v[q] -= 1
        elif s[i+1] == q:
            ans[i] = q
            v[p] -= 1
            v[q] += 1
        else:
            ans[i] = p
            v[p] += 1
            v[q] -= 1
    else:
        OK = False
        break

ch = ["A", "B" ,"C"]
if OK:
    print("Yes")
    for i in range(N):
        print(ch[ans[i]])
else:
    print("No")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
sys.setrecursionlimit(300000)

YES = "Yes"  # type: str
NO = "No"  # type: str

def solve(N: int, A: int, B: int, C: int, S: "List[str]"):
    L = 'ABC'
    def rec(i, a, b, c, ll):
        if i == N:
            return ll
        s = S[i]
        vals = [a, b, c]
        if s == 'AB':
            u, v = 0, 1
        if s == 'BC':
            u, v = 1, 2
        if s == 'AC':
            u, v = 0, 2

        if vals[u] == 0 and vals[v] == 0:
            return False
        elif vals[u] == 0 or (vals[v] > 1 and vals[u] == 1):
            vals[u] += 1
            vals[v] -= 1
            return rec(i + 1, vals[0], vals[1], vals[2], ll + L[u])
        #elif vals[v] == 0:
        else:
            vals[u] -= 1
            vals[v] += 1
            return rec(i + 1, vals[0], vals[1], vals[2], ll + L[v])
        #else:
        #    if vals[u] > vals[v]:
        #        u, v = v, u
        #    vals[u] += 1
        #    vals[v] -= 1
        #    tmp = rec(i + 1, vals[0], vals[1], vals[2], ll + L[u])
        #    if tmp:
        #        return tmp
        #    else:
        #        vals[u] -= 2
        #        vals[v] += 2
        #        return rec(i + 1, vals[0], vals[1], vals[2], ll + L[v])

    ret = rec(0, A, B, C, '')
    if ret:
        print('Yes')
        for c in ret:
            print(c)
        '''
        vals = [A, B, C]
        for i in range(N):
            x = S[i]
            c = ret[i]
            for d in x:
                k = L.find(d)
                if d == c:
                    vals[k] += 1
                else:
                    vals[k] -= 1
            print(x, c)
            print(vals)
        '''
    else:
        print('No')

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, A, B, C, s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
number = {}
N, number["A"], number["B"], number["C"] = map(int, input().split())

listN = ["0"]*N

for i in range(N):
    s = str(input())
    s1 = s[0]
    s2 = s[1]
    if number[s1]<=number[s2]:
        listN[i] = s1
        number[s1] +=1
        number[s2] -=1
    else:
        listN[i] = s2
        number[s1] -=1
        number[s2] +=1
        
if (number["A"]<0)or(number["B"]<0)or(number["C"]<0):
    print ("No")
else:
    print ("Yes")
    for i in listN:
        print (i)

Submission status: Wrong Answer

Solution code:
#F
N,A,B,C = map(int,input().split())
S = [str(input()) for i in range(N)]
Ac = [0]*N
Bc = [0]*N
Cc = [0]*N

for i,s in enumerate(S):
    x,y = s
    if x == "A" or y == "A":
        Ac[i]+=1
    if x == "B" or y == "B":
        Bc[i]+=1
    if x == "C" or y == "C":
        Cc[i]+=1
        
Ac = Ac[::-1]
Bc = Bc[::-1]
Cc = Cc[::-1]
    
flag = True
ans = []
for i,s in enumerate(S):
    #print(i,s)
    if s == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        elif A == 0:
            A+=1
            B-=1
            ans.append("A")
        elif B == 0:
            A-=1
            B+=1
            ans.append("B")
        else:
            if A-Ac[i] >= B-Bc[i]:
                A-=1
                B+=1
                ans.append("B")
            else:
                A+=1
                B-=1
                ans.append("A")
    if s == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        elif A == 0:
            A+=1
            C-=1
            ans.append("A")
        elif C == 0:
            A-=1
            C+=1
            ans.append("C")
        else:
            if A-Ac[i] >= C-Cc[i]:
                A-=1
                C+=1
                ans.append("C")
            else:
                A+=1
                C-=1
                ans.append("A")
    if s == "BC":
        if C == 0 and B == 0:
            flag = False
            break
        elif C == 0:
            C+=1
            B-=1
            ans.append("C")
        elif B == 0:
            C-=1
            B+=1
            ans.append("B")
        else:
            if C-Cc[i] >= B-Bc[i]:
                C-=1
                B+=1
                ans.append("B")
            else:
                C+=1
                B-=1
                ans.append("C")
                
if flag:
    print("Yes")
    for a in ans:
        print(a)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
s.append("0")
score = {"A":a, "B":b, "C":c}
result = []
for i in range(n):
    x, y = list(s[i])
    if score[x] == score[y] == 1:
        if x in s[i+1]:
            score[x] += 1
            score[y] -= 1
            result.append(x) 
        else:
            score[y] += 1
            score[x] += 1
            result.append(y)
    else:
        if score[x] < score[y]:
            score[x] += 1
            score[y] -= 1
            result.append(x)
        else:
            score[y] += 1
            score[x] -= 1
            result.append(y)
    if score[y] < 0 or score[x] < 0:
        print("No")
        sys.exit()
print("Yes")
for i in result:
    print(i)