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())
    s_list = [input() for _ in [0]*n]

    if a == b == c == 0:
        print("No")
    elif a+b+c == 1:
        if a == 1:
            color = "A"
        elif b == 1:
            color = "B"
        else:
            color = "C"
        l = []
        for i in s_list:
            if color not in i:
                print("No")
                return
            j = list(i)
            j.remove(color)
            color = j[0]
            l.append(color)
        print("Yes")
        for i in l:
            print(i)
    else:
        s_list += ["ABC"]
        l = []
        for j, i in enumerate(s_list[:-1]):
            if i == "AB":
                if a >= 2:
                    l.append("B")
                    a -= 1
                    b += 1
                elif b >= 2:
                    l.append("A")
                    a += 1
                    b -= 1
                elif a == b == 0:
                    print("No")
                    return
                elif a == 1 and b == 0:
                    l.append("B")
                    a -= 1
                    b += 1
                elif a == 0 and b == 1:
                    l.append("A")
                    a += 1
                    b -= 1
                else:
                    if "A" in s_list[j+1]:
                        l.append("A")
                        a += 1
                        b -= 1
                    else:
                        l.append("B")
                        a -= 1
                        b += 1
            elif i == "BC":
                if b >= 2:
                    l.append("C")
                    b -= 1
                    c += 1
                elif c >= 2:
                    l.append("B")
                    b += 1
                    c -= 1
                elif b == c == 0:
                    print("No")
                    return
                elif b == 1 and c == 0:
                    l.append("C")
                    b -= 1
                    c += 1
                elif b == 0 and c == 1:
                    l.append("B")
                    b += 1
                    c -= 1
                else:
                    if "B" in s_list[j+1]:
                        l.append("B")
                        b += 1
                        c -= 1
                    else:
                        l.append("C")
                        b -= 1
                        c += 1
            else:
                if c >= 2:
                    l.append("A")
                    c -= 1
                    a += 1
                elif a >= 2:
                    l.append("C")
                    c += 1
                    a -= 1
                elif c == a == 0:
                    print("No")
                    return
                elif c == 1 and a == 0:
                    l.append("A")
                    c -= 1
                    a += 1
                elif b == 0 and c == 1:
                    l.append("C")
                    c += 1
                    a -= 1
                else:
                    if "C" in s_list[j+1]:
                        l.append("C")
                        c += 1
                        a -= 1
                    else:
                        l.append("A")
                        c -= 1
                        a += 1
        print("Yes")
        for i in l:
            print(i)


main()

Submission status: Wrong Answer

Solution code:
def no():
    print("No")
    exit()

def op(u, v):
    x[u] += 1
    x[v] -= 1

n, a, b, c = map(int, input().split())
x = [a, b, c]
y = ["A", "B", "C"]
ans = []
s = [input() for _ in range(n)]
t = [0] * n
for i in range(n):
    if s[i] == "BC":
        t[i] = 1
    elif s[i] == "AC":
        t[i] = 2
for i in range(n):
    j, k, l = t[i], (t[i] + 1) % 3, (t[i] + 2) % 3
    if x[j] == x[k] == 1 and not i == n - 1:
        if (not x[l] == 0) or (s[i + 1] == j):
            op(j, k)
            ans.append(y[j])
        else:
            if s[i + 1] == l:
                op(j, k)
                ans.append(y[j])
            else:
                op(k, j)
                ans.append(y[k])
    elif x[j] + x[k] > 0:
        if x[j] <= x[k]:
            op(j, k)
            ans.append(y[j])
        else:
            op(k, j)
            ans.append(y[k])
    else:
        no()
print("Yes")
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

def dfs(i, d, ans):
    if d[0]<0 or d[1]<0 or d[2]<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    s, t = cv(S[i][0]), cv(S[i][1])
    d[s] += 1
    d[t] -= 1
    print(i, D[s], D[t])
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    d[s] -= 2
    d[t] += 2
    print(i, D[s], D[t])
    ans[i] = S[i][1]
    dfs(i+1, d, ans)
    d[s] += 1
    d[t] -= 1

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2
'''
def dfs(i, A, B, C, ans):
    if A<0 or B<0 or C<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    if S[i] == 'AB':
        ans[i] = 'A'
        dfs(i+1, A+1, B-1, C, ans)
        ans[i] = 'B'
        dfs(i+1, A-1, B+1, C, ans)
    if S[i] == 'BC':
        ans[i] = 'B'
        dfs(i+1, A, B+1, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A, B-1, C+1, ans)
    if S[i] == 'AC':
        ans[i] = 'A'
        dfs(i+1, A+1, B, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A-1, B, C+1, ans)
'''
N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N
dfs(0, D, ans)
print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

def dfs(i, A, B, C, ans):
    if D[0]<0 or D[1]<0 or D[2]<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    s, t = cv(S[i][0]), cv(S[i][1])
    '''
    D[s] += 1
    D[t] -= 1
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    D_[s] -= 1
    D_[t] += 1
    ans_[i] = S[i][1]
    dfs(i+1, D_, ans_)
    '''
    if S[i] == 'AB':
        ans[i] = 'A'
        dfs(i+1, A+1, B-1, C, ans)
        ans[i] = 'B'
        dfs(i+1, A-1, B+1, C, ans)
    if S[i] == 'BC':
        ans[i] = 'B'
        dfs(i+1, A, B+1, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A, B-1, C+1, ans)
    if S[i] == 'AC':
        ans[i] = 'A'
        dfs(i+1, A+1, B, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A-1, B, C+1, ans)

dfs(0, D[0], D[1], D[2], ans)
print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

def dfs(i, A, B, C, ans):
    if D[0]<0 or D[1]<0 or D[2]<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    s, t = cv(S[i][0]), cv(S[i][1])
    '''
    D[s] += 1
    D[t] -= 1
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    D_[s] -= 1
    D_[t] += 1
    ans_[i] = S[i][1]
    dfs(i+1, D_, ans_)
    '''
    if S[i] == 'AB':
        ans[i] = 'A'
        dfs(i+1, A+1, B-1, C, ans)
        ans[i] = 'B'
        dfs(i+1, A-1, B+1, C, ans)
    if S[i] == 'BC':
        ans[i] = 'B'
        dfs(i+1, A, B+1, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A, B-1, C+1, ans)
    if S[i] == 'AC':
        ans[i] = 'A'
        dfs(i+1, A+1, B+1, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A-1, B-1, C+1, ans)

dfs(0, D[0], D[1], D[2], ans)
print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import copy
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

def dfs(i, D, ans):
    print(i, D, ans)
    if D[0]<0 or D[1]<0 or D[2]<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    s, t = cv(S[i][0]), cv(S[i][1])
    D_ = copy.deepcopy(D)
    ans_ = copy.deepcopy(ans)
    D[s] += 1
    D[t] -= 1
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    D_[s] -= 1
    D_[t] += 1
    ans_[i] = S[i][1]
    dfs(i+1, D_, ans_)

dfs(0, D, ans)
print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import math
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

def dfs(i, D, ans):
    flag = 1
    if D[0]<0 or D[1]<0 or D[2]<0:
        flag = 0
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    if flag:
        s, t = cv(S[i][0]), cv(S[i][1])
        D[s] += 1
        D[t] -= 1
        ans[i] = S[i][0]
        dfs(i+1, D, ans)
        D[s] -= 2
        D[t] += 2
        ans[i] = S[i][1]
        dfs(i+1, D, ans)

dfs(0, D, ans)
print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import math
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

def dfs(i, D, ans):
    if D[0]<0 or D[1]<0 or D[2]<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    s, t = cv(S[i][0]), cv(S[i][1])
    D[s] += 1
    D[t] -= 1
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    D[s] -= 2
    D[t] += 2
    ans[i] = S[i][1]
    dfs(i+1, D, ans)

dfs(0, D, ans)
print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import math
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

def invcv(m):
    if m == 0:
        return 'A'
    if m == 1:
        return 'B'
    if m == 2:
        return 'C'

def dfs(i, D, ans):
    if D[0]<0 or D[1]<0 or D[2]<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    s, t = cv(S[i][0]), cv(S[i][1])
    D[s] += 1
    D[t] -= 1
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    D[s] -= 2
    D[t] += 2
    ans[i] = S[i][1]
    dfs(i+1, D, ans)

dfs(0, D, ans)
print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import math
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

def dfs(i, D, l):
    if D[0]<0 or D[1]<0 or D[2]<0:
        return
    if i == N:
        print('Yes')
        for m in l:
            print(m)
        exit()
    s, t = cv(S[i][0]), cv(S[i][1])
    D[s] += 1
    D[t] -= 1
    dfs(i+1, D, l+[S[i][0]])
    D[s] -= 2
    D[t] += 2
    dfs(i+1, D, l+[S[i][1]])

dfs(0, D, [])
print('No')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import math
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

ans = []
sum_d = sum(D)
for i in range(N):
    s, t = cv(S[i][0]), cv(S[i][1])
    if D[s] == D[t] == 0:
            print('No')
            exit()
    if sum_d > 2:
        if D[s] >= D[t]:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        else:
            D[s] += 1
            D[t] -= 1
            ans.append(S[i][0])
    elif sum_d == 2:
        if D[s] == 2:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        elif D[t] == 2:
            D[t] -= 1
            D[s] += 1
            ans.append(S[i][0])
        elif D[s] == D[t] == 1:
            if i+1 < N:
                if S[i] == S[i+1]:
                    D[s] -= 1
                    D[t] += 1
                    ans.append(S[i][1])
                elif cv(S[i][0]) == s or cv(S[i][1]) == s:
                    D[s] += 1
                    D[t] -= 1
                    ans.append(S[i][0])
                else:
                    D[t] += 1
                    D[s] -= 1
                    ans.append(S[i][1])
            else:
                ans.append(S[i][0])
        else:
            D[s] = 1 - D[s]
            D[t] = 1 - D[t]
            if D[s] == 1:
                ans.append(S[i][0])
            else:
                ans.append(S[i][1])
    else:
        D[s] = 1 - D[s]
        D[t] = 1 - D[t]
        if D[s] == 1:
            ans.append(S[i][0])
        else:
            ans.append(S[i][1])

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import math
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

ans = []
sum_d = sum(D)
for i in range(N):
    s, t = cv(S[i][0]), cv(S[i][1])
    if D[s] == D[t] == 0:
            print('No')
            exit()
    if sum_d > 2:
        if D[s] >= D[t]:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        else:
            D[s] += 1
            D[t] -= 1
            ans.append(S[i][0])
    elif sum_d == 2:
        if D[s] == 2:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        elif D[t] == 2:
            D[t] -= 1
            D[s] += 1
            ans.append(S[i][0])
        elif D[s] == D[t] == 1:
            if i+1 < N:
                if S[i] == S[i+1]:
                    D[s] -= 1
                    D[t] += 1
                    ans.append(S[i][1])
                if cv(S[i][0]) == s or cv(S[i][1]) == s:
                    D[s] += 1
                    D[t] -= 1
                    ans.append(S[i][0])
                else:
                    D[t] += 1
                    D[s] -= 1
                    ans.append(S[i][1])
            else:
                ans.append(S[i][0])
        else:
            D[s] = 1 - D[s]
            D[t] = 1 - D[t]
            if D[s] == 1:
                ans.append(S[i][0])
            else:
                ans.append(S[i][1])
    else:
        D[s] = 1 - D[s]
        D[t] = 1 - D[t]
        if D[s] == 1:
            ans.append(S[i][0])
        else:
            ans.append(S[i][1])

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import math
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

ans = []
sum_d = sum(D)
for i in range(N):
    s, t = cv(S[i][0]), cv(S[i][1])
    if D[s] == D[t] == 0:
            print('No')
            exit()
    if sum_d > 2:
        if D[s] >= D[t]:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        else:
            D[s] += 1
            D[t] -= 1
            ans.append(S[i][0])
    elif sum_d == 2:
        if D[s] == 2:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        elif D[t] == 2:
            D[t] -= 1
            D[s] += 1
            ans.append(S[i][0])
        elif D[s] == D[t] == 1:
            if i+1 < N:
                if S[i] == S[i+1]:
                    D[s] -= 1
                    D[t] += 1
                    ans.append(S[i][1])
                if cv(S[i][0]) == s or cv(S[i][1]) == s:
                    D[s] += 1
                    ans.append(S[i][0])
                else:
                    D[t] += 1
                    ans.append(S[i][1])
            else:
                ans.append(S[i][0])
        else:
            D[s] = 1 - D[s]
            D[t] = 1 - D[t]
            if D[s] == 1:
                ans.append(S[i][0])
            else:
                ans.append(S[i][1])
    else:
        D[s] = 1 - D[s]
        D[t] = 1 - D[t]
        if D[s] == 1:
            ans.append(S[i][0])
        else:
            ans.append(S[i][1])

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import math
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

ans = []
sum_d = sum(D)
for i in range(N):
    s, t = cv(S[i][0]), cv(S[i][1])
    if D[s] == D[t] == 0:
            print('No')
            exit()
    if sum_d > 2:
        if D[s] >= D[t]:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        else:
            D[s] += 1
            D[t] -= 1
            ans.append(S[i][0])
    elif sum_d == 2:
        if D[s] == 2:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        elif D[t] == 2:
            D[t] -= 1
            D[s] += 1
            ans.append(S[i][0])
        elif D[s] == D[t] == 1:
            if i+1 < N:
                if S[i] == S[i+1]:
                    D[s] -= 1
                    ans.append(S[i][0])
                if cv(S[i][0]) == s or cv(S[i][1]) == s:
                    D[s] += 1
                    ans.append(S[i][0])
                else:
                    D[t] += 1
                    ans.append(S[i][1])
            else:
                ans.append(S[i][0])
        else:
            D[s] = 1 - D[s]
            D[t] = 1 - D[t]
            if D[s] == 1:
                ans.append(S[i][0])
            else:
                ans.append(S[i][1])
    else:
        D[s] = 1 - D[s]
        D[t] = 1 - D[t]
        if D[s] == 1:
            ans.append(S[i][0])
        else:
            ans.append(S[i][1])

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
import math
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2


yoso = ['A', 'B', 'C']
ans = []
for i in range(N):
    s, t = cv(S[i][0]), cv(S[i][1])
    if D[s] == D[t] == 0:
            print('No')
            exit()
    if sum(D) > 2:
        if D[s] >= D[t]:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        else:
            D[s] += 1
            D[t] -= 1
            ans.append(S[i][0])
    elif sum(D) == 2:
        if D[s] == 2:
            D[s] -= 1
            D[t] += 1
            ans.append(S[i][1])
        elif D[t] == 2:
            D[t] -= 1
            D[s] += 1
            ans.append(S[i][0])
        elif D[s] == D[t] == 1:
            if i+1 < N:
                if S[i] == S[i+1]:
                    D[s] = 1 - D[s]
                    D[t] = 1 - D[t]
                    if D[s] == 1:
                        ans.append(S[i][0])
                    else:
                        ans.append(S[i][1])
                if cv(S[i][0]) == s or cv(S[i][1]) == s:
                    D[s] += 1
                    ans.append(S[i][0])
                else:
                    D[t] += 1
                    ans.append(S[i][1])
            else:
                ans.append(S[i][0])
        else:
            D[s] = 1 - D[s]
            D[t] = 1 - D[t]
            if D[s] == 1:
                ans.append(S[i][0])
            else:
                ans.append(S[i][1])
    else:
        D[s] = 1 - D[s]
        D[t] = 1 - D[t]
        if D[s] == 1:
            ans.append(S[i][0])
        else:
            ans.append(S[i][1])

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