Vitamins(CodeForces - 1042B )

Berland shop sells nn kinds of juices. Each juice has its price cici. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin "A", vitamin "B" and vitamin "C". Each juice can contain one, two or all three types of vitamins in it.

Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.

Input

The first line contains a single integer n (1≤n≤1000) — the number of juices.

Each of the next n lines contains an integer ci(1≤ci≤100000) and a string si — the price of the i-th juice and the vitamins it contains. String sisi contains from 1 to 3 characters, and the only possible characters are "A", "B" and "C". It is guaranteed that each letter appears no more than once in each string si. The order of letters in strings sisi is arbitrary.

Output

Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins.

Examples

Input

4
5 C
6 B
16 BAC
4 A

Output

15

Input

2
10 AB
15 BA

Output

-1

Input

5
10 A
9 BC
11 CA
4 A
5 B

Output

13

Input

6
100 A
355 BCA
150 BC
160 AC
180 B
190 CA

Output

250

Input

2
5 BA
11 CB

Output

16

Note

In the first example Petya buys the first, the second and the fourth juice. He spends 5+6+4=15 and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is 16, which isn't optimal.

In the second example Petya can't obtain all three vitamins, as no juice contains vitamin "C".

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 320007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
using namespace std;


struct qaq
{
    int s;
    char b[4];
}a[1111];
char s[7][5]={"A","B","C","AB","AC","BC","ABC"};
int sz[7]={300007,300007,300007,300007,300007,300007,300007};//单个值最大为100000
int c[4];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i].s>>a[i].b;
    for(int i=0;i<n;i++)
    {
        int l=strlen(a[i].b);
        for(int j=0;j<l;j++)
            c[a[i].b[j]-'A']++;
        sort(a[i].b,a[i].b+l);
    }
    int flag=0;
    for(int i=0;i<3;i++)//判断是否都存在
        if(c[i]==0)
            flag=1;
    if(flag)
        cout<<"-1"<<endl;
    else
    {
        for(int i=0;i<n;i++)//遍历取最小值
        {
            for(int j=0;j<7;j++)
            {
                if(strcmp(a[i].b,s[j])==0)
                    if(sz[j]>a[i].s)
                        sz[j]=a[i].s;
            }
        }
        /*for(int i=0;i<7;i++)
            cout<<sz[i]<<endl;*/
        //简便写了所有可行组合
        int k=min(sz[1]+sz[2]+sz[0],min(sz[0]+sz[5],min(sz[1]+sz[4],min(sz[2]+sz[3],min(sz[3]+sz[5],min(sz[3]+sz[4],min(sz[5]+sz[4],sz[6])))))));
        cout<<k<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值