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
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 = []

def getidx(c):
    return 'ABC'.index(c)

g = [ [], [], [] ]
for i in xrange(n):
    s = raw_input()
    cmds.append(s)
    g[getidx(s[0])].append(i)
    g[getidx(s[1])].append(i)


cnt = [a, b, c]
res = []


for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1 or cnt[p1] == 0 or cnt[p2] == 0:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        if min(cnt) < 0:
            res = []
            break
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        if cmd == nxt or sum(cnt) > 2:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            for c in 'ABC':
                if c in cmd and c in nxt:
                    break
            else:
                assert False

            res.append(c)
            if c == cmd[0]:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
    assert min(cnt) >= 0

if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

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 = []

def getidx(c):
    return 'ABC'.index(c)

g = [ [], [], [] ]
for i in xrange(n):
    s = raw_input()
    cmds.append(s)
    g[getidx(s[0])].append(i)
    g[getidx(s[1])].append(i)


cnt = [a, b, c]
res = []


for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1 or cnt[p1] == 0 or cnt[p2] == 0:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        if min(cnt) < 0:
            res = []
            break
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        if cmd == nxt:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            for c in 'ABC':
                if c in cmd and c in nxt:
                    break
            else:
                assert False

            res.append(c)
            if c == cmd[0]:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
    assert min(cnt) >= 0

if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Wrong Answer

Solution code:
#F
N,A,B,C = map(int,input().split())
S =[]
for x in range(N):
    S.append(input())
ans =[]
f =True

for x in range(N):
    
    if A==0 and B==0 and C ==0:
        f =False
        break
    
    elif A+B+C ==1:
        if S[x] =="AB" and A==1:
            ans.append("B")
            A -=1
            B +=1
        elif S[x] =="AB" and B==1:
            ans.append("A")
            A +=1
            B -=1
        elif S[x] =="AC" and A==1:
            ans.append("C")
            A -=1
            C +=1
        elif S[x] =="AC" and C==1:
            ans.append("A")
            A +=1
            C -=1
        elif S[x] =="BC" and B==1:
            ans.append("C")
            B -=1
            C +=1
        elif S[x] =="BC" and C==1:
            ans.append("B")
            B +=1
            C -=1
        else:
            f =False
            break
    elif A +B+C >= 2:
        if A==0 or B ==0 or C==0:
            if S[x]=="AB" and A ==0 and B == 0:
                f =False
                break
            elif S[x]=="AC" and A ==0 and C == 0:
                f =False
                break
            elif S[x]=="BC" and C ==0 and B == 0:
                f =False
                break
        if A+B+C ==2 and x <N-1 and S[x]!=S[x+1]:
            if S[x+1] =="AC":
                ans.append("A")
                A +=1
                B -=1
            elif S[x+1] =="BC":
                ans.append("B")
                A -=1
                B +=1
            elif S[x+1] =="BC":
                ans.append("C")
                A -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("A")
                A +=1
                C -=1
            elif S[x+1] =="AC":
                ans.append("C")
                B -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("B")
                B +=1
                C -=1
                
        else:
            if S[x] =="AB" and B == 0:
                ans.append("B")
                A -=1
                B +=1
            elif S[x] =="AB":
                ans.append("A")
                A +=1
                B =1
            elif S[x] =="AC" and C ==0:
                ans.append("C")
                A -=1
                C +=1
            elif S[x] =="AC":
                ans.append("A")
                A +=1
                C -=1    
            elif S[x] =="BC" and C ==0:
                ans.append("C")
                B -=1
                C +=1
            elif S[x] =="BC":
                ans.append("B")
                B +=1
                C -=1

            
    
if f ==True:
    print("Yes")
    for x in ans:
        print(x)
else:
    print("No")

Submission status: Wrong Answer

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
                    b -= 1
                    res.append('A')
                else:
                    a -= 1
                    b += 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: Wrong Answer

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]:
            ans.append(cur1)
            C[cur1] += 1
            C[cur2] -= 1
        else:
            ans.append(cur2)
            C[cur2] += 1
            C[cur1] -= 1
    else:
        ans.append(cur1)
        C[cur1] += 1
        C[cur2] -= 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: Wrong Answer

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:
                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
                    B -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    C += 1
                    ans.append("C")
            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:
                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: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
abc = {'A':a,'B':b,'C':c}
ans = []

if a+b+c==0:
    for _ in range(n):
        s = input()
    print('No')
elif a+b+c==1:
    for _ in range(n):
        s = input()
        x,y = s[0],s[1]
        if abc[x]==abc[y]:
            print('No')
            break
        elif abc[x]==1:
            abc[x] -=1
            abc[y] +=1
            ans.append(y)
        else:
            abc[x] +=1
            abc[y] -=1
            ans.append(x)
elif a+b+c==2:
    s = input()
    x,y = s[0],s[1]
    if abc[x]==0 and abc[y]==0:
        print('No')
    else:
        for _ in range(n-1):
            s_ = input()
            x_,y_ = s_[0],s_[1]
            if abc[x]==abc[y]:
                if x==x_ and y==y_:
                    abc[x] +=1
                    abc[y] -=1
                    ans.append(x)
                elif x == x_ or (y != y_ and x == 'B'):
                    abc[x] +=1
                    abc[y] -=1
                    ans.append(x)
                else:
                    abc[x] -=1
                    abc[y] +=1
                    ans.append(y)
            elif abc[x]==2:
                abc[x] -=1
                abc[y] +=1
                ans.append(y)
            else:
                abc[x] +=1
                abc[y] -=1
                ans.append(x)
            x,y=x_,y_
        if abc[x]>=abc[y]:
            ans.append(y)
        else:
            ans.append(x)
else:
    s = input()
    x,y = s[0],s[1]
    if abc[x]==0 and abc[y]==0:
        print('No')
    else:
        for _ in range(n-1):
            if abc[x]>=abc[y]:
                abc[x] -=1
                abc[y] +=1
                ans.append(y)
            else:
                abc[x] +=1
                abc[y] -=1
                ans.append(x)
            s = input()
            x,y = s[0],s[1]
        if abc[x]>=abc[y]:
            ans.append(y)
        else:
            ans.append(x)
                
if len(ans)==n:
    print('Yes')
    print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
#F
N,A,B,C = map(int,input().split())
S =[]
for x in range(N):
    S.append(input())
ans =[]
f =True

for x in range(N):
    
    if A==0 and B==0 and C ==0:
        f =False
        break
    
    elif A+B+C ==1:
        if S[x] =="AB" and A==1:
            ans.append("B")
            A -=1
            B +=1
        elif S[x] =="AB" and B==1:
            ans.append("A")
            A +=1
            B -=1
        elif S[x] =="AC" and A==1:
            ans.append("C")
            A -=1
            C +=1
        elif S[x] =="AC" and C==1:
            ans.append("A")
            A +=1
            C -=1
        elif S[x] =="BC" and B==1:
            ans.append("C")
            B -=1
            C +=1
        elif S[x] =="BC" and C==1:
            ans.append("B")
            B +=1
            C -=1
        else:
            f =False
            break
    elif A +B+C >= 2:
        if A==0 or B ==0 or C==0:
            if S[x]=="AB" and A ==0 and B == 0:
                f =False
                break
            if S[x]=="AC" and A ==0 and C == 0:
                f =False
                break
            if S[x]=="BC" and C ==0 and B == 0:
                f =False
                break
        if A+B+C ==2 and x <N-1 and S[x]!=S[x+1]:
            if S[x+1] =="AC":
                ans.append("A")
                A +=1
                B -=1
            elif S[x+1] =="BC":
                ans.append("B")
                A -=1
                B +=1
            elif S[x+1] =="BC":
                ans.append("C")
                A -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("A")
                A +=1
                C -=1
            elif S[x+1] =="AC":
                ans.append("C")
                B -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("B")
                B +=1
                C -=1
                
        else:
            if S[x] =="AB" and B == 0:
                ans.append("B")
                A -=1
                B +=1
            elif S[x] =="AB":
                ans.append("A")
                A +=1
                B =1
            elif S[x] =="AC" and C ==0:
                ans.append("C")
                A -=1
                C +=1
            elif S[x] =="AC":
                ans.append("A")
                A +=1
                C -=1    
            elif S[x] =="BC" and C ==0:
                ans.append("C")
                B -=1
                C +=1
            elif S[x] =="BC":
                ans.append("B")
                B +=1
                C -=1

            
    
if f ==True:
    print("Yes")
    for x in ans:
        print(x)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split()))
slist = []
d = {'A': a, 'B':b, 'C':c}
soln = []
prev = None

for i in range(n):
    s = input()

    # corner case
    if prev:
        if prev[0] in s:
            d[prev[0]] += 1
            d[prev[1]] -= 1
            soln.append(prev[0])
        else:
            d[prev[1]] += 1
            d[prev[0]] -= 1
            soln.append(prev[1])
        prev = None
    
    # corner case
    if d[s[0]] ==1 and d[s[1]] == 1:
        prev = s

    # greedy
    elif d[s[0]] >= d[s[1]]:
        d[s[0]] -= 1
        d[s[1]] += 1
        soln.append(s[1])

    elif d[s[0]] < d[s[1]]:
        d[s[1]] -= 1
        d[s[0]] += 1
        soln.append(s[0])

    if d['A'] < 0 or d['B'] < 0 or d['C'] < 0:
        print('No')
        exit()
        break

print('Yes')
print('\n'.join(soln))

Submission status: Wrong Answer

Solution code:
i = input().split(' ')
A = int(i[1])
B = int(i[2])
C = int(i[3])
ans = ''
count=0
while count<int(i[0]):
    move = input()
    if move=='AB':
        up = min(A,B)
        if A==up:
            ans += 'A' + '\n'
        else:
            ans += 'B' + '\n'
        if up==A:
            A+=1
            B-=1
        else:
            A-=1
            B+=1
    elif move=='AC':
        up = min(A,C)
        if A==up:
            ans += 'A' + '\n'
        else:
            ans += 'C' + '\n'
        if up==A:
            A+=1
            C-=1
        else:
            A-=1
            C+=1
    else:
        up = min(B,C)
        if B==up:
            ans += 'B' + '\n'
        else:
            ans += 'C' + '\n'
        if up==B:
            B+=1
            C-=1
        else:
            B-=1
            C+=1
    count+=1
    m = min(A,B,C)
    if m<0:
        print('No')
        break

m = min(A,B,C)
if m>=0:
    print('Yes')
    print(ans, end='')

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split()))
slist = []
d = {'A': a, 'B':b, 'C':c}
flag = 1
soln = []
prev = None

for i in range(n):
    s = input()

    if prev:
        if prev[0] in s:
            d[prev[0]] += 1
            d[prev[1]] -= 1
            soln.append(prev[0])
        else:
            d[prev[1]] += 1
            d[prev[0]] -= 1
            soln.append(prev[1])
        prev = None
        
        if d['A'] < 0 or d['B'] < 0 or d['C'] < 0:
            print('No')
            flag = 0
            break
    
    # corner case
    if d[s[0]] ==1 and d[s[1]] == 1:
        prev = s

    # greedy
    elif d[s[0]] >= d[s[1]]:
        d[s[0]] -= 1
        d[s[1]] += 1
        soln.append(s[1])

    elif d[s[0]] < d[s[1]]:
        d[s[1]] -= 1
        d[s[0]] += 1
        soln.append(s[0])

    if d['A'] < 0 or d['B'] < 0 or d['C'] < 0:
        print('No')
        flag = 0
        break

if flag:
    print('Yes')
    print('\n'.join(soln))

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 = []

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

cnt = [a, b, c]
res = []

for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1 or cnt[p1] == 0 or cnt[p2] == 0:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        # assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        if cmd == nxt:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            for c in 'ABC':
                if c in cmd and c in nxt:
                    break
            else:
                assert False

            res.append(c)
            if c == cmd[0]:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
    if min(cnt) < 0:
        res = []
        break

if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Wrong Answer

Solution code:
X = [0, 0, 0]
N, X[0], X[1], X[2] = map(int, input().split())
Q = []
for _ in range(N):
    s = input()
    if s == 'AB':
        Q.append([0, 1])
    elif s == 'AC':
        Q.append([0, 2])
    else:
        Q.append([1, 2])
ans = True
ret = ''
for i in range(N):
    a = Q[i][0]
    b = Q[i][1]
    if X[a] == X[b] == 0:
        ans = False
        break
    elif i == N - 1 or X[a] < X[b]:
        ret += chr(ord(str(a)) + 17)
        X[a] += 1
        X[b] -= 1
    elif X[a] > X[b]:
        ret += chr(ord(str(b)) + 17)
        X[a] -= 1
        X[b] += 1
    else:
        s = Q[i + 1][0]
        t = Q[i + 1][1]
        if a == s or a == t:
            ret += chr(ord(str(a)) + 17)
            X[a] += 1
            X[b] -= 1
        else:
            ret += chr(ord(str(b)) + 17)
            X[a] -= 1
            X[b] += 1
    if X[a] < 0 or X[b] < 0:
        ans = False
        break
if ans:
    print('Yes')
    print('\n'.join(ret))
else:
    print('No')

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 = []

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

cnt = [a, b, c]
res = []

for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif cnt[p1] > 1 or cnt[p2] > 1 or (0 in [cnt[p1], cnt[p2]]):
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        # assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        if cmd == nxt:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            for c in 'ABC':
                if c in cmd and c in nxt:
                    break
            else:
                assert False

            res.append(c)
            if c == cmd[0]:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
    assert min(cnt) >= 0


if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Wrong Answer

Solution code:
i = input().split(' ')
A = int(i[1])
B = int(i[2])
C = int(i[3])
ans = ''
count=0
while count<int(i[0]):
    move = input()
    if move=='AB':
        up = min(A,B)
        if A==up:
            ans += 'A' + '\n'
        else:
            ans += 'B' + '\n'
        if up==A:
            A+=1
            B-=1
        else:
            A-=1
            B+=1
    elif move=='AC':
        up = min(A,C)
        if A==up:
            ans += 'A' + '\n'
        else:
            ans += 'C' + '\n'
        if up==A:
            A+=1
            C-=1
        else:
            A-=1
            C+=1
    else:
        up = min(B,C)
        if B==up:
            ans += 'B' + '\n'
        else:
            ans += 'C' + '\n'
        if up==B:
            B+=1
            C-=1
        else:
            B-=1
            C+=1
    count+=1
m = min(A,B,C)
if m>=0:
    print('Yes')
    print(ans, end='')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
S =[]
for x in range(N):
    S.append(input())
ans =[]
f =True

for x in range(N):
    
    if A==0 and B==0 and C ==0:
        f =False
        break
    
    elif A+B+C ==1:
        if S[x] =="AB" and A==1:
            ans.append("B")
            A -=1
            B +=1
        elif S[x] =="AB" and A==1:
            ans.append("A")
            A +=1
            B -=1
        elif S[x] =="AC" and A==1:
            ans.append("C")
            A -=1
            C +=1
        elif S[x] =="AC" and C==1:
            ans.append("A")
            A +=1
            C -=1
        elif S[x] =="BC" and B==1:
            ans.append("C")
            B -=1
            C +=1
        elif S[x] =="BC" and C==1:
            ans.append("B")
            B +=1
            C -=1
        else:
            f =False
            break
    elif A +B+C >= 2:
        if A==0 or B ==0 or C==0:
            if S[x]=="AB" and A ==0 and B == 0:
                f =False
                break
            if S[x]=="AC" and A ==0 and C == 0:
                f =False
                break
            if S[x]=="BC" and C ==0 and B == 0:
                f =False
                break
        elif A+B+C ==2 and x <N-1 and S[X]!=S[X+1]:
            if S[x+1] =="AC":
                ans.append("A")
                A +=1
                B -=1
            elif S[x+1] =="BC":
                ans.append("B")
                A -=1
                B +=1
            elif S[x+1] =="BC":
                ans.append("C")
                A -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("A")
                A +=1
                C -=1
            elif S[x+1] =="AC":
                ans.append("C")
                B -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("B")
                B +=1
                C -=1
                
        else:
            if S[x] =="AB":
                ans.append("B")
                A -=1
                B +=1
            elif S[x] =="AC":
                ans.append("C")
                A -=1
                C +=1
            if S[x] =="BC":
                ans.append("C")
                B -=1
                C +=1

            
    
if f ==True:
    print("Yes")
    for x in ans:
        print(x)
else:
    print("No")

Submission status: Wrong Answer

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 s==2 and abc[i1]*abc[i2]==1 and 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")