B. Vitamins

B. Vitamins

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn (1≤n≤1000)(1≤n≤1000) — the number of juices.

Each of the next nn lines contains an integer cici (1≤ci≤100000)(1≤ci≤100000) and a string sisi — the price of the ii-th juice and the vitamins it contains. String sisi contains from 11 to 33 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 sisi. 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

Copy

4
5 C
6 B
16 BAC
4 A

output

Copy

15

input

Copy

2
10 AB
15 BA

output

Copy

-1

input

Copy

5
10 A
9 BC
11 CA
4 A
5 B

output

Copy

13

input

Copy

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

output

Copy

250

input

Copy

2
5 BA
11 CB

output

Copy

16

Note

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

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

AC代码(题目的意思是每一个字符串中abc最多出现一次所以字符串最长就是3暴力就好)

#include <iostream>
#include <bits/stdc++.h>
#define maxn 300050

using namespace std;

int main()
{
    int a = maxn, b = maxn, c = maxn, ab = maxn, ac = maxn, bc = maxn, abc = maxn, mini = maxn;
    char s[10000+10];
    int n, i, x;
    scanf("%d",&n);
    for(i = 0;i<n;i++)
    {
        cin>>x>>s;
        if(strcmp(s,"A")==0) a = min(a, x);
        if(strcmp(s,"B")==0) b = min(b, x);
        if(strcmp(s,"C")==0) c = min(c, x);
        if(strcmp(s,"AB")==0||strcmp(s,"BA")==0) ab = min(ab, x);
        if(strcmp(s,"AC")==0||strcmp(s,"CA")==0) ac = min(ac, x);
        if(strcmp(s, "BC")==0||strcmp(s, "CB")==0) bc = min(bc, x);
        int len = strlen(s);
        if(len>=3) abc = min(x, abc);
    }
     mini = min(mini, abc);
    mini = min(mini,a+b+c);
    mini = min(mini, ab+c);
    mini = min(mini, ac+b);
    mini = min(mini, bc+a);
    mini = min(mini, ab+ac);
    mini = min(mini, ab+bc);
    mini = min(mini, ac+bc);
    mini = min(mini, ab+ac+bc);
    if(mini>=maxn) printf("-1\n");
    else printf("%d\n",mini);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值