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
sys.setrecursionlimit(10**9)

N, A, B, C = list(map(int, input().split()))
s = [0] * (N+1)

for i in range(N):
    s[i] = input()
s[N] = 'AB'
cnt = 0
if A == 0:
    cnt += 1
if B == 0:
    cnt += 1
if C == 0:
    cnt += 1


if cnt == 3:
    print('No')
    sys.exit()

if not ((A == 1 and B == 1 and C == 0) or (A == 1 and B == 0 and C == 1) or (A == 0 and B == 1 and C == 1)):
    ans = [0] * N
    for i in range(N):
        if s[i] == 'AB':
            if A > B:
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
        if s[i] == 'AC':
            if A > C:
                A -= 1
                C += 1
                ans[i] = 'C'
            else:
                A += 1
                C -= 1
                ans[i] = 'A'
        if s[i] == 'BC':
            if B > C:
                B -= 1
                C += 1
                ans[i] = 'C'
            else:
                B += 1
                C -= 1
                ans[i] = 'B'
        if A < 0 or B < 0 or C < 0:
            print('No')
            sys.exit()
    print('Yes')
    for i in range(N):
        print(ans[i])
    sys.exit()


# 1, 1, 0

for i in range(N):
    ans = [0] * N
    if s[i] == 'AB':
        if A > B:
            A -= 1
            B += 1
            ans[i] = 'B'
        elif A < B:
            A += 1
            B -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'BC':
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
    if s[i] == 'AC':
        if A > C:
            A -= 1
            C += 1
            ans[i] = 'C'
        elif A < C:
            A += 1
            C -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'AB':
                A += 1
                C -= 1
                ans[i] = 'A'
            else:
                A -= 1
                C += 1
                ans[i] = 'C'
    if s[i] == 'BC':
        if B > C:
            B -= 1
            C += 1
            ans[i] = 'C'
        elif B < C:
            B += 1
            C -= 1
            ans[i] = 'B'
        else:
            if s[i+1] == 'AB':
                B += 1
                C -= 1
                ans[i] = 'B'
            else:
                B -= 1
                C += 1
                ans[i] = 'C'

    if A < 0 or B < 0 or C < 0:
        print('No')
        sys.exit()
print('Yes')
for i in range(N):
    print(ans[i])

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 -sum(n==0 for n in 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+4, 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())
s = []
for i in range(N):
    s.append(input())

if A == 0 and B == 0 and C == 0:
    print("No")
elif 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:
    flag = True
    ans = []
    for i in range(N):
        if s[i] == "AB":
            if A == 0 and B == 0:
                flag = False
                break
            elif A < B:
                A += 1
                B -= 1
                ans.append("A")
            else:
                A -= 1
                B += 1
                ans.append("B")
        elif s[i] == "BC":
            if B == 0 and C == 0:
                flag = False
                break
            elif B < C:
                B += 1
                C -= 1
                ans.append("B")
            else:
                B -= 1
                C += 1
                ans.append("C")
        else:
            if C == 0 and A == 0:
                flag = False
                break
            elif C < A:
                C += 1
                A -= 1
                ans.append("C")
            else:
                C -= 1
                A += 1
                ans.append("A")
    if flag:
        print("Yes")
        print(*ans)
    else:
        print("No")
elif A == 0 and B == 0 and C > 1 and s[0] == "AB" \
        or A == 0 and B > 1 and C == 0 and s[0] == "AC" \
        or A > 1 and B == 0 and C == 0 and s[0] == "BC":
        print("No")
else:
    print("Yes")
    flag = 0
    ans = []
    for i in range(N):
        if flag > 0:
            if flag == 1:
                if s[i] == "AB":
                    ans.append("A")
                    ans.append("B")
                    continue
                elif s[i] == "BC":
                    A -= 1
                    C += 1
                    ans.append("B")
                    ans.append("C")
                else:
                    B -= 1
                    C += 1
                    ans.append("A")
                    ans.append("C")
            elif flag == 2:
                if s[i] == "BC":
                    ans.append("B")
                    ans.append("C")
                    continue
                elif s[i] == "AC":
                    B -= 1
                    A += 1
                    ans.append("C")
                    ans.append("A")
                else:
                    C -= 1
                    A += 1
                    ans.append("B")
                    ans.append("A")
            elif flag == 3:
                if s[i] == "AC":
                    ans.append("A")
                    ans.append("C")
                    continue
                elif s[i] == "AB":
                    C -= 1
                    B += 1
                    ans.append("A")
                    ans.append("B")
                else:
                    A -= 1
                    B += 1
                    ans.append("C")
                    ans.append("B")
            flag = 0
        else:
            if s[i] == "AB":
                if A < B:
                    A += 1
                    B -= 1
                    ans.append("A")
                elif A == 1 and B ==1 and C == 0:
                    flag = 1
                else:
                    A -= 1
                    B += 1
                    ans.append("B")
            elif s[i] == "BC":
                if B < C:
                    B += 1
                    C -= 1
                    ans.append("B")
                elif A == 0 and B == 1 and C ==1:
                    flag = 2
                else:
                    B -= 1
                    C += 1
                    ans.append("C")
            else:
                if C < A:
                    C += 1
                    A -= 1
                    ans.append("C")
                elif C == 1 and A ==1 and B == 0:
                    flag = 3
                else:
                    C -= 1
                    A += 1
                    ans.append("A")
    print(*ans)

Submission status: Wrong Answer

Solution code:
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
# if s.count(0) <= 1 and sum(s) >= 3:
#     for i,t in enumerate(T):
#         nxt = [0,1,2]
#         nxt.remove(t)
#         if s[nxt[0]] < s[nxt[1]]:
#             ans[i] = ABC[nxt[0]]
#             s[nxt[0]] += 1
#             s[nxt[1]] -= 1
#         else:
#             ans[i] = ABC[nxt[1]]
#             s[nxt[0]] -= 1
#             s[nxt[1]] += 1
#     print('Yes')
#     print('\n'.join(ans))
if s.count(0) == 1 and s.count(1) == 2:
    now = s.index(0)
    i = 0
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        if T[i] == now:
            for j in range(3):
                if j not in [now,T[i]]:ans[i] = ABC[j]
        else:
            ans[i] = ABC[now]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Submission status: Wrong Answer

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

ans = "No"
q = [[] for i in range(N+1)]
q[0] = [(A,B,C)]
q2 = [[] for i in range(N+1)]
q2[0] = [[]]
for i in range(N):
  while q[i]:
    a,b,c = q[i].pop(0)
    trace = q2[i].pop(0)
    if s[i] == "AB":
      if a-1>0 and (a-1,b+1,c) not in q[i+1]:
        q[i+1].append((a-1,b+1,c))
        q2[i+1].append(trace+["B"])
      if b-1>0 and (a+1,b-1,c) not in q[i+1]:
        q[i+1].append((a+1,b-1,c))
        q2[i+1].append(trace+["A"])
    if s[i] == "BC":
      if b-1>0 and (a,b-1,c+1) not in q[i+1]:
        q[i+1].append((a,b-1,c+1))
        q2[i+1].append(trace+["C"])
      if c-1>0 and (a,b+1,c-1) not in q[i+1]:
        q[i+1].append((a,b+1,c-1))
        q2[i+1].append(trace+["B"])
    if s[i] == "AC":
      if a-1>0 and (a-1,b,c+1) not in q[i+1]:
        q[i+1].append((a-1,b,c+1))
        q2[i+1].append(trace+["C"])
      if c-1>0 and (a+1,b,c-1) not in q[i+1]:
        q[i+1].append((a+1,b,c-1))
        q2[i+1].append(trace+["A"])
if q[N]:
  trace = q2[N].pop(0)
  ans = "Yes"


print(ans)
if ans =="Yes":
  for v in trace:
    print(v)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, input().split())
d = {"A": a, "B": b, "C": c}
outputs = ["Yes"]

for i in range(n):
    s = input()
    fir = s[0]
    sec = s[1]
    if d[fir] == 0 and d[sec] == 0:
        print("No")
        exit()
    if d[fir] > d[sec]:
        outputs.append(sec)
        d[fir] -= 1
        d[sec] += 1
    else:
        outputs.append(fir)
        d[fir] += 1
        d[sec] -= 1
print("\n".join(outputs))

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
ans = []
s = [input() for i in range(n)]
for i in range(n):
    
    if s[i] == "AB":
        if a > b:
            a -= 1
            b += 1
            ans.append("B")
        elif a==b:
            if i < n-1:
                if s[i+1] == "AB":
                    a -= 1
                    b += 1
                    ans.append("B")
                elif s[i+1] == "AC":
                    a += 1
                    b -= 1
                    ans.append("A")
                else:
                    a -= 1
                    b += 1
                    ans.append("B")

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

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']

print('Yes')
for ss in s:
    for j in range(3):
        if ss == arr[j][0]:
            flag = True
            for k in range(1, 3):
                if pls[arr[j][k]] > 0 and flag:
                    print(tmp[arr[j][k]])
                    pls[arr[j][k]] -= 1
                    nxt = 2 if k == 1 else 1
                    abc[arr[j][nxt]] -= 1
                    if abc[arr[j][nxt]] < 0:
                        pls[arr[j][nxt]] += 1
                    flag = False
            if flag:
                print('A')
            break

Submission status: Wrong Answer

Solution code:
import sys
N, A, B, C = map(int, input().split())
d=dict()
d["A"] = A
d["B"] = B
d["C"] = C
h = {"AB":"C","BC":"A","AC":"B"}
S = []
for _ in range(N):
    S.append(input().strip())
if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    R = []
    for k in range(N):
        s = S[k]
        for i in range(2):
            if d[s[i]] == 1:
                R.append(s[1-i])
                d[s[i]] = 0
                d[s[1-i]] = 1
                break
        else:
            print("No")
            break
    else:
        print("Yes")
        for i in range(N):
            print(R[i])
else:
    R = []
    if d[S[0][0]] == 0 and d[S[0][1]] == 0:
        print("No")
        sys.exit()
    for i in range(N-1):
        s = S[i]
        if d[s[0]] == 1 and d[s[1]] == 1 and d[h[s]] == 0:
            if S[i+1] == s:
                d[s[0]] += 1
                d[s[1]] -= 1
                R.append(s[0])
            else:
                for j in range(2):
                    if s[j] not in S[i+1]:
                        d[s[j]] -= 1
                        d[s[j]] += 1
                        R.append(s[j])
                        break
        elif d[s[0]] >= d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            R.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            R.append(s[0])
    for i in range(2):
        if d[S[N-1][i]] > 0:
            R.append(S[N-1][i])
            break
    print("Yes")
    for i in range(N):
        print(R[i])

Submission status: Wrong Answer

Solution code:
import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, raw_input().split())
cmds = []

a_ = a
b_ = b
c_ = c

def print_res(cnts, res):
    # print cnts, res
    rem = [a_, b_, c_]
    p = [0, 0, 0]
    for i in xrange(3):
        p[i] = cnts[i] + res[i]
        assert p[i] % 2 == 0
        p[i] /= 2

    print 'Yes'
    for s in cmds:
        nr = {
            'AB': 0,
            'AC': 1,
            'BC': 2
        }[s]

        x, y = ord(s[0]) - ord('A'), ord(s[1]) - ord('A')

        if p[nr] > 0:
            p[nr] -= 1
            print s[0]
            rem[x] += 1
            rem[y] -= 1
        else:
            print s[1]
            rem[y] += 1
            rem[x] -= 1

    assert min(rem) >= 0


cnts = [0, 0, 0]

for i in xrange(n):
    s = raw_input()
    cmds.append(s)

    nr = {
        'AB': 0,
        'AC': 1,
        'BC': 2
    }[s]

    cnts[nr] += 1


for i in xrange(-cnts[0], cnts[0] + 1, 2):
    aa = a + i
    bb = b - i
    cc = c

    res = [i, -1, -1]

    delta = cnts[1]
    da = max(0, (delta - aa + 1) / 2)
    dc = delta - da
    # print da, dc, cnts[1]
    if abs(da) > cnts[1] or abs(dc) > cnts[1]:
        continue
    res[1] = da - dc
    assert da + dc == delta
    aa = aa + da - dc
    cc = cc + dc - da

    delta = cnts[2]
    db = max(0, (delta - bb + 1) / 2)
    dc = delta - db
    if abs(db) > cnts[2] or abs(dc) > cnts[2]:
        continue
    assert db + dc == delta
    bb = bb + db - dc
    cc = cc + dc - db
    res[2] = db - dc

    if aa >= 0 and bb >= 0 and cc >= 0:
        print_res(cnts, res)
        break
else:
    print 'No'

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
ss = [input()for i in range(n)]
ok = True
ans = []
cnt = [[0]*3 for i in range(n)]
for i in range(n-1,-1,-1):
    
    s = ss[i]
    p = s[0]
    q = s[1]
    cnt[i][ord(p) - ord('A')] +=1
    cnt[i][ord(q) - ord('A')] +=1
    if i == 0:continue
    cnt[i-1][0] = cnt[i][0]
    cnt[i-1][1] = cnt[i][1]
    cnt[i-1][2] = cnt[i][2]

    
for i in range(n):
    s = ss[i]
    if s == 'AB':
        if a == 0 and b == 0:
            ok = False
            break
        
        if a == 0:
            ans.append('A')
            a += 1
            b -= 1
        elif b == 0:
            ans.append('B')
            b += 1
            a -= 1
        elif a < b:
            ans.append('A')
            a += 1
            b -= 1
        elif a > b:
            ans.append('B')
            b += 1
            a -= 1
            
        else:
            if cnt[i][0] > cnt[i][1]:
                a += 1
                b -= 1
                ans.append('A')
            else:
                ans.append('B')
                a -= 1
                b += 1
    elif s == 'AC':
        if a == 0 and c == 0:
            ok = False
            break
        if a == 0:
            ans.append('A')
            a += 1
            c -= 1
        elif c == 0:
            ans.append('C')
            c += 1
            a -= 1
        elif a > c:
            ans.append('C')
            c += 1
            a -= 1
        elif a < c:
            ans.append('A')
            c -= 1
            a += 1
        else:
            if cnt[i][0] > cnt[i][2]:
                a += 1
                c -= 1
                ans.append('A')
            else:
                ans.append('C')
                a -= 1
                c += 1
    else:
        if c == 0 and b == 0:
            ok = False
            break
        
        if b == 0:
            ans.append('B')
            c -= 1
            b += 1
        elif c == 0:
            ans.append('C')
            c += 1
            b -= 1
        elif b > c:
            ans.append('C')
            c += 1
            b -= 1
        elif b < c:
            ans.append('B')
            b += 1
            c -= 1
        else:
            if cnt[i][2] > cnt[i][1]:
                c += 1
                b -= 1
                ans.append('C')
            else:
                ans.append('B')
                c -= 1
                b += 1
if ok:
    print('Yes')
    for x in ans:
        print(x)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from itertools import product
from copy import deepcopy
N, Ax, Bx, Cx = map(int, input().split())
S = [input() for _ in range(N)]

def calc(ab, ac, bc):
    A, B, C = deepcopy(Ax), deepcopy(Bx), deepcopy(Cx)
    ans = []
    for s in S:
        if s == 'AB':
            if A - ab > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')

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

        if s == 'BC':
            if B - bc > 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:
            return
    return ans


for pattern in product([0, 1], repeat=3):
    pattern = list(pattern)
    ab, ac, bc = pattern

    ans = calc(ab, ac, bc)
    if ans:
        print('Yes')
        print(*ans, sep="\n")
        exit()

print('No')

Submission status: Wrong Answer

Solution code:
import sys, copy, math, heapq, bisect
from itertools import accumulate
from collections import deque, defaultdict, Counter
sys.setrecursionlimit(500000)


N,A,B,C = map(int,input().split())
ABC = [A,B,C]
S = []
SS = ('A','B','C')
d = [0,0,0]
flag = True
for i in range(N):
    s = input()
    if s=="AB":
        S.append(2)
        d[2] += 1
    elif s=="BC":
        S.append(0)
        d[0] += 1
    else:
        S.append(1)
        d[1] += 1
ans = []
for i in range(N):
    x,y = (S[i]+1)%3,(S[i]+2)%3
    if ABC[x]<ABC[y]:
        ans.append(SS[x])
        ABC[y] -= 1
        ABC[x] += 1
    elif ABC[x]>ABC[y]:
        ans.append(SS[y])
        ABC[y] += 1
        ABC[x] -= 1
    else:
        if d[x]<d[y]:
            ans.append(SS[y])
            ABC[y] += 1
            ABC[x] -= 1
        else:
            ans.append(SS[x])
            ABC[y] -= 1
            ABC[x] += 1
    d[S[i]] -= 1
    if ABC[0]<0 or ABC[1]<0 or ABC[2]<0:
        flag = False
        break


if flag:
    print('Yes')
    print(*ans,sep='\n')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import math


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

    # dp_abc = [None for _ in range(N+1)]
    # dp_history = [[] for _ in range(N+1)]
    #
    # dp_abc[0] = (A, B, C)

    history = []

    cnt = {
        'A': A,
        'B': B,
        'C': C,
    }

    for i in range(N):
        x, y = s[i][0], s[i][1]
        if cnt[x] > cnt[y]:
            cnt[x] -= 1
            cnt[y] += 1
            history.append(y)
        else:
            cnt[x] += 1
            cnt[y] -= 1
            history.append(x)

        if cnt[x] < 0 or cnt[y] < 0:
            print('No')
            return

    print('Yes')
    for c in history:
        print(c)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, *x = map(int, input().split())
k = 'ABC'
ans = []
for i in range(n):
    s = input()
    if s == 'AB':
        i = 0
        j = 1
    elif s == 'BC':
        i = 1
        j = 2
    else:
        i = 0
        j = 2
    if x[i] == x[j] == 0:
        print('No')
        break
    elif x[i] < x[j]:
        x[i] += 1
        x[j] -= 1
        ans.append(k[i])
    else:
        x[i] -= 1
        x[j] += 1
        ans.append(k[j])
else:
    print('Yes')
    print(*ans, sep='\n')

Submission status: Wrong Answer

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

N, A, B, C = map(int, input().split())
Sss = [input().rstrip() for _ in range(N)]

convs = dict(zip(['AB','AC','BC'], [0, 1, 2]))
chs = 'ABC'
Xss = [[0,1], [0,2], [1,2]]

for S in range(8):
    Xss = [[0,1], [0,2], [1,2]]
    for i in range(3):
        if (S>>i)&1:
            Xss[i] = Xss[i][::-1]
#    print(bin(S)[2:].zfill(3), Xss)

    anss = []
    Ks = list([A, B, C])
    for Ss in Sss:
        no = convs[Ss]
        x, y = Xss[no]
        anss.append(chs[x])
        Ks[x] += 1
        Ks[y] -= 1
        if Ks[x] < 0 or Ks[y] < 0:
            break
    else:
        print('Yes')
        print('\n'.join(anss))
        break
else:
    print('No')

Submission status: Wrong Answer

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

Ans=[]
for j in range(N):
    s=S[j]
    if s=='AB':
        if A>=B:
            A-=1
            B+=1
            if A<0:
                print('No')
                break
            Ans.append('B')
        else:
            A+=1
            B-=1
            if B<0:
                print('No')
                break
            Ans.append('A')
    elif s=='BC':
        if B>=C:
            B-=1
            C+=1
            if B<0:
                print('No')
                break
            Ans.append('C')
        else:
            B+=1
            C-=1
            if C<0:
                print('No')
                break
            Ans.append('B')
    else:
        if C>=A:
            C-=1
            A+=1
            if C<0:
                print('No')
                break
            Ans.append('A')
        else:
            C+=1
            A-=1
            if A<0:
                print('No')
                break
            Ans.append('C')
else:
    print('Yes')
    for k in range(N):
        print(Ans[k])

Submission status: Wrong Answer

Solution code:
import sys

N,A,B,C = map(int,input().split())
s = [0]*N

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

ans = [0]*N
for i in range(N):
    if s[i] == "AB":
        if A == 0 and B == 0:
            print("No")
            sys.exit()
        elif A == 0:
            ans[i] = "A"
            A += 1
            B -= 1
        elif B == 0:
            ans[i] = "B"
            B += 1
            A -= 1
        elif i < N-1 and ans[i+1] == "BC":
            ans[i] = "B"
            B += 1
            A -= 1
        elif i < N-1 and ans[i+1] == "AC":
            ans[i] = "A"
            A += 1
            B -= 1
        else:
            ans[i] = "A"
            A += 1
            B -= 1
    elif s[i] == "BC":
        if B == 0 and C == 0:
            print("No")
            sys.exit()
        elif B == 0:
            ans[i] = "B"
            B += 1
            C -= 1
        elif C == 0:
            ans[i] = "C"
            C += 1
            B -= 1
        elif i < N-1 and ans[i+1] == "AB":
            ans[i] = "B"
            B += 1
            C -= 1
        elif i < N-1 and ans[i+1] == "AC":
            ans[i] = "C"
            C += 1
            B -= 1
        else:
            ans[i] = "B"
            B += 1
            C -= 1
    elif s[i] == "AC":
        if A == 0 and C == 0:
            print("No")
            sys.exit()
        elif C == 0:
            ans[i] = "C"
            C += 1
            A -= 1
        elif A == 0:
            ans[i] = "A"
            A += 1
            C -= 1
        elif i < N-1 and ans[i+1] == "AB":
            ans[i] = "A"
            A += 1
            C -= 1
        elif i < N-1 and ans[i+1] == "BC":
            ans[i] = "C"
            C += 1
            A -= 1
        else:
            ans[i] = "C"
            C += 1
            A -= 1

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

Submission status: Wrong Answer

Solution code:
def execute(idx, order, A, B, C, ans):
    lft, rgt = order[idx]
    if eval(lft) > eval(rgt): lft, rgt = rgt, lft
    if eval(lft) == eval(rgt) and rgt in order[idx+1]:
        lft, rgt = rgt, lft

    if eval(lft) == 0 and eval(rgt) == 0:
        print("No")
        return -1
    else:
        exec(lft + "+=1")
        exec(rgt + "-=1")
        ans[idx] = lft
    return 1

def main():
    n,A,B,C = map(int,input().split())
    order = [input() for i in range(n)]
    ans = [' ']*n
    order.append("banpei")
    for idx in range(n):
        if execute(idx, order, A, B, C, ans) < 0: return
    print("Yes")
    for c in ans: print(c)

main()