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:
n,a,b,c=map(int,input().split())
k=[]
if a+b+c==0:
  print('No')
elif a+b+c==1:
  if a==1:
    current=0
  if b==1:
    current=1
  if c==1:
    current=2
  breakflag=0
  for i in range(n):
    s=input()
    if s=='AB':
      if current==2:
        breakflag=1
      else:
        current=1-current
        k.append(current)
    if s=='AC':
      if current==1:
        breakflag=1
      else:
        current=2-current
        k.append(current)
    if s=='BC':
      if current==0:
        breakflag=1
      else:
        current=3-current
        k.append(current)
    if breakflag==1:
      print('No')
      break
    if i==n-1:
      print('Yes')
      for j in range(n):
        if k[j]==0:
          print('A')
        elif k[j]==1:
          print('B')
        else:
          print('C')
elif a+b+c==2:
  s=input()
  breakflag=0
  for i in range(n-1):
    s2=input()
    if s=='AB':
      if a==0 and b==0:
        breakflag=1
      elif a>b:
        a-=1
        b+=1
        k.append('B')
      elif b>a:
        b-=1
        a+=1
        k.append('A')
      else:
        if s2=='AC':
          a+=1
          b-=1
          k.append('A')
        else:
          a-=1
          b+=1
          k.append('B')
    elif s=='AC':
      if a==0 and c==0:
        breakflag=1
      elif a>c:
        a-=1
        c+=1
        k.append('C')
      elif c>a:
        c-=1
        a+=1
        k.append('A')
      else:
        if s2=='AB':
          a+=1
          c-=1
          k.append('A')
        else:
          a-=1
          c+=1
          k.append('C')
    elif s=='BC':
      if b==0 and c==0:
        breakflag=1
      elif b>c:
        b-=1
        c+=1
        k.append('C')
      elif c>b:
        c-=1
        b+=1
        k.append('B')
      else:
        if s2=='AB':
          b+=1
          c-=1
          k.append('B')
        else:
          b-=1
          c+=1
          k.append('C')
    if breakflag==1:
      print('No')
      break
    s=s2
  if breakflag==0:
    if s=='AB':
      if a==0 and b==0:
        print('No')
        breakflag=1
      elif a>=b:
        k.append('B')
      else:
        k.append('A')
    elif s=='AC':
      if a==0 and c==0:
        print('No')
        breakflag=1
      elif a>=c:
        k.append('C')
      else:
        k.append('A')
    else:
      if b==0 and c==0:
        print('No')
        breakflag=1
      elif b>=c:
        k.append('C')
      else:
        k.append('B')  
  if breakflag==0:
    print('Yes')
    for i in k:
      print(i)
else:
  breakflag=0
  for i in range(n):
    s=input()
    if s=='AB':
      if a==0 and b==0:
        breakflag=1
      elif a<b:
        a+=1
        b-=1
        k.append('A')
      else:
        b+=1
        a-=1
        k.append('B')
    if s=='AC':
      if a==0 and c==0:
        breakflag=1
      elif a<c:
        a+=1
        c-=1
        k.append('A')
      else:
        c+=1
        a-=1
        k.append('C')
    if s=='BC':
      if b==0 and c==0:
        breakflag=1
      elif b<c:
        b+=1
        c-=1
        k.append('B')
      else:
        c+=1
        b-=1
        k.append('C')
    if breakflag==1:
      print('No')
      break
  if breakflag==0:
    print('Yes')
    for i in k:
      print(i)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
result = []
def calc(a, b, c, i):
    if a < 0 or b < 0 or c < 0:
        return False
    if i >= N:
        return True
    command = s[i]
    if command == "AB":
        if calc(a+1, b-1, c, i+1):
            result.append("A")
            return True
        if calc(a-1, b+1, c, i+1):
            result.append("B")
            return True
        return False
    elif command == "AC":
        if calc(a+1, b, c-1, i+1):
            result.append("A")
            return True
        if calc(a-1, b, c+1, i+1):
            result.append("C")
            return True
        return False
    elif command == "BC":
        if calc(a, b-1, c+1, i+1):
            result.append("C")
            return True
        if calc(a, b+1, c-1, i+1):
            result.append("B")
            return True
        return False
def main():
    global N, s
    N, A, B, C = map(int, input().split())
    s = [input() for _ in range(N)]
    if calc(A, B, C, 0):
        print("Yes")
        for c in result[::-1]:
            print(c)
    else:
        print("No")
main()

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math


NN = 202020
MOD = 10**9+7
# MOD = 998244353
INF = float("inf")

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

ans = []
for i in range(n):
    if s[i] == "AB":
        if num[0] > num[1]:
            num[0], num[1] = num[0]-1, num[1]+1
            ans.append("B")
        elif num[0] < num[1]:
            num[0], num[1] = num[0]+1, num[1]-1
            ans.append("A")
        else:
            if num[0] == num[1] == 0:
                print("No")
                exit()
            if i < n-1 and s[i+1] == "BC":
                num[0], num[1] = num[0] - 1, num[1] + 1
                ans.append("B")
            else:
                num[0], num[1] = num[0] + 1, num[1] - 1
                ans.append("A")

    elif s[i] == "AC":
        if num[0] > num[2]:
            num[0], num[2] = num[0]-1, num[2]+1
            ans.append("C")
        elif num[0] < num[2]:
            num[0], num[2] = num[0]+1, num[2]-1
            ans.append("A")
        else:
            if num[0] == num[2] == 0:
                print("No")
                exit()
            if i < n-1 and s[i+1] == "BC":
                num[0], num[2] = num[0] - 1, num[2] + 1
                ans.append("C")
            else:
                num[0], num[2] = num[0] + 1, num[2] - 1
                ans.append("A")

    elif s[i] == "BC":
        if num[1] > num[2]:
            num[1], num[2] = num[1]-1, num[2]+1
            ans.append("C")
        elif num[1] < num[2]:
            num[1], num[2] = num[1]+1, num[2]-1
            ans.append("B")
        else:
            if num[1] == num[2] == 0:
                print("No")
                exit()
            if i < n-1 and s[i+1] == "AC":
                num[1], num[2] = num[1] - 1, num[2] + 1
                ans.append("C")
            else:
                num[1], num[2] = num[1] + 1, num[2] - 1
                ans.append("B")
    # print(num)

print("Yes")
for i in range(n):
    print(ans[i])

Submission status: Accepted

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

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

ans = []
ans_append = ans.append

S = [str(input()) for _ in range(N)]

for i in range(N):
    s = S[i]
    if s == 'AB\n':
        if A == B == 0:
            print ('No')
            exit()
        elif A == 0:
            A += 1
            B -= 1
            ans_append('A')
        elif B == 0:
            B += 1
            A -= 1
            ans_append('B')
        elif A * B > 0:
            if i == N - 1:
                A += 1
                B -= 1
                ans_append('A')
            else :
                if S[i + 1] == 'BC\n':
                    B += 1
                    A -= 1
                    ans_append('B')
                elif S[i + 1] == 'AC\n':
                    A += 1
                    B -= 1
                    ans_append('A')
                else:
                    if A < B:
                        A += 1
                        B -= 1
                        ans_append('A')
                    else:
                        B += 1
                        A -= 1
                        ans_append('B')

    elif s == 'BC\n':
        if B == C == 0:
            print ('No')
            exit()
        elif B == 0:
            B += 1
            C -= 1
            ans_append('B')
        elif C == 0:
            C += 1
            B -= 1
            ans_append('C')
        elif B * C > 0:
            if i == N - 1:
                B += 1
                C -= 1
                ans_append('B')
            else:
                if S[i + 1] == 'AB\n':
                    B += 1
                    C -= 1
                    ans_append('B')
                elif S[i + 1] == 'AC':
                    C += 1
                    B -= 1
                    ans_append('C')
                else:
                    if B < C:
                        B += 1
                        C -= 1
                        ans_append('B')
                    else:
                        C += 1
                        B -= 1
                        ans_append('C')

    else: #AC
        if C == A == 0:
            print ('No')
            exit()
        elif A == 0:
            A += 1
            C -= 1
            ans_append('A')
        elif C == 0:
            C += 1
            A -= 1
            ans_append('C')
        elif A * C > 0:
            if i == N - 1:
                A += 1
                C -= 1
                ans_append('A')
            else:
                if S[i + 1] == 'AB\n':
                    A += 1
                    C -= 1
                    ans_append('A')
                elif S[i + 1] == 'BC\n':
                    C += 1
                    A -= 1
                    ans_append('C')
                else:
                    if A > C:
                        C += 1
                        A -= 1
                        ans_append('C')
                    else:
                        A += 1
                        C -= 1
                        ans_append('A')
    # print (A, B, C)

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

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
z=a+b+c
if z==0:
    print('No')
if z==2:
    ans=[]
    sl=[]
    for i in range(n):
        sl.append(input())
    i=0
    while i<n:
        s=sl[i]
        if s=='AB' and a==0 and b==0:
            print('No')
            exit()
        if s=='BC' and c==0 and b==0:
            print('No')
            exit()
        if s=='AC' and a==0 and c==0:
            print('No')
            exit()
        if s=='AB':
            if a<b:
                b-=1
                a+=1
                ans.append('A')
            elif a>b:
                a-=1
                b+=1
                ans.append('B')
            else:
                i+=1
                ff=1
                while i<n and sl[i]=='AB':
                    ff+=1
                    i+=1
                if i==n:
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('A')
                        else:
                            ans.append('B')
                    break
                if sl[i]=='AC':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('A')
                            a+=1
                            b-=1
                        else:
                            ans.append('B')
                            b+=1
                            a-=1
                if sl[i]=='BC':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('B')
                            b+=1
                            a-=1
                        else:
                            ans.append('A')
                            b-=1
                            a+=1
                            
                continue
        if s=='AC':
            if c<a:
                a-=1
                c+=1
                ans.append('C')
            elif c>a:
                c-=1
                a+=1
                ans.append('A')
            else:
                i+=1
                ff=1
                while i<n and sl[i]=='AC':
                    ff+=1
                    i+=1
                if i==n:
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('A')
                        else:
                            ans.append('C')
                    break
                if sl[i]=='AB':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('A')
                            a+=1
                            c-=1
                        else:
                            ans.append('C')
                            a-=1
                            c+=1
                if sl[i]=='BC':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('C')
                            c+=1
                            a-=1
                        else:
                            ans.append('A')
                            c-=1
                            a+=1                            
                continue
        if s=='BC':
            if c<b:
                b-=1
                c+=1
                ans.append('C')
            elif c>b:
                c-=1
                b+=1
                ans.append('B')
            else:
                i+=1
                ff=1
                while i<n and sl[i]=='BC':
                    ff+=1
                    i+=1
                if i==n:
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('C')
                        else:
                            ans.append('B')
                    break
                if sl[i]=='AC':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('C')
                            c+=1
                            b-=1
                        else:
                            ans.append('B')
                            c-=1
                            b+=1
                if sl[i]=='AB':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('B')
                            c-=1
                            b+=1
                        else:
                            ans.append('C')
                            c+=1
                            b-=1
                            
                continue
        i+=1
    print('Yes')
    for i in ans:
        print(i)     
if z>=3 or z==1:
    ans=[]
    for i in range(n):
        s=input()
        if s=='AB' and a==0 and b==0:
            print('No')
            exit()
        if s=='BC' and c==0 and b==0:
            print('No')
            exit()
        if s=='AC' and a==0 and c==0:
            print('No')
            exit()
        if s=='AB':
            if b<a:
                a-=1
                b+=1
                ans.append('B')
            else:
                b-=1
                a+=1
                ans.append('A')
        if s=='AC':
            if c<a:
                a-=1
                c+=1
                ans.append('C')
            else:
                c-=1
                a+=1
                ans.append('A')
        if s=='BC':
            if c<b:
                b-=1
                c+=1
                ans.append('C')
            else:
                c-=1
                b+=1
                ans.append('B')
    print('Yes')
    for i in ans:
        print(i)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N,A,B,C = map(int, input().split())
list_choice = [input() for i in range(N)]
list_choice.append(list_choice[-1])

def operation(A,B,C,S,nextS):
  if S=='AB':
    if max(A,B)==0:
      return ('No','',0,0,0)
    elif A==1 and B==1:
      if nextS=='AB' or nextS=='AC':
        return ('Yes','A',A+1,B-1,C)
      else:
        return ('Yes','B',A-1,B+1,C)  
    elif A>=B:
      return ('Yes','B',A-1,B+1,C)
    else: # A<B
      return ('Yes','A',A+1,B-1,C)
  elif S=='AC':
    if max(A,C)==0:
      return ('No','',0,0,0)
    elif A==1 and C==1:
      if nextS=='AB' or nextS=='AC':
        return ('Yes','A',A+1,B,C-1)
      else:
        return ('Yes','C',A-1,B,C+1)  
    elif A>=C:
      return ('Yes','C',A-1,B,C+1)
    else: # A<C
      return ('Yes','A',A+1,B,C-1)
  else: # S=='BC'
    if max(B,C)==0:
      return ('No','',0,0,0)
    elif B==1 and C==1:
      if nextS=='AB' or nextS=='BC':
        return ('Yes','B',A,B+1,C-1)
      else:
        return ('Yes','C',A,B-1,C+1)  
    elif B>=C:
      return ('Yes','C',A,B-1,C+1)
    else: # B<C
      return ('Yes','B',A,B+1,C-1)
  
def func(A,B,C,list_choice):
  list_add = []
  for i in range(N):
    answer,add,A,B,C = operation(A,B,C,list_choice[i],list_choice[i+1])
    if answer=='No':
      return ('No', [])
    else:
      list_add.append(add)
  return ('Yes',list_add)
  
answer, list_add = func(A,B,C,list_choice)
print(answer)
if answer == 'Yes':
  for add in list_add:
    print(add)

Submission status: Accepted

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


def no():
    print('No')
    exit()


ans = []
for sa, sb in zip(S, S[1:]):
    if sa == 'AB':
        if A == B == 0:
            no()
        elif B == 0:
            ans += ['B']
            B += 1
            A -= 1
        elif A == 0:
            ans += ['A']
            A += 1
            B -= 1
        elif A == 1 and C == 0 and sb == 'AC' or B > A:
            ans += ['A']
            A += 1
            B -= 1
        else:
            ans += ['B']
            B += 1
            A -= 1
    elif sa == 'BC':
        if B == C == 0:
            no()
        elif B == 0:
            ans += ['B']
            B += 1
            C -= 1
        elif C == 0:
            ans += ['C']
            C += 1
            B -= 1
        elif B == 1 and A == 0 and sb == 'AB' or C > B:
            ans += ['B']
            B += 1
            C -= 1
        else:
            ans += ['C']
            C += 1
            B -= 1
    elif sa == 'AC':
        if C == A == 0:
            no()
        elif C == 0:
            ans += ['C']
            C += 1
            A -= 1
        elif A == 0:
            ans += ['A']
            A += 1
            C -= 1
        elif A == 1 and B == 0 and sb == 'AB' or C > A:
            ans += ['A']
            A += 1
            C -= 1
        else:
            ans += ['C']
            C += 1
            A -= 1
print('Yes')
print('\n'.join(ans))

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,A,B,C=map(int,input().split())
z=[A,B,C].count(0)
s=[input() for i in range(N)]
t=1
if z==3:
    print("No")
    t=0
if z==2:
    if sum([A,B,C])==1:
        a,b,c=A,B,C
        for i in range(N):
            if s[i]=="AC":
                if A:
                    A-=1
                    C+=1
                elif C:
                    A+=1
                    C-=1
                else:
                    t=0
                    print("No")
                    break
            elif s[i]=="AB":
                if A:
                    A-=1
                    B+=1
                elif B:
                    A+=1
                    B-=1
                else:
                    t=0
                    print("No")
                    break
            if s[i]=="BC":
                if B:
                    B-=1
                    C+=1
                elif C:
                    B+=1
                    C-=1
                else:
                    t=0
                    print("No")
                    break
        if t:
            print("Yes")
            t=0
            A,B,C=a,b,c
            for i in range(N):
                if s[i]=="AC":
                    if A:
                        A-=1
                        C+=1
                        print("C")
                    elif C:
                        A+=1
                        C-=1
                        print("A")
                elif s[i]=="AB":
                    if A:
                        A-=1
                        print("B")
                        B+=1
                    elif B:
                        A+=1
                        B-=1
                        print("A")
                if s[i]=="BC":
                    if B:
                        B-=1
                        C+=1
                        print("C")
                    elif C:
                        B+=1
                        C-=1
                        print("B")
    else:
        if s[0]=="AB":
            if A+B==0:
                print("No")
                t=0
        elif s[0]=="AC":
            if A+C==0:
                print("No")
                t=0
        if s[0]=="BC":
            if C+B==0:
                print("No")
                t=0
if t:
    print("Yes")
    for i in range(N):
        if s[i]=="AB":
            if A==B==1 and C==0 and i!=N-1:
                if s[i+1]=="BC":
                    print("B")
                    B+=1
                    A-=1
                else:
                    print("A")
                    A+=1
                    B-=1
            else:
                if A<B:
                    print("A")
                    A+=1
                    B-=1
                else:
                    print("B")
                    B+=1
                    A-=1
        elif s[i]=="AC":
            if A==C==1 and B==0 and i!=N-1:
                if s[i+1]=="BC":
                    print("C")
                    C+=1
                    A-=1
                else:
                    print("A")
                    A+=1
                    C-=1
            else:
                if A<C:
                    print("A")
                    A+=1
                    C-=1
                else:
                    print("C")
                    C+=1
                    A-=1
        elif s[i]=="BC":
            if C==B==1 and A==0 and i!=N-1:
                if s[i+1]=="AB":
                    print("B")
                    B+=1
                    C-=1
                else:
                    print("C")
                    C+=1
                    B-=1
            else:
                if C<B:
                    print("C")
                    C+=1
                    B-=1
                else:
                    print("B")
                    B+=1
                    C-=1

Submission status: Accepted

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

for i in range(n):
  s.append(input())

ans = []
flag = True
for i in range(n):
  if s[i] == "AB":
    if a < b:
      ans.append("A")
      a += 1
      b -= 1
    elif a == b == 1 and i < n - 1 and "A" in s[i + 1]:
      ans.append("A")
      a += 1
      b -= 1
    elif a == b == 0:
      flag = False
      break
    else:
      ans.append("B")
      a -= 1
      b += 1
  if s[i] == "AC":
    if a < c:
      ans.append("A")
      a += 1
      c -= 1
    elif a == c == 1 and i < n - 1 and "A" in s[i + 1]:
      ans.append("A")
      a += 1
      c -= 1
    elif a == c == 0:
      flag = False
      break
    else:
      ans.append("C")
      a -= 1
      c += 1
  if s[i] == "BC":
    if b < c:
      ans.append("B")
      b += 1
      c -= 1
    elif b == c == 1 and i < n - 1 and "B" in s[i + 1]:
      ans.append("B")
      b += 1
      c -= 1
    elif b == c == 0:
      flag = False
      break
    else:
      ans.append("C")
      b -= 1
      c += 1

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

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    N, A, B, C = read_values()
    res = "Yes\n"
    SS = [input().strip() for _ in range(N)]
    i = 0
    for i, S in enumerate(SS):
        if S == "AB":
            if A == 0 and B == 0:
                print("No")
                return

            if A == 1 and B == 1 and C == 0 and i != N - 1:
                T = SS[i + 1]
                if T == "AC":
                    res += "A\n"
                    A += 1
                    B -= 1 
                    continue

            if A >= B:
                res += "B\n"
                A -= 1
                B += 1
            else:
                res += "A\n"
                A += 1
                B -= 1

        elif S == "BC":
            if C == 0 and B == 0:
                print("No")
                return

            if A == 0 and B == 1 and C == 1 and i != N - 1:
                T = SS[i + 1]
                if T == "AC":
                    res += "C\n"
                    C += 1
                    B -= 1 
                    continue

            if C >= B:
                res += "B\n"
                C -= 1
                B += 1
            else:
                res += "C\n"
                C += 1
                B -= 1

        elif S == "AC":
            if A == 0 and C == 0:
                print("No")
                return

            if A == 1 and B == 0 and C == 1 and i != N - 1:
                T = SS[i + 1]
                if T == "AB":
                    res += "A\n"
                    A += 1
                    C -= 1 
                    continue

            if A >= C:
                res += "C\n"
                A -= 1
                C += 1
            else:
                res += "A\n"
                A += 1
                C -= 1

    print(res)


if __name__ == "__main__":
    main()