import sys input = lambda: sys.stdin.readline().rstrip() from collections import defaultdict as dd # aまでcが最多となる条件を考える # ⇒c>a//3は必要 # ⇒c>a//2あれば十分 # 必要条件をチェックして大丈夫そうなら適当に構成する # indexをRGBに変換 i2c=["R","G","B"] # R,G,Bをindexに変換 c2i=dd(int) c2i["R"]=0 c2i["G"]=1 c2i["B"]=2 N,K=map(int,input().split()) AC=[] for _ in range(K): a,c=input().split() a=int(a) AC.append((a,c)) AC=sorted(AC) # 必要条件チェック RGB=[0,0,0] for a,c in AC: # 今確認したい色以外の必要数をチェック othermax=0 for i in range(3): if c2i[c]==i:continue othermax=max(othermax,RGB[i]) # cはc>a//3かそれまでの最多数を更新する必要がある RGB[c2i[c]]=max((a+2)//3,othermax,0) # RGBの総和がaを上回ればNG if a0: res.append(c) resRGB[c2i[c]]+=1 RGB[c2i[c]]-=1 target-=1 # 他の色が残っている場合は最多条件を破らないようにいい感じに埋める for i in range(3): if c2i[c]==i:continue while RGB[i] and resRGB[i]