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:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

n, a, b, c = MAP()
abc  = [a, b, c]
arr = [['AB', 0, 1], ['AC', 0, 2], ['BC', 1, 2]]
s = [STR() for _ in range(n)]

cnt = [0, 0, 0]

for i in range(n):
    for j in range(3):
        if s[i] == arr[j][0]:
            cnt[arr[j][1]] += 1
            cnt[arr[j][2]] += 1

pls = [max(0, cnt[i] - abc[i]) for i in range(3)]
if sum(pls) > n:
    print('No')
    exit()

tmp = ['A', 'B', 'C']

arr2 = []
for ss in s:
    for j in range(3):
        if ss == arr[j][0]:
            if pls[arr[j][1]] >= pls[arr[j][2]]:
                arr2.append(tmp[arr[j][1]])
                pls[arr[j][1]] -= 1
                pls[arr[j][1]] = max(0, pls[arr[j][1]])
            else:
                arr2.append(tmp[arr[j][2]])
                pls[arr[j][2]] -= 1
                pls[arr[j][2]] = max(0, pls[arr[j][2]])
            break
if sum(pls):
    print('No')
else:
    print('Yes')
    for i in range(n):
        print(arr2[i])

Submission status: Wrong Answer

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

DP = [[0] * 3 for _ in range(N + 1)]
DP[0] = [A, B, C]

L = [""] * N
R = {}
R[0] = "A"
R[1] = "B"
R[2] = "C"
A = []
B = []
try :
  for i in range(N):
    t = s[i]
    if t == "AB":
      x = 0
      y = 1
    if t == "BC":
      x = 1
      y = 2
    if t == "AC":
      x = 0
      y = 2
    if DP[i][y] != 0:
      DP[i + 1] = DP[i]
      DP[i + 1][x] += 1
      DP[i + 1][y] -= 1
      L[i] = R[x]
      if x == 0:
        A.append([i, y])
      else:
        B.append([i, y])
    elif DP[i][x] != 0:
      DP[i + 1] = DP[i]
      DP[i + 1][x] -= 1
      DP[i + 1][y] += 1
      L[i] = R[y]
    else:
      if x == 0:
        if len(B) == 0:
          print("No")
          exit()
        z = 1
        k, n = pop(B)
      if x == 1:
        if len(A) == 0:
          print("No")
          exit()
        z = 0
        k, n = pop(A)
      DP[i + 1] = DP[i]
      DP[i + 1][z] -= 2
      DP[i + 1][n] += 2
      if n == x:
        x, y = y, x
      DP[i + 1][x] += 1
      DP[i + 1][y] -= 1
      if DP[i + 1][z] < 0:
        print("No")
        exit()
      L[i] = R[x]
      L[t] = R[y]

  print("Yes")
  for i in L:
    print(i)
except Exception:
  print("No")

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,(input().split()))
s = [[] for _ in range(N+1)]
for i in range(N):
    s[i] = input()
s[N] = "NOTHING"

flag = 0
ans = []
S = A+B+C

if S >= 3:
    for i in range(N):
        if s[i] == "AB":
            if A+B == 0:
                flag = 1
                break
            if A >= B:
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C == 0:
                flag = 1
                break
            if A >= C:
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C == 0:
                flag = 1
                break
            if B >= C:
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("B")
                B += 1
                C -= 1
    if flag == 1:
        print("No")
    else:
        print("Yes")
        for i in ans:
            print(i)

else:
    for i in range(N):
        if s[i] == "AB":
            if A+B == 0:
                flag = 1
                break
            if B == 0:
                ans.append("B")
                B += 1
                A -= 1
            if A == 0:
                ans.append("A")
                A += 1
                B -= 1
            if s[i+1] == "AC":
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C == 0:
                flag = 1
                break
            if C == 0:
                ans.append("C")
                C += 1
                A -= 1
            if A == 0:
                ans.append("A")
                A += 1
                C -= 1
            if s[i+1] == "BC":
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C == 0:
                flag = 1
                break
            if C == 1:
                ans.append("C")
                C += 1
                B -= 1
            if B == 1:
                ans.append("B")
                B += 1
                C -= 1
            if s[i+1] == "AC":
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("b")
                B += 1
                C -= 1

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

Submission status: Wrong Answer

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

flag = True

for x in S:
    if x == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        if A < B:
            A += 1
            B -= 1
            add.append("A")
        else:
            B += 1
            A -= 1
            add.append("B")
    elif x == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        if A < C:
            A += 1
            C -= 1
            add.append("A")
        else:
            C += 1
            A -= 1
            add.append("C")
    else:
        if B == 0 and C == 0:
            flag = False
            break
        if B < C:
            B += 1
            C -= 1
            add.append("B")
        else:
            C += 1
            B -= 1
            add.append("C")


if flag is True and A >= 0 and B >= 0 and C >= 0:
    print("Yes")
    for y in add:
        print(y)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,(input().split()))
s = [[] for _ in range(N+1)]
for i in range(N):
    s[i] = input()
s[N] = "NOTHING"

flag = 0
ans = []
S = A+B+C

if S >= 3:
    for i in range(N):
        if s[i] == "AB":
            if A+B == 0:
                flag = 1
                break
            if A >= B:
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C == 0:
                flag = 1
                break
            if A >= C:
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C == 0:
                flag = 1
                break
            if B >= C:
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("B")
                B += 1
                C -= 1
    if flag == 1:
        print("No")
    else:
        print("Yes")
        for i in ans:
            print(i)

else:
    for i in range(N):
        print(A,B,C)
        if s[i] == "AB":
            if A+B == 0:
                flag = 1
                break
            if B == 0:
                ans.append("B")
                B += 1
                A -= 1
            if A == 0:
                ans.append("A")
                A += 1
                B -= 1
            if s[i+1] == "AC":
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C == 0:
                flag = 1
                break
            if C == 0:
                ans.append("C")
                C += 1
                A -= 1
            if A == 0:
                ans.append("A")
                A += 1
                C -= 1
            if s[i+1] == "BC":
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C == 0:
                flag = 1
                break
            if C == 1:
                ans.append("C")
                C += 1
                B -= 1
            if B == 1:
                ans.append("B")
                B += 1
                C -= 1
            if s[i+1] == "AC":
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("b")
                B += 1
                C -= 1

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

Submission status: Wrong Answer

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

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


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    if A ==0 and B ==0 and C ==0:
        print(NO)
        return 

    def yes():
        tmp = [A,B,C]
        print(YES)
        for i in range(N):
            a = tmp[0]
            b = tmp[1]
            c = tmp[2]
            if s[i] == "AB":
                if a<=b:
                    a += 1
                    b -= 1
                    print("A")
                else:
                    a -=1
                    b += 1
                    print("B")
            elif s[i] == "AC":
                if a<=c:
                    a += 1
                    c -= 1
                    print('A')
                else:
                    a -=1
                    c += 1
                    print('C')
            else:
                if b<=c:
                    b += 1
                    c -= 1
                    print('B')

                else:
                    b -=1
                    c += 1
                    print('C')
            tmp = [a,b,c]
        return 

    if A+B+C>2 and [A,B,C].count(0) <= 1:
        yes()
    else:
        if A+B+C>2:
            if C>0:
                if s[0] == "AB":
                    print(NO)
                else:
                    yes()
            elif A>0:
                if s[0] == "BC":
                    print(NO)
                else:
                    yes()
            else: 
                if s[0] == "AC":
                    print(NO)
                else:
                    yes()
        else:
            if [A,B,C].count(0) == 2:
                tmp = [A,B,C]
                for i in range(N):
                    a = tmp[0]
                    b = tmp[1]
                    c = tmp[2]
                    if s[i] == "AB":
                        if a<=b:
                            a += 1
                            b -= 1
                        else:
                            a -=1
                            b += 1
                    elif s[i] == "AC":
                        if a<=c:
                            a += 1
                            c -= 1
                        else:
                            a -=1
                            c += 1
                    else:
                        if b<=c:
                            b += 1
                            c -= 1
                        else:
                            b -=1
                            c += 1
                    if a < 0 or b<0 or c<0:
                        print(NO)
                        return
                    tmp = [a,b,c]
                yes()
            elif [A,B,C].count(0) == 1:
                tmp = [A,B,C]
                print(YES)

                for i in range(N):
                    a = tmp[0]
                    b = tmp[1]
                    c = tmp[2]
                    if s[i] == "AB" and a==1 and b == 1:
                        if i+1<N:
                            if s[i+1] == "BC":
                                b += 1
                                c -= 1
                                print("B")
                            else:
                                b -= 1
                                c += 1
                                print("C")
                            tmp = [a,b,c]

                    if s[i] == "BC" and b==1 and c == 1:
                        if i+1<N:
                            if s[i+1] == "AC":
                                b -= 1
                                c += 1
                                print("C")
                            else:
                                b += 1
                                c -= 1
                                print("B")
                            tmp = [a,b,c]
                            continue
                    if s[i] == "AC" and a==1 and c == 1:
                        if i+1<N:
                            if s[i+1] == "BC":
                                c += 1
                                a -= 1
                                print("C")
                            else:
                                c -= 1
                                a += 1
                                print("A")     
                            tmp = [a,b,c]
                            continue               

                    if s[i] == "AB":
                        if a<=b:
                            a += 1
                            b -= 1
                            print("A")
                        else:
                            a -=1
                            b += 1
                            print("B")
                    elif s[i] == "AC":
                        if a<=c:
                            a += 1
                            c -= 1
                            print('A')
                        else:
                            a -=1
                            c += 1
                            print('C')
                    else:
                        if b<=c:
                            b += 1
                            c -= 1
                            print('B')

                        else:
                            b -=1
                            c += 1
                            print('C')
                    tmp = [a,b,c]
        return 
    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:
n,a,b,c = map(int,input().strip().split())
ca,cb,cc = a,b,c
ans  = []
queue = []
ind = 0
f = 0
for _ in range(n):
    st = input()
    if st=="AB":
        if ca>cb:
            ca -= 1
            cb += 1
            ans.append("A")
        elif cb>0:
            cb -= 1
            ca += 1
            ans.append("B")
        else:
            ans.append("AB")
            queue.append(ind)
            f = 1
    elif st=="BC":
        if cc>cb:
            cc -= 1
            cb += 1
            ans.append("C")
        elif cb>0:
            cb -= 1
            cc += 1
            ans.append("B")
        else:
            ans.append("BC")
            queue.append(ind)
            f = 1
    else:
        if ca>cc:
            ca -= 1
            cc += 1
            ans.append("A")
        elif cc>0:
            cc -= 1
            ca += 1
            ans.append("C")
        else:
            ans.append("AC")
            queue.append(ind)
            f = 1
    ind += 1
while queue:
    tqueue = []
    while queue:
        f  = 0 
        ind = queue.pop(0)
        st = ans[ind]
        if st=="AB":
            if ca>cb:
                ca -= 1
                cb += 1
                ans[ind] = "A"
                f = 0
            elif cb>0:
                cb -= 1
                ca += 1
                ans[ind] = "B"
                f = 0
            else:
                tqueue.append(ind)
                f = 1
        elif st=="BC":
            if cc>cb:
                cc -= 1
                cb += 1
                ans[ind] =  "C"
                f = 0
            elif cb>0:
                cb -= 1
                cc += 1
                ans[ind] = "B"
                f = 0
            else:
                tqueue.append(ind)
                f = 1
        else:
            if ca>cc:
                ca -= 1
                cc += 1
                ans[ind] = "A"
                f =0
            elif cc>0:
                cc -= 1
                ca += 1
                ans[ind] = "C"
                f = 0
            else:
                tqueue.append(ind)
                f = 1
    if f:
        break
    else:
        queue = tqueue.copy()
if not f:
    print("Yes")
    for val in ans:
        print(val)
else:
    print("No")

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==0 and B==0:
            print("No")
            exit()
        if A>=B:
            A-=1
            B+=1
            ans.append("B")
        else:
            A+=1
            B-=1
            ans.append("A")
    elif s=="BC":
        if B==0 and C==0:
            print("No")
            exit()
        if B>=C:
            B-=1
            C+=1
            ans.append("C")
        else:
            B+=1
            C-=1
            ans.append("B")
    else:
        if A==0 and C==0:
            print("No")
            exit()
        if A>=C:
            A-=1
            C+=1
            ans.append("C")
        else:
            A+=1
            C-=1
            ans.append("A")
print("Yes")
for i in range(N):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
res = []
for i in range(n):
    s = input()

    if(s == "AB"):
        if(a>b):
            b += 1
            a -= 1
            res.append("B")
        else:
            a += 1
            b -= 1
            res.append("A")
    if(s == "AC"):
        if(a<c):
            a += 1
            c -= 1
            res.append("A")
        else:
            a -= 1
            c += 1
            res.append("C")
    else:
        if(b<c):
            c -= 1
            b += 1
            res.append("B")
        else:
            c += 1
            b -= 1
            res. append("C")
    
    if(a<0 or b<0 or c < 0): 
        print("No")
        exit()

print("Yes")
for r in res:
    print(r)

Submission status: Wrong Answer

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

DP = [[0] * 3 for _ in range(N + 1)]
DP[0] = [A, B, C]

L = [""] * N
R = {}
R[0] = "A"
R[1] = "B"
R[2] = "C"
A = []
B = []
try :
  for i in range(N):
    t = s[i]
    if t == "AB":
      x = 0
      y = 1
    if t == "BC":
      x = 1
      y = 2
    if t == "AC":
      x = 0
      y = 2
    if DP[i][y] != 0:
      DP[i + 1] = DP[i]
      DP[i + 1][x] += 1
      DP[i + 1][y] -= 1
      L[i] = R[x]
      if x == 0:
        A.append([i, y])
      else:
        B.append([i, y])
    elif DP[i][x] != 0:
      DP[i + 1] = DP[i]
      DP[i + 1][x] -= 1
      DP[i + 1][y] += 1
      L[i] = R[y]
    else:
      if x == 0:
        if len(B) == 0:
          print("No")
          exit()
        z = 1
        k, n = pop(B)
      if x == 1:
        if len(A) == 0:
          print("No")
          exit()
        z = 0
        k, n = pop(A)
      DP[i + 1] = DP[i]
      DP[i + 1][z] -= 2
      DP[i + 1][n] += 2
      if n == x:
        x, y = y, x
      DP[i + 1][x] += 1
      DP[i + 1][y] -= 1
      if DP[i + 1][z] < 0:
        print("No")
        exit()
      L[i] = R[x]
      L[t] = R[y]

  print(B)
  print("Yes")
  for i in L:
    print(i)
except Exception:
  print("No")