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:
import sys


def input():
    return sys.stdin.readline().strip()


def solve():
    def distribute(fm, to):
        count[fm] -= 1
        count[to] += 1
        ans.append(to)

    n, a, b, c = map(int, input().split())
    queries = [input() for _ in range(n)]

    count = {'A': a, 'B': b, 'C': c}
    ans = ['Yes']

    for i in range(n):
        x, y = queries[i]
        if count[x] == count[y] == 0:
            return 'No'
        elif count[x] == count[y] == 1:
            if i == n - 1:
                distribute(x, y)
            else:
                if x in queries[i + 1]:
                    distribute(y, x)
                else:
                    distribute(x, y)
        elif count[y] > count[x]:
            distribute(y, x)
        else:
            distribute(x, y)
    return '\n'.join(ans)


print(solve())

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

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

ans = []

d = {"A": a, "B": b, "C": c}


def dfs(i):
    if min(d.values()) < 0:
        return

    if i == n:
        print("Yes")
        print(*ans, sep="\n")
        exit()

    for j in range(2):
        add = s[i][j]
        sub = s[i][1-j]
        d[add] += 1
        d[sub] -= 1
        ans.append(add)
        dfs(i + 1)
        d[add] -= 1
        d[sub] += 1
        ans.pop()


dfs(0)

print("No")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

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

ans = []

d = {"A": a, "B": b, "C": c}


def dfs(i):
    if min(d.values()) < 0:
        return

    if i == n:
        print("Yes")
        print(*ans, sep="\n")
        exit()

    k1, k2 = s[i]

    d[k1] += 1
    d[k2] -= 1
    ans.append(k1)
    dfs(i+1)
    d[k1] -= 1
    d[k2] += 1
    ans.pop()

    d[k2] += 1
    d[k1] -= 1
    ans.append(k2)
    dfs(i+1)
    d[k2] -= 1
    d[k1] += 1
    ans.pop()


dfs(0)

print("No")

Submission status: Accepted

Solution code:
import sys
import queue

q = queue.Queue()
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7
S = []
history = []
N, A, B ,C = map(int,input().split())
d = {}
d['A'] = A
d['B'] = B
d['C'] = C

for n in range(N):
    s = input()
    S.append(s)

def select(L,R,x):
    diff = 1
    if L!=x:
        diff *= -1
    d[L] += diff
    d[R] -= diff
    history.append(x)

for n in range(N):
    L, R = S[n]
    if d[L] >= 2:
        select(L,R,R)
    elif d[L] == 0:
        if d[R] == 0:
            print ('No')
            exit()
        else:
            select(L,R,L)
    elif d[L] == 1:
        if d[R] >= 2:
            select(L,R,L)
        elif d[R] == 0:
            select(L,R,R)
        elif d[R] == 1:
            if n!=N-1:
                if L in S[n+1]:
                    select(L,R,L)
                else:
                    select(L,R,R)
            else:
                select(L,R,L) # どちらでもOK
print ('Yes')
for h in history:
    print (h)

Submission status: Accepted

Solution code:
def add_i1():
	abc[i1]+=1
	abc[i2]-=1
	res[i]=i1
	return

def add_i2():
	abc[i1]-=1
	abc[i2]+=1
	res[i]=i2

n,*abc=map(int,input().split())
ip=[0]*n

for i in range(n):
	ip[i]=list(map(lambda x:ord(x)-ord("A"),list(input())))

ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			add_i1()
		else:
			add_i2()
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]==0:
				add_i1()
			elif abc[i2]==0:
				add_i2()
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					add_i1()
				else:
					add_i2()
			else:
				add_i1()

dic={0:"A",1:"B",2:"C"}
print(ans)
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Submission status: Accepted

Solution code:
def add_i1():
	abc[i1]+=1
	abc[i2]-=1
	res[i]=i1
	return

def add_i2():
	abc[i1]-=1
	abc[i2]+=1
	res[i]=i2

n,*abc=map(int,input().split())
ip=[0]*n
dic={0:"A",1:"B",2:"C"}
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			add_i1()
		else:
			add_i2()
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]==0:
				add_i1()
			elif abc[i2]==0:
				add_i2()
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					add_i1()
				else:
					add_i2()
			else:
				add_i1()
print(ans)
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Submission status: Accepted

Solution code:
def add_i1():
	abc[i1]+=1
	abc[i2]-=1
	res[i]=i1
	return

n,*abc=map(int,input().split())
ip=[0]*n
dic={0:"A",1:"B",2:"C"}
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			add_i1()
			'''
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
			'''
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			elif abc[i2]==0:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Submission status: Accepted

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
dic={0:"A",1:"B",2:"C"}
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			elif abc[i2]==0:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Submission status: Accepted

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
dic={0:"A",1:"B",2:"C"}
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])
		'''
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")
		'''

Submission status: Accepted

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
dic={0:"A",1:"B",2:"C"}
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])
		'''
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")
		'''

Submission status: Accepted

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		if abc[i1]+abc[i2]==0:
			ans="No"
		elif abc[i1]+abc[i2]==1:
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			else:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
dic={0:"A",1:"B",2:"C"}
if ans=="Yes":
	for i in range(n):
		'''
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")
		'''
		print(dic[res[i]])

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
abc = list((a, b, c))
ans = [0] * n
dic = {'A': 0, 'B': 1, 'C': 2}
flag = True
s = [input() for i in range(n)]
for i in range(n):
    if abc[dic[s[i][0]]] == 0 and abc[dic[s[i][1]]] == 0:
        flag = False
        break
    elif sum(abc) == 2 and abc[dic[s[i][0]]] == 1 and abc[dic[s[i][1]]] == 1 and i != n - 1:
        ps = (set(s[i]) & set(s[i + 1])).pop()
        ms = (set(s[i]) - set(ps)).pop()
        abc[dic[ps]] += 1
        abc[dic[ms]] -= 1
        ans[i] = ps
    elif abc[dic[s[i][0]]] <= abc[dic[s[i][1]]]:
        abc[dic[s[i][0]]] += 1
        abc[dic[s[i][1]]] -= 1
        ans[i] = s[i][0]
    else:
        abc[dic[s[i][0]]] -= 1
        abc[dic[s[i][1]]] += 1
        ans[i] = s[i][1]
if flag:
    print('Yes')
    print(*ans, sep='\n')
else:
    print('No')

Submission status: Accepted

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

n, a, b, c = [int(x) for x in input().split()]
score = {}
score["A"] = a
score["B"] = b
score["C"] = c     
S = [input().rstrip() for _ in range(n)]
ans = []

for i in range(n - 1):
    s = S[i]
    next_s = S[i + 1]
    s1 = s[0]
    s2 = s[1]
    if score[s1] == 0 and score[s2] == 0:
        print("No")
        sys.exit()
    elif score[s1] == 0:
        ans.append(s1)
        score[s1] += 1
        score[s2] -= 1
    elif score[s2] == 0:
        ans.append(s2)
        score[s1] -= 1
        score[s2] += 1
    else:
        if next_s == s:
            ans.append(s1)
            score[s1] += 1
            score[s2] -= 1
        elif s1 in next_s:
            ans.append(s1)
            score[s1] += 1
            score[s2] -= 1
        else:
            ans.append(s2)
            score[s2] += 1
            score[s1] -= 1
s = S[-1]
s1 = s[0]
s2 = s[1]

if score[s1] == 0 and score[s2] == 0:
        print("No")
        sys.exit()
elif score[s1] == 0:
    ans.append(s1)
    score[s1] += 1
    score[s2] -= 1
elif score[s2] == 0:
    ans.append(s2)
    score[s1] -= 1
    score[s2] += 1
else:
    ans.append(s1)
print("Yes")
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    n,A,B,C = map(int, readline().split())
    N = A + B + C
    S = [readline().strip() for _ in range(n)]

    ans = []
    possible = True
    for i, s in enumerate(S):
        if s == "AB":
            if A + B == 0:
                print("No")
                possible = False
                break
            
            if i < n - 1 and N == 2 and A == B == 1:
                if S[i+1] == "AC":
                    A += 1
                    B -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    B += 1
                    ans.append("B")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            else:
                if A < B:
                    A += 1
                    B -= 1
                    ans.append("A")
                else:
                    A -= 1
                    B += 1
                    ans.append("B")

        elif s == "AC":
            if A + C == 0:
                print("No")
                possible = False
                break
            
            if i < n - 1 and N == 2 and A == C == 1:
                if S[i+1] == "AB":
                    A += 1
                    C -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    C += 1
                    ans.append("C")
                else:
                    A += 1
                    C -= 1
                    ans.append("A")
            else:
                if A < C:
                    A += 1
                    C -= 1
                    ans.append("A")
                else:
                    A -= 1
                    C += 1
                    ans.append("C")

        else:
            if B + C == 0:
                print("No")
                possible = False
                break
            
            if i < n - 1 and N == 2 and B == C == 1:
                if S[i+1] == "AB":
                    B += 1
                    C -= 1
                    ans.append("B")
                elif S[i+1] == "AC":
                    B -= 1
                    C += 1
                    ans.append("C")
                else:
                    B += 1
                    C -= 1
                    ans.append("B")
            else:
                if B < C:
                    B += 1
                    C -= 1
                    ans.append("B")
                else:
                    B -= 1
                    C += 1
                    ans.append("C")

    if possible:
        print("Yes")
        print("\n".join(ans))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		if abc[i1]+abc[i2]==0:
			ans="No"
		elif abc[i1]+abc[i2]==1:
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			else:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
if ans=="Yes":
	for i in range(n):
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n, a, b, c = map(int, input().split())

value_dict = {'A': a, 'B': b, 'C': c}
result_list = []
input_list = []
flag = True

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

for next, input in enumerate(input_list, 1):
    if value_dict[input[0]] < value_dict[input[1]]:
        value_dict[input[0]] += 1
        value_dict[input[1]] -= 1
        result_list.append(input[0])
    elif value_dict[input[1]] < value_dict[input[0]]:
        value_dict[input[1]] += 1
        value_dict[input[0]] -= 1
        result_list.append(input[1])
    else:
        if value_dict[input[0]] == 0:
            flag = False
            break
        elif next == len(input_list):
            result_list.append(input[0])
        elif input[0] in input_list[next]:
            value_dict[input[0]] += 1
            value_dict[input[1]] -= 1
            result_list.append(input[0])
        else:
            value_dict[input[1]] += 1
            value_dict[input[0]] -= 1
            result_list.append(input[1])

if flag:
    print('Yes')
    for result in result_list:
        print(result)

else:
    print('No')

Submission status: Accepted

Solution code:
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N, A, B, C = [int(x) for x in input().split()]
    S = [input().strip() for _ in range(N)]
    c = collections.Counter(S)


    ans = []
    for i in range(N):
        if S[i] == "AB":
            if A == B == 0:
                print("No")
                return
            elif A == B and i != N - 1:
                if S[i + 1] == "BC":
                    A -= 1
                    B += 1
                    ans.append("B")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            elif A > B:
                A -= 1
                B += 1
                ans.append("B")
            else:
                A += 1
                B -= 1
                ans.append("A")
        elif S[i] == "AC":
            if A == C == 0:
                print("No")
                return
            elif A == C and i != N - 1:
                if S[i + 1] == "BC":
                    A -= 1
                    C += 1
                    ans.append("C")
                else:
                    A += 1
                    C -= 1
                    ans.append("A")
            elif A > C:
                A -= 1
                C += 1
                ans.append("C")
            else:
                A += 1
                C -= 1
                ans.append("A")
        else:
            if B == C == 0:
                print("No")
                return
            elif B == C and i != N - 1:
                if S[i + 1] == "AC":
                    B -= 1
                    C += 1
                    ans.append("C")
                else:
                    B += 1
                    C -= 1
                    ans.append("B")
            elif B > C:
                B -= 1
                C += 1
                ans.append("C")
            else:
                B += 1
                C -= 1
                ans.append("B")

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

buff_readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

def read_int_n():
    return list(map(int, buff_readline().split()))


def read_str():
    return readline().strip()


def slv(N, A, B, C, S):
    D = [A, B, C]
    ans = []

    def f(s):
        t = []
        if 'A' in s:
            t.append(0)
        if 'B' in s:
            t.append(1)
        if 'C' in s:
            t.append(2)
        return t
    for i, s in enumerate(S):
        t = f(s)
        if D[t[0]] == 0 and D[t[1]] == 0:
            print('No')
            return
        else:
            if D[t[0]] == D[t[1]] == 1 and i != N-1:
                ns = S[i+1]
                if s == ns:
                    ans.append(t[1])
                    D[t[0]] -= 1
                    D[t[1]] += 1
                else:
                    n = set(t) & set(f(ns))
                    m = set(t) - n
                    n = n.pop()
                    m = m.pop()
                    ans.append(n)
                    D[n] += 1
                    D[m] -= 1


            elif D[t[0]] > D[t[1]]:
                ans.append(t[1])
                D[t[0]] -= 1
                D[t[1]] += 1
            else:
                ans.append(t[0])
                D[t[0]] += 1
                D[t[1]] -= 1

    print('Yes')
    for a in ans:
        if a == 0:
            print('A')
        elif a == 1:
            print('B')
        else:
            print('C')



def main():
    N, A, B, C = read_int_n()
    S = [read_str() for _ in range(N)]
    (slv(N, A, B, C, S))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

n,A,B,C = map(int,input().split())
s = [[*input()] for _ in range(n)]
a = {"A":A, "B":B, "C":C}
ans = ["Yes"]+[0]*n
def dfs(i):
    if i==n:
        print("\n".join(ans))
        exit()
    for p,m in [s[i],s[i][::-1]]:
        if a[m]:
            a[p]+=1; a[m]-=1; ans[i+1]=p
            dfs(i+1)
            #一つ戻る
            a[p]-=1; a[m]+=1
dfs(0)
print("No")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, a, b, c = MAP()
C = {0: a, 1: b, 2: c}
S = []
for i in range(N):
    s = input()
    c1, c2 = s
    S.append((ord(c1)-65, ord(c2)-65))

ans = []
for i in range(N-1):
    cur1, cur2 = S[i]
    if C[cur1] == C[cur2] == 0:
        No()
        exit()
    elif C[cur1] == C[cur2] == 1 and C[3-cur1-cur2] == 0:
        if cur1 in S[i+1] and 3-cur1-cur2 in S[i+1]:
            ans.append(cur1)
            C[cur1] += 1
            C[cur2] -= 1
        else:
            ans.append(cur2)
            C[cur2] += 1
            C[cur1] -= 1
    elif C[cur1] <= C[cur2]:
        ans.append(cur1)
        C[cur1] += 1
        C[cur2] -= 1
    else:
        ans.append(cur2)
        C[cur2] += 1
        C[cur1] -= 1
cur1, cur2 = S[-1]
if C[cur1] == C[cur2] == 0:
    No()
    exit()
elif C[cur1] <= C[cur2]:
    ans.append(cur1)
    C[cur1] += 1
    C[cur2] -= 1
else:
    ans.append(cur2)
    C[cur2] += 1
    C[cur1] -= 1
Yes()
for i in range(N):
    print(chr(ans[i]+65))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, a, b, c = MAP()
C = {0: a, 1: b, 2: c}
S = []
for i in range(N):
    s = input()
    c1, c2 = s
    S.append((ord(c1)-65, ord(c2)-65))

ans = []
for i in range(N-1):
    cur1, cur2 = S[i]
    if C[cur1] == C[cur2] == 0:
        No()
        exit()
    elif C[cur1] == 0:
        ans.append(cur1)
        C[cur1] += 1
        C[cur2] -= 1
    elif C[cur2] == 0:
        ans.append(cur2)
        C[cur2] += 1
        C[cur1] -= 1
    elif C[cur1] == C[cur2] == 1 and C[3-cur1-cur2] == 0:
        if cur1 in S[i+1] and 3-cur1-cur2 in S[i+1]:
            ans.append(cur1)
            C[cur1] += 1
            C[cur2] -= 1
        else:
            ans.append(cur2)
            C[cur2] += 1
            C[cur1] -= 1
    elif C[cur1] <= C[cur2]:
        ans.append(cur1)
        C[cur1] += 1
        C[cur2] -= 1
    else:
        ans.append(cur2)
        C[cur2] += 1
        C[cur1] -= 1
cur1, cur2 = S[-1]
if C[cur1] == C[cur2] == 0:
    No()
    exit()
elif C[cur1] == 0:
    ans.append(cur1)
    C[cur1] += 1
    C[cur2] -= 1
else:
    ans.append(cur2)
    C[cur2] += 1
    C[cur1] -= 1
Yes()
for i in range(N):
    print(chr(ans[i]+65))

Submission status: Accepted

Solution code:
import sys

n, a, b, c = map(int, sys.stdin.readline().split())
s = sys.stdin.read().split() + ['$$']

def main(a, b, c):
    res = []
    for i in range(n):
        x, y = s[i], s[i+1]
        if x == 'AB':
            if a == b == 0: break
            if a > b:
                a -= 1
                b += 1
                res.append('B')
            elif a < b:
                a += 1
                b -= 1
                res.append('A')
            else:
                if y == 'AC':
                    a += 1
                    b -= 1
                    res.append('A')
                else:
                    a -= 1
                    b += 1
                    res.append('B')
        elif x == 'AC':
            if a == c == 0: break
            if a > c:
                a -= 1
                c += 1
                res.append('C')
            elif a < c:
                a += 1
                c -= 1
                res.append('A')
            else:
                if y == 'AB':
                    a += 1
                    c -= 1
                    res.append('A')
                else:
                    a -= 1
                    c += 1
                    res.append('C')  
        elif x == 'BC':
            if b == c == 0: break
            if b > c:
                b -= 1
                c += 1
                res.append('C')
            elif b < c:
                b += 1
                c -= 1
                res.append('B')
            else:
                if y == 'AB':
                    b += 1
                    c -= 1
                    res.append('B')
                else:
                    b -= 1
                    c += 1
                    res.append('C')
    else:
        print('Yes')
        print(*res, sep='\n')
        return 
    print('No')
                
if __name__ == '__main__':
    main(a, b, c)

Submission status: Accepted

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

S = ['']*N
for i in range(N):
  S[i] = input()
  
play = [''] * N
flag = True
for i, s in enumerate(S):
  next = ''
  if i < len(S)-1:
    next = S[i+1]
  if s == 'AB':
    if A == 0 and B == 0:
      flag = False
      break
    elif A == B and 'B' in next:
      A -= 1
      B += 1
      play[i] = 'B'
    elif A > B:
      A -= 1
      B += 1
      play[i] = 'B'
    else:
      A += 1
      B -= 1
      play[i] = 'A'
  elif s == 'AC':
    if A == 0 and C == 0:
      flag = False
      break
    elif A == C and 'C' in next:
      A -= 1
      C += 1
      play[i] = 'C'
    elif A > C:
      A -= 1
      C += 1
      play[i] = 'C'
    else:
      A += 1
      C -= 1
      play[i] = 'A'
  else:
    if B == 0 and C == 0:
      flag = False
      break
    elif B == C and 'C' in next:
      B -= 1
      C += 1
      play[i] = 'C'
    elif B > C:
      B -= 1
      C += 1
      play[i] = 'C'
    else:
      B += 1
      C -= 1
      play[i] = 'B'

if flag:
  print('Yes')
  for i in play:
    print(i)
else:
  print('No')