Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis!

The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options.

You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once.

You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy.


-----Input-----

The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively.

The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list.

The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in.


-----Output-----

Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy.


-----Example-----
Input
2 1 1
4
5 USB
6 PS/2
3 PS/2
7 PS/2

Output
3 14



-----Note-----

In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.

Difficulty: interview

Starter code:

Solutions:
import math, re, itertools as it;prime = lambda n: len([i for i in range(2, int(math.sqrt(n) + 1)) if n % i == 0]) == 0;gcd = lambda a, b: gcd(b, a % b) if b else a;fact = lambda x: x * fact(x - 1) if x else 1;bino = lambda n, k: fact(n) / fact(k) / fact(n - k);fib11 = lambda n: 1 if n < 2 else fib11(n - 1) + fib11(n - 2);fib01 = lambda n: 0 if n == 0 else 1 if n == 1 else fib01(n - 1) + fib01(n - 2);sumofd = lambda x: x if x < 10 else sumofd(x // 10) + x % 10

a, b, c = map(int, input().split())
m = int(input())
d = []
for i in range(m):
	s = input().split()
	d.append([int(s[0]), 1 if s[1] == 'USB' else 0])
d.sort()
i = 0
p = 0
nn = 0
while i < len(d) and (a or b or c):
	f1 = f2 = False
	if a and d[i][1]:
		a -= 1
		p += d[i][0]
		f1 = True
		nn += 1
	if b and d[i][1] == 0:
		b -= 1
		p += d[i][0]
		f2 = True
		nn += 1
	if not f1 and not f2:
		if c:
			c -= 1
			p += d[i][0]
			nn += 1
	i += 1
print(nn, p)

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

USB, PS = [], []

for _ in range(m):
    s = input().split()
    l, r = int(s[0]), s[1]

    if r == "USB":
        USB.append(l)
    else:
        PS.append(l)

USB.sort()
PS.sort()

a = min(a, len(USB))
res = sum(USB[:a])
USB = USB[a:]

b = min(b, len(PS))
res += sum(PS[:b])
BOTH = PS[b:] + USB
BOTH.sort()

c = min(c, len(BOTH))
res += sum(BOTH[:c])

print(str(a + b + c) + " " + str(res))


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

m = int(input())

usb = []
ps2 = []
for i in range(m):
    aa,bb = input().split()
    if(bb=='USB'):
        usb.append(int(aa))
    else:
        ps2.append(int(aa))

usb.sort()
ps2.sort()

equip = 0
cost = 0

t = min(a,len(usb))
equip += t
for i in range(t):
    cost += usb[i]

p = min(b,len(ps2))
equip += p
for i in range(p):
    cost += ps2[i]

rest = usb[t:]+ps2[p:]
rest.sort()

k = min(c,len(rest))
equip += k
for i in range(k):
    cost += rest[i]

print(equip, cost)

a, b, c = map(int, input().split())
m = int(input())
usb = []
ps = []
for i in range(m):
    val, name = input().split()
    if name == 'USB':
        usb.append(int(val))
    else:
        ps.append(int(val))
usb.sort()
ps.sort()
sum1 = sum(usb[:min(len(usb), a)])
sum2 = sum(ps[:min(len(ps), b)])
u1 = min(len(usb), a)
u2 = min(len(ps), b)
usbps = usb[min(len(usb), a):] + ps[min(len(ps), b):]
usbps.sort()
sum3 = sum(usbps[:min(len(usbps), c)])
u3 = min(len(usbps), c)
print(u1 + u2 + u3, sum1 + sum2 + sum3)

MAXINT = 10 ** 10

def __starting_point():
	a, b, c = map(int, input().split())
	m, = map(int, input().split())
	usb, ps2 = [], []
	for _ in range(m):
		v, tp = input().split()
		if tp == 'USB':
			usb.append(int(v))
		else:
			ps2.append(int(v))
	usb.sort()
	ps2.sort()
	ui, pi = 0, 0
	un, pn = len(usb), len(ps2)
	#print(usb, ps2, un, pn)
	usb.append(MAXINT)
	ps2.append(MAXINT)
	res, cost = 0, 0
	while (ui < un and a > 0):
		res += 1
		cost += usb[ui]
		ui += 1
		a -= 1
	#print(res, cost)
	while (pi < pn and b > 0):
		res += 1
		cost += ps2[pi]
		pi += 1
		b -= 1
	#print(res, cost)
	while (c > 0 and (ui < un or pi < pn)):
		v = None
		if usb[ui] <= ps2[pi]:
			v = usb[ui]
			ui += 1
		else:
			v = ps2[pi]
			pi += 1
		res += 1
		cost += v
		c -= 1
	print(res, cost)
__starting_point()

a, b, c = map(int, input().split())
n = int(input())
mice_usb = []
mice_ps = []
for i in range(n):
	price, mice_type = input().split()
	if mice_type[0] == 'U':
		mice_usb.append(int(price))
	else:
		mice_ps.append(int(price))

usb = min(len(mice_usb), a)
ps2 = min(len(mice_ps), b)

mice_usb.sort()
mice_ps.sort()

answer = sum(mice_usb[:usb]) + sum(mice_ps[:ps2])
num = usb + ps2

left = mice_usb[usb:] + mice_ps[ps2:]
left.sort()

any_num = min(len(left), c)
answer += sum(left[:any_num])
num += any_num

print(num, answer)

#!/usr/bin/env python3
def ri():
    return list(map(int, input().split()))

a, b, c = ri()

m = int(input())

u = []
p = []
for i in range(m):
    cost, t = input().split()
    cost = int(cost)
    if t == "USB":
        u.append(cost)
    else:
        p.append(cost)

u.sort(reverse=True)
p.sort(reverse=True)


count = 0
totcost = 0
for i in range(a):
    if len(u):
        count += 1
        totcost += u[-1]
        u.pop(-1)
    else:
        break
for i in range(b):
    if len(p):
        count += 1
        totcost += p[-1]
        p.pop(-1)
    else:
        break

for i in range(c):
    if len(u) and len(p):
        count += 1
        if u[-1] < p[-1]:
            totcost += u[-1]
            u.pop(-1)
        else:
            totcost += p[-1]
            p.pop(-1)
    elif len(u):
        count += 1
        totcost += u[-1]
        u.pop(-1)
    elif len(p):
        count += 1
        totcost += p[-1]
        p.pop(-1)
    else:
        break

print(count, totcost)


a, b, c = map(int, input().split())
m = int(input())
usbMice = []
ps2Mice = []
for i in range(m):
	price, mouseType = input().split()
	if mouseType == 'USB':
		usbMice.append(int(price))
	else:
		ps2Mice.append(int(price))

usbMice.sort()
ps2Mice.sort()

quantity = 0
totalPrice = 0

aOk = min(a, len(usbMice))
bOk = min(b, len(ps2Mice))

totalPrice += sum(usbMice[:aOk])
totalPrice += sum(ps2Mice[:bOk])

mixMice = usbMice[aOk:] + ps2Mice[bOk:]

mixMice.sort()

cOk = min(c, len(mixMice))

totalPrice += sum(mixMice[:cOk])
quantity = aOk + bOk + cOk

print(quantity, totalPrice)

import sys

a, b, c = map(int, sys.stdin.readline().split())
m = int(sys.stdin.readline())

num_item = 0
cost = 0

usb_l = []
ps2_l = []

for i in range(m):
    val, type_p = sys.stdin.readline().split()
    val = int(val)

    if type_p[0] == 'U':
        usb_l.append(val)
    else:
        ps2_l.append(val)

usb_l.sort()
ps2_l.sort()

len_usb = len(usb_l)
len_ps2 = len(ps2_l)

usb_l.append(float('inf'))
ps2_l.append(float('inf'))

'''
print(usb_l, file=sys.stderr)
print(ps2_l, file=sys.stderr)
'''

pos1 = 0
pos2 = 0

if a > len_usb:
    num_item += len_usb
    cost += sum(usb_l[:-1])
    pos1 = len_usb
else:
    num_item += a
    cost += sum(usb_l[:a])
    pos1 = a

if b > len_ps2:
    num_item += len_ps2
    cost += sum(ps2_l[:-1])
    pos2 = len_ps2
else:
    num_item += b
    cost += sum(ps2_l[:b])
    pos2 = b

count = 0

while num_item < m and count < c:
    if usb_l[pos1] < ps2_l[pos2]:
        cost += usb_l[pos1]
        pos1 += 1
    else:
        cost += ps2_l[pos2]
        pos2 += 1

    num_item += 1
    count += 1

print(num_item, cost)

def list_from_input():
    return list(map(int, input().split()))

class Mouse:
    def __init__(self, price, type):
        self.type = type
        self.price = price

    @classmethod
    def from_input(cls):
        mouse_data = input().split()
        price = int(mouse_data[0])
        type = mouse_data[1][0]
        return Mouse(price, type)


def main():
    usb_pc, ps_pc, both_pc = list_from_input()
    mouses_count = int(input())

    mouses = []
    for i in range(mouses_count):
        mouses.append(Mouse.from_input())

    mouses.sort(key=lambda mouse: mouse.price)
    purchase_amount = 0
    pc_with_mouses = 0
    for mouse in mouses:
        if mouse.type is 'U' and usb_pc > 0:
            usb_pc -= 1
            purchase_amount += mouse.price
            pc_with_mouses += 1
        elif mouse.type is 'P' and ps_pc > 0:
            ps_pc -= 1
            purchase_amount += mouse.price
            pc_with_mouses += 1
        elif both_pc > 0:
            both_pc -= 1
            purchase_amount += mouse.price
            pc_with_mouses += 1


    print(pc_with_mouses, purchase_amount)

main()






a,b,c=list(map(int,input().split()))
n,u0,u1=int(input()),[],[]
for i in range(n):
    e0,e1=input().split()
    if e1=='USB':
        u0.append(int(e0))
    else:
        u1.append(int(e0))
u0.sort()
u1.sort()
n=0
x=0
if len(u0)>a:
    x=x+sum(u0[:a])
    u0=u0[a:]
    n=n+a
else:
    x=x+sum(u0)
    n=n+len(u0)
    u0=[]
if len(u1)>b:
    x=x+sum(u1[:b])
    u1=u1[b:]
    n=n+b
else:
    x=x+sum(u1)
    n=n+len(u1)
    u1=[]
u=u1+u0
u.sort()
if len(u)>c:
    x=x+sum(u[:c])
    n=n+c
else:
    x=x+sum(u)
    n=n+len(u)
print(str(n)+' '+str(x))


from sys import stdin

A, B, C = list(map(int, stdin.readline().split()))
M = int(stdin.readline())

usb_mouses = []
ps2_mouses = []
for m in range(M):
    p, t = stdin.readline().split()

    if t == "USB":
        usb_mouses.append(int(p))
    elif t == "PS/2":
        ps2_mouses.append(int(p))

usb_mouses.sort()
ps2_mouses.sort()

usb_cur = 0
ps2_cur = 0

cost = 0

while usb_cur < len(usb_mouses) and A > 0:
    cost += usb_mouses[usb_cur]
    usb_cur += 1
    A -= 1

while ps2_cur < len(ps2_mouses) and B > 0:
    cost += ps2_mouses[ps2_cur]
    ps2_cur += 1
    B -= 1

while C > 0:
    if usb_cur < len(usb_mouses) and ps2_cur < len(ps2_mouses):
        if usb_mouses[usb_cur] < ps2_mouses[ps2_cur]:
            cost += usb_mouses[usb_cur]
            usb_cur += 1
        else:
            cost += ps2_mouses[ps2_cur]
            ps2_cur += 1
    elif usb_cur < len(usb_mouses):
        cost += usb_mouses[usb_cur]
        usb_cur += 1
    elif ps2_cur < len(ps2_mouses):
        cost += ps2_mouses[ps2_cur]
        ps2_cur += 1
    else:
        break

    C -= 1

print("{} {}".format(usb_cur + ps2_cur, cost))


a,b,c=list(map(int,input().split()))
m=int(input())
USB=[]
PS=[]
i2=0
i1=0
ans1=0
ans2=0
for i in range(m):
	n,k=input().split()
	if(k=="USB"):
		USB.append(int(n))
	else:
		PS.append(int(n))
USB.sort()
PS.sort()
for i in range(a):
	if(i>=len(USB)):
		break
	ans1+=USB[i]
	ans2+=1
	i1=i+1
for i in range(b):
	if(i>=len(PS)):
		break
	ans1+=PS[i]
	ans2+=1
	i2=i+1
for i in range(c):
	if(i2<len(PS) and i1<len(USB)):
		if(PS[i2]<USB[i1]):
			ans1+=PS[i2]
			i2+=1
			ans2+=1
		else:
			ans1+=USB[i1]
			i1+=1
			ans2+=1
	elif(i2>=len(PS)):
		if(i1<len(USB)):
			ans1+=USB[i1]
			i1+=1
			ans2+=1
	elif(i1>=len(USB)):
		if(i2<len(PS)):
			ans1+=PS[i2]
			i2+=1
			ans2+=1
print(ans2,ans1)			


a,b,c=list(map(int,input().split()))
arr=[[],[]]
for i in range(int(input())):
	p,s=input().split()
	pr=int(p)
	if s=="USB":
		arr[0].append(pr)
	else:
		arr[1].append(pr)
arr[0].sort()
arr[1].sort()
# print(arr)
pr=0
n=0
d=[]
for j in range(min(a,len(arr[0]))):
	n+=1
	pr+=arr[0][j]
d+=arr[0][min(a,len(arr[0])):]
for j in range(min(b,len(arr[1]))):
	n+=1
	pr+=arr[1][j]
	arr[1][j]=1e10
d+=arr[1][min(b,len(arr[1])):]
d.sort()
# print(d)
for k in range(min(c,len(d))):
	n+=1
	pr+=d[k]
print(n,pr)


a,b,c=list(map(int,input().split()))
m=int(input())
z=0
A=[]
B=[]
su=0
for i in range(m):
    s=input().split()
    if s[1]=='USB':
        A.append(int(s[0]))
    else:
        B.append(int(s[0]))
A.sort()
B.sort()
##print(A,B)
if a < len(A):
    su+=sum(A[:a])
    ##print(A[:a],'A')
    A=A[a:]
    z+=a
    a=0
    
else:
    su+=sum(A)
    z+=len(A)
    a+=-len(A)
   ## print(a,'a')
    A=[]
if b < len(B):
    su+=sum(B[:b])
    ##print(B[:b],'B')
    B=B[b:]
    z+=b
    b=0
else:
    su+=sum(B)
    z+=len(B)
    b+=-len(B)
   ## print(b,'b')
    B=[]
i=0
j=0
while c>0 and len(A)>i and len(B)>j:
    if A[i]==B[j]:
        if len(A)-1-i>len(B)-1-j:
            su+=A[i]
           ## print(A[i],'!!!')
            i+=1
        else:
            su+=B[j]
            ##print(B[j],'! !')
            j+=1
    elif min(A[i],B[j])==A[i]:
        su+=A[i]
        ##print(A[i],'!!!')
        i+=1
    else:
        su+=B[j]
        ##print(B[j],'! !')
        j+=1
    c+=-1
    z+=1
##print(B[j:j+c]),(A[i:i+c])
su +=sum(B[j:j+c])+sum(A[i:i+c])
z+=len(B[j:j+c])+len(A[i:i+c])
print(z,su)
    
    


a,b,c=list(map(int,input().split()))
m=int(input())
d={'USB':[], 'PS/2':[]}
for _ in range(m):
  v,t=input().split()
  d[t].append(int(v))

d['PS/2'].sort()
d['USB'].sort()

eq=cst=f1=f2=0
nusb=len(d['USB'])
nps2=len(d['PS/2'])
while a>0 and f1<nusb:
  a-=1
  eq+=1
  cst+=d['USB'][f1]
  f1+=1
while b>0 and f2<nps2:
  b-=1
  eq+=1
  cst+=d['PS/2'][f2]
  f2+=1
while c>0 and (f1<nusb or f2<nps2):
  c-=1
  eq+=1
  if f1==nusb:
    cst+=d['PS/2'][f2]
    f2+=1
    continue
  elif f2==nps2:
    cst+=d['USB'][f1]
    f1+=1
    continue
    
  if d['PS/2'][f2]<d['USB'][f1]:
    cst+=d['PS/2'][f2]
    f2+=1
  else:
    cst+=d['USB'][f1]
    f1+=1

print(eq,cst)




__author__ = 'zihaozhu'
from sys import stdin

USBOnly, UpsOnly, either = map(int, stdin.readline().split())
options = int(stdin.readline().rstrip())

usb = []
ups = []
for i in range (options):
    ip = stdin.readline().split()
    if ip[1] == "USB":
        usb.append(int(ip[0]))
    else:
        ups.append(int(ip[0]))

usb.sort()
ups.sort()
numMouse = 0
cost = 0
usbCnt = 0
upsCnt = 0
while USBOnly>0 and usbCnt<len(usb):
    cost+=usb[usbCnt]
    usbCnt+=1
    USBOnly-=1
    numMouse+=1

while UpsOnly>0 and upsCnt<len(ups):
    cost+=ups[upsCnt]
    upsCnt+=1
    UpsOnly-=1
    numMouse+=1

while either>0 and (usbCnt<len(usb) or upsCnt<len(ups)):
    either-=1
    val1 = float('inf')
    val2 = float('inf')
    if usbCnt<len(usb):
        val1 = usb[usbCnt]
        #usbCnt+=1
    if upsCnt<len(ups):
        val2 = ups[upsCnt]
        #upsCnt+=1
    numMouse+=1
    minVal = min(val1,val2)
    if minVal == val1:
        usbCnt+=1
    elif minVal == val2:
        upsCnt+=1
    cost+=min(val1,val2)


print("%s %s"% (numMouse, cost))

(u,p,b) = tuple(map(int,input().split()))
n = int(input())
equiped = 0
fullPrice = 0
arrusb = []
arrpc2 = []
for k in range(n):
    (price, mtype) = tuple(input().split())
    if mtype == "USB":
        arrusb.append(int(price))
    else:
        arrpc2.append(int(price))

arrusb.sort()
arrpc2.sort()
bcounter=0
pcounter=0

for i in range(n):
    if bcounter==len(arrusb):
        if p > 0:
            fullPrice+=arrpc2[pcounter]
            p -= 1
            equiped += 1
        elif b > 0:
            fullPrice += arrpc2[pcounter]
            b -= 1
            equiped += 1
        pcounter+=1
        continue

    if pcounter == len(arrpc2):
        if u > 0:
            fullPrice+=arrusb[bcounter]
            u-=1
            equiped+=1
        elif b > 0:
            fullPrice+=arrusb[bcounter]
            b-=1
            equiped+=1
        bcounter+=1
        continue

    if arrusb[bcounter] < arrpc2[pcounter]:
        if u > 0:
            fullPrice+=arrusb[bcounter]
            u-=1
            equiped+=1
        elif b > 0:
            fullPrice+=arrusb[bcounter]
            b-=1
            equiped+=1
        bcounter+=1
    else:
        if p > 0:
            fullPrice+=arrpc2[pcounter]
            p -= 1
            equiped += 1
        elif b > 0:
            fullPrice += arrpc2[pcounter]
            b -= 1
            equiped += 1
        pcounter+=1

print(str(equiped) + ' ' + str(fullPrice))


from math import floor,sqrt
from collections import Counter
from functools import reduce
def __starting_point():
    a,b,c=list(map(int, input().split()))
    m = int(input())

    mouse = {"USB":[], "PS/2":[]}
    for _ in range(m):
        p,m = input().strip().split()
        mouse[m].append(int(p))

    for m in list(mouse.keys()):
        mouse[m].sort()

    equipped = 0
    cost = 0
    for i in range(a):
        if i < len(mouse["USB"]):
            cost += mouse["USB"][i]
            equipped += 1
        else:
            break
    for j in range(b):
        if j < len(mouse["PS/2"]):
            cost += mouse["PS/2"][j]
            equipped += 1
        else:
            break

    for k in range(c):
        A = ( a < len(mouse["USB"]) )
        B = ( b < len(mouse["PS/2"]))

        if A:
            if B:
                if mouse["USB"][a] < mouse ["PS/2"][b]:
                    price = mouse["USB"][a]
                    a += 1
                else:
                    price = mouse["PS/2"][b]
                    b += 1
            else:
                price = mouse["USB"][a]
                a += 1

            equipped += 1
            cost += price
        else:
            if B:
                price = mouse["PS/2"][b]
                b += 1
                equipped += 1
                cost += price
            else:
                break

    print(str(equipped) + "  " + str(cost))



__starting_point()

z,b,c = map(int,input().split())
m = int(input())
a = []
for _ in range(m):
    x,y = input().split()
    if y == 'USB':
        a.append([int(x),1])
    else:
        a.append([int(x),2])
cost = ans = 0        
a.sort()
for i in a:
    if i[1] == 1:
        if z:
            z -= 1
            ans += 1
            cost += i[0]
        elif c:
            c -= 1
            ans += 1
            cost += i[0]
    else:
        if b:
            b -= 1
            ans += 1
            cost += i[0]
        elif c:
            c -= 1
            ans += 1
            cost += i[0]
print(ans,cost)            

from collections import deque

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

usb_cost = []
ps_cost = []


for i in range(m):
	cost, t = input().split()
	cost = int(cost)

	if t == 'USB':
		usb_cost.append(cost)
	else:
		ps_cost.append(cost)


usb_cost.sort(reverse = True)
ps_cost.sort(reverse = True)

# print(usb_cost)
# print(ps_cost)

ans = 0
ans_sum = 0

for i in range(min(a, len(usb_cost))):
	ans_sum += usb_cost.pop()
	ans += 1

for i in range(min(b, len(ps_cost))):
	ans_sum += ps_cost.pop()
	ans += 1

all_types = sorted(usb_cost + ps_cost, reverse = True)

for i in range(min(c, len(all_types))):
	ans_sum += all_types.pop()
	ans += 1


print(ans, ans_sum)

a,b,c =list(map(int,input().split()))
n = int(input())
usb = list()
pc2 = list()
for i in range(n):
    kl = input().split()
    if(kl[1]=="USB"):
        usb.append(int(kl[0]))
    else:
        pc2.append(int(kl[0]))
usb = sorted(usb)
pc2 = sorted(pc2)
summ = 0
num = 0
i = 0
j = 0
can = True
if(len(usb)<=a):
    summ+=sum(usb)
    i=len(usb)
    a-=len(usb)
    num+=i
else:
    summ+=sum(usb[0:a])
    i=a
    a=0
    num+=i

if(len(pc2)<=b):
    summ+=sum(pc2)
    j=len(pc2)
    b=0
    num+=len(pc2)
else:
    summ+=sum(pc2[0:b])
    j=b
    b=0
    num+=j
#print(j, summ, num)
while (c>0 and( i<len(usb) or j <len(pc2))):
    if(i>=len(usb)):
        if(len(pc2)-j>=c):
            summ+=sum(pc2[j:j+c])

            num+=c
            c=0
        else:
            summ+=sum(pc2[j:])
            c=0
            num+=len(pc2[j:])
    elif(j>=len(pc2)):
        if(len(usb)-i>=c):
            summ+=sum(usb[i:i+c])

            num+=c
            c=0
        else:
            summ+=sum(usb[i:])

            num+=len(usb[i:])
            c=0
    else:
        num+=1
        if(usb[i]<=pc2[j]):
            summ+=usb[i]
            c=c-1
            i+=1
        else:
            summ+=pc2[j]
            c=c-1
            j+=1
print(num, summ)


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

n = int(input())

usb = []
ps = []

for i in range(n):
	x = input().split()
	
	if x[1] == "USB":
		usb.append(int(x[0]))
	else:
		ps.append(int(x[0]))

usb.sort()
usb.reverse()
ps.sort()
ps.reverse()

usb_k = min(a, len(usb))
ps_k  = min(b, len(ps))


price = 0


for i in range(usb_k):
	price += usb.pop()


for i in range(ps_k):
	price += ps.pop()

usb.extend(ps)
usb.sort()
usb.reverse()

k = min(c, len(usb))

for i in range(k):
	price += usb.pop()

k = k + usb_k + ps_k

print(k, price)