B. USB vs. PS/2(结构体排序)

来源:互联网 发布:淘宝助理无法打印 编辑:程序博客网 时间:2024/06/05 06:20

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 ab and c (0 ≤ a, b, c ≤ 105)  — 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·105)  — the number of mouses in the price list.

The next m lines each describe another mouse. The i-th line contains first integer vali (1 ≤ vali ≤ 109)  — 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 145 USB6 PS/23 PS/27 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.


题意:用最少的钱买配套的鼠标,最多买几个,最少花多少钱。

思路:结构体排序,价格从小到大,依次进行加法运算。

代码:

#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<cmath>#include<algorithm>using namespace std;typedef long long LL;struct pp{    int v;    char t[10];}s[300000+10];bool cmp(pp a,pp b){    return a.v<b.v;}int main(){    int a,b,c;    scanf("%d%d%d",&a,&b,&c);    int m;    scanf("%d",&m);    memset(s,0,sizeof(s));    for(int i=0;i<m;i++)        scanf("%d %s",&s[i].v,s[i].t);    sort(s,s+m,cmp);    int cout1=0,cout2=0,cout3=0;    LL w1=0,w2=0,w3=0;    for(int i=0;i<m;i++)    {        if(s[i].t[0]=='U'&&cout1!=a)        {            cout1++;            w1+=s[i].v;        }        else if(s[i].t[0]=='P'&&cout2!=b)        {            cout2++;            w2+=s[i].v;        }        else if(cout3!=c)        {            cout3++;            w3+=s[i].v;        }    }   printf("%d %I64d\n",cout1+cout2+cout3,w1+w2+w3);}


0 0
原创粉丝点击