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:
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==0 or b+c==0 or c+a==0:
    if 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]==abc[y]:
            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]==abc[y]:
            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)
else:
     for _ in range(n):
        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:
n, a, b, c = list(map(int, input().split()))
slist = []
d = {'A': a, 'B':b, 'C':c}
flag = 1
soln = []
corner_case = False
prev = None

# Greedy approach is wrong
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
        
    # greedy
    if d[s[0]] ==1 and d[s[1]] == 1:
        prev = s

    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[s[1]]<0 or d[s[0]]<0 or sum(d.values()) == 0:
        print('No')
        flag = 0
        break

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

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 = []
corner_case = False
prev = None

# Greedy approach is wrong
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
        
    # greedy
    if d[s[0]] ==1 and d[s[1]] == 1:
        prev = s

    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[s[1]]<0 or d[s[0]]<0:
        print('No')
        flag = 0
        break

if flag:
    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('Yes')
    print(ans, end='')
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] >= 1 or cnt[p2] >= 1:
        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 cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif cnt[p1] >= 1 and cnt[p2] == 0:
        res.append(cmd[1])
        cnt[p1] -= 1
        cnt[p2] += 1
    elif cnt[p1] == 0 and cnt[p2] >= 1:
        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:
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 and cnt[p2] >= 1:
        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 cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif cnt[p1] >= 1 and cnt[p2] == 0:
        res.append(cmd[1])
        cnt[p1] -= 1
        cnt[p2] += 1
    elif cnt[p1] == 0 and cnt[p2] >= 1:
        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:
n,a,b,c=map(int,input().split())
ans=[]
for i in range(n):
  k = input()
  if k=="AB":
    if a==0 and b==0:
      print("No")
      quit()
    else:
      if a>b:
        a -= 1
        b+=1
        ans.append("B")
      else:
        b-=1
        a+=1
        ans.append("A")
  if k=="BC":
    if c==0 and b==0:
      print("No")
      quit()
    else:
      if c>b:
        c -= 1
        b+=1
        ans.append("B")
      else:
        b-=1
        c+=1
        ans.append("C")
  if k=="AC":
    if a==0 and c==0:
      print("No")
      quit()
    else:
      if a>c:
        a -= 1
        c+=1
        ans.append("C")
      else:
        c-=1
        a+=1
        ans.append("A")
print("Yes")
for i in ans:
  print(i)

Submission status: Wrong Answer

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):
  if s == 'AB':
    if A == 0 and B == 0:
      flag = False
      break
    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:
      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:
      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')

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 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==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 >2 and B> 2 and C >2:
        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
            
            
    elif A < 2 or B <2 or C <2:
        if S[x] =="AB" and A<B:
            ans.append("A")
            A +=1
            B -=1
        elif S[x] =="AB" and B<=A:
            ans.append("B")
            A -=1
            B +=1
        elif S[x] =="AC" and C<A:
            ans.append("C")
            A -=1
            C +=1
        elif S[x] =="AC" and A<=C:
            ans.append("A")
            A +=1
            C -=1
        elif S[x] =="BC" and C<B:
            ans.append("C")
            B -=1
            C +=1
        elif S[x] =="BC" and B<=C:
            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 numpy as np

def to_number(text):
    if text == "A":
        return 0
    if text == "B":
        return 1
    if text == "C":
        return 2

N, a, b, c = map(int, input().split())

abc = np.zeros(3)
abc_first = np.array([a, b, c])
choice = []
text = list(input())
for i in range(2):
        n = to_number(text[i])
        abc[n] += 1
        if abc[n] >= 2:
            abc[n] -= 2
text2 = text            
for _ in range(N-1):
    text = list(input())
    for i in range(2):
        n = to_number(text[i])
        abc[n] += 1
        if abc[n] >= 2:
            choice.append(text[i])
            abc[n] -= 2
    
    text2 = text
if sum(abc_first[abc == 1]) == 0:
    print("No")
    exit(0)
print("Yes")
for i in choice:
    print(i)
# print(abc)
if abc[n] == 1:
    print(text[1])
else:
    print(text[0])
# print(choice)

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 and cnt[p2] >= 1:
        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 cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif cnt[p1] >= 1 and cnt[p2] == 0:
        res.append(cmd[1])
        cnt[p1] -= 1
        cnt[p2] += 1
    elif cnt[p1] == 0 and cnt[p2] >= 1:
        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]
        p3 = ord(nxt[0]) - ord('A')
        p4 = ord(nxt[1]) - ord('A')

        if p3 in [p1, p2] and cnt[p4] == 0:
            if p3 == p1:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
        elif p4 in [p1, p2] and cnt[p3] == 0:
            if p4 == p1:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
        else:
            res.append(cmd[0])
            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 = []

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 and cnt[p2] >= 1:
        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 cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif cnt[p1] >= 1 and cnt[p2] == 0:
        res.append(cmd[1])
        cnt[p1] -= 1
        cnt[p2] += 1
    elif cnt[p1] == 0 and cnt[p2] >= 1:
        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]
        p3 = ord(nxt[0]) - ord('A')
        p4 = ord(nxt[1]) - ord('A')

        if p3 in [p1, p2] and cnt[p4] == 0:
            if p3 == p1:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
        if p4 in [p1, p2] and cnt[p3] == 0:
            if p4 == p1:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
        else:
            res.append(cmd[0])
            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 = []

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 and cnt[p2] >= 1:
        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 cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif cnt[p1] >= 1 and cnt[p2] == 0:
        res.append(cmd[1])
        cnt[p1] -= 1
        cnt[p2] += 1
    elif cnt[p1] == 0 and cnt[p2] >= 1:
        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 i + 1 < n

        nxt = cmds[i + 1]
        p3 = ord(nxt[0]) - ord('A')
        p4 = ord(nxt[1]) - ord('A')

        if p3 in [p1, p2] and cnt[p4] == 0:
            if p3 == p1:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
        else:
            res.append(cmd[0])
            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:
n, a, b, c = list(map(int, input().split()))
slist = []
d = {'A': a, 'B':b, 'C':c}
flag = 1
soln = []
corner_case = False
prev = None

# Greedy approach is wrong
for i in range(n):
    s = input()
    # greedy
    if 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])

    else: #d[s[0]] == d[s[1]]
        
        corner_case = True
        prev = s

    if corner_case:
        # increment and decrememnt based on the future.
        if prev[0] in s:
            d[prev[0]] += 1
            d[prev[1]] -= 1
            soln.append(prev[0])
        else:
            d[prev[0]] -= 1
            d[prev[1]] += 1
            soln.append(prev[1])
        corner_case = False

    if d[s[1]]<0 or d[s[0]]<0:
        print('No')
        flag = 0
        break
if flag:
    print('Yes')
    print('\n'.join(soln))

Submission status: Wrong Answer

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

history = [] 
flag = True
  
for _ in range(n):
  s = input()
  if s == 'AB':
    if a == 0 and b == 0:
      print('No')
      flag = False
      break
    elif a == 0:
      history.append('A')
      a += 1
      b -= 1
    elif b == 0:
      history.append('B')
      a -= 1
      b += 1
    else:
      if a >= b:
        history.append('B')
        a -= 1
        b += 1
      else:
        history.append('A')
        a += 1
        b -= 1
  elif s == "AC":
    if a == 0 and c == 0:
      print("No")
      flag = False
      break
    elif a == 0:
      history.append('A')
      a += 1
      c -= 1
    elif c == 0:
      history.append('C')
      a -= 1
      c += 1
    else:
      if a >= c:
        history.append('C')
        a -= 1
        c += 1
      else:
        history.append('A')
        a += 1
        c -= 1
  elif s == "BC":
    if b == 0 and c == 0:
      print("No")
      flag = False
      break
    elif b == 0:
      history.append('B')
      b += 1
      c -= 1
    elif c == 0:
      history.append('C')
      b -= 1
      c += 1
    else:
      if b >= c:
        history.append('C')
        b -= 1
        c += 1
      else:
        history.append('B')
        b += 1
        c -= 1
if flag:
  print("Yes")
  print('\n'.join(history))

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, j in Q:
    if X[i] == X[j] == 0:
        ans = False
        break
    elif X[i] < X[j]:
        ret += chr(ord(str(i)) + 17)
        X[i] += 1
        X[j] -= 1
    else:
        ret += chr(ord(str(j)) + 17)
        X[i] -= 1
        X[j] += 1
    if X[i] < 0 or X[j] < 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] > 1 and cnt[p2] > 1:
        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 cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif cnt[p1] >= 1 and cnt[p2] == 0:
        res.append(cmd[1])
        cnt[p1] -= 1
        cnt[p2] += 1
    elif cnt[p1] == 0 and cnt[p2] >= 1:
        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 i + 1 < n

        nxt = cmds[i + 1]
        p3 = ord(nxt[0]) - ord('A')
        p4 = ord(nxt[1]) - ord('A')

        if p3 in [p1, p2] and cnt[p4] == 0:
            if p3 == p1:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
        else:
            res.append(cmd[0])
            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:
n, a, b, c = map(int, input().split())

if a + b + c == 0:
  print('No')

history = [] 
flag = True
  
for _ in range(n):
  s = input()
  if s == 'AB':
    if a == 0 and b == 0:
      print('No')
      flag = False
      break
    elif a == 0:
      history.append('A')
      a += 1
      b -= 1
    elif b == 0:
      history.append('B')
      a -= 1
      b += 1
    else:
      if a >= b:
        history.append('B')
        a -= 1
        b += 1
      else:
        history.append('A')
        a += 1
        b -= 1
  elif s == "AC":
    if a == 0 and c == 0:
      print("No")
      flag = False
      break
    elif a == 0:
      history.append('A')
      a += 1
      c -= 1
    elif c == 0:
      history.append('C')
      a -= 1
      c += 1
    else:
      if a >= c:
        history.append('C')
        a -= 1
        c += 1
      else:
        history.append('A')
        a += 1
        c -= 1
  else:
    if b == 0 and c == 0:
      print("No")
      flag = False
      break
    elif b == 0:
      history.append('B')
      b += 1
      c -= 1
    elif c == 0:
      history.append('C')
      b -= 1
      c += 1
    else:
      if b >= c:
        history.append('C')
        b -= 1
        c += 1
      else:
        history.append('B')
        b += 1
        c -= 1
if flag:
  print("Yes")
  print('\n'.join(history))

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 and cnt[p2] > 1:
        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 cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif cnt[p1] >= 1 and cnt[p2] == 0:
        res.append(cmd[1])
        cnt[p1] -= 1
        cnt[p2] += 1
    elif cnt[p1] == 0 and cnt[p2] >= 1:
        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 i + 1 < n

        nxt = cmds[i + 1]
        p3 = ord(nxt[0]) - ord('A')
        p4 = ord(nxt[1]) - ord('A')

        if p3 in [p1, p2] and cnt[p4] == 0:
            if p3 == p1:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1


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

Submission status: Wrong Answer

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

if a + b + c == 0:
  print('No')

history = [] 
flag = True
  
for _ in range(n):
  s = input()
  if s == 'AB':
    if a == 0 and b == 0:
      print('No')
      flag = False
      break
    elif a == 0:
      history.append('A')
      a += 1
      b -= 1
    elif b == 0:
      history.append('B')
      a -= 1
      b += 1
    else:
      if a >= b:
        history.append('B')
        a -= 1
        b += 1
      else:
        history.append('A')
        a += 1
        b -= 1
  elif s == "AC":
    if a == 0 and c == 0:
      print("No")
      flag = False
      break
    elif a == 0:
      history.append('A')
      a += 1
      c -= 1
    elif c == 0:
      history.append('C')
      a -= 1
      c += 1
    else:
      if a >= c:
        history.append('C')
        a -= 1
        c += 1
      else:
        history.append('A')
        a += 1
        c -= 1
  elif s == "BC":
    if b == 0 and c == 0:
      print("No")
      flag = False
      break
    elif b == 0:
      history.append('B')
      b += 1
      c -= 1
    elif c == 0:
      history.append('C')
      b -= 1
      c += 1
    else:
      if b >= c:
        history.append('C')
        b -= 1
        c += 1
      else:
        history.append('B')
        b += 1
        c -= 1
if flag:
  print("Yes")
  print('\n'.join(history))