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
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=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".

你猜我的代码错在哪了,找找看?

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; int main()
{
char kind[];
LL i,p,j;
LL n,x;
LL a,b,c,ab,ac,bc,abc;
LL flag=;
a=b=c=ab=ac=bc=abc=;
scanf("%lld",&n);
for(i=;i<=n;i++)
{
scanf("%lld %s",&x,kind);
if((strcmp(kind,"BA")==)||(strcmp(kind,"AB")==))
{
if(x<ab)
ab=x;
}
else if((strcmp(kind,"AC")==)||(strcmp(kind,"CA")==))
{
if(x<ac)
ac=x;
}
else if((strcmp(kind,"BC")==)||(strcmp(kind,"CB")==))
{
if(x<bc)
bc=x;
}
else if(kind[]=='A')
{
flag++;
if(x<a)
a=x;
}
else if(kind[]=='B')
{
flag++;
if(x<b)
b=x;
}
else if(kind[]=='C')
{
flag++;
if(x<c)
c=x;
}
else
{
if(x<abc)
abc=x;
}
kind[]=;
} LL head=;
if(a<&&b<&&c<)
head=a+b+c;
if(bc<&&a<&&(a+bc<head))
head=a+bc;
if(abc<&&a<&&(a+abc<head))
head=a+abc;
if(abc<&&b<&&(b+abc<head))
head=b+abc;
if(abc<&&c<&&(c+abc<head))
head=c+abc;
if(ac<&&b<&&(b+ac<head))
head=b+ac;
if(ab<&&c<&&(c+ab<head))
head=c+ab;
if(ac<&&ab<&&(ac+ab<head))
head=ac+ab;
if(ab<&&bc<&&(ab+bc<head))
head=ab+bc;
if(ac<&&bc<&&(ac+bc<head))
head=ac+bc;
if(abc<&&abc<head)
head=abc; if(head!=)
printf("%lld\n",head);
else
printf("-1\n");
return ; }

if else if else if if if .......因为这种问题错过几遍了,心里没有点Number  B 吗?!!

明明"ABC"的串还没有比过,你就直接比较kind[0]???  脑子呢???

写if else 的时候能不能心里想着逻辑点,能不能走点心?

正确代码:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; int main()
{
char kind[];
LL i,p,j;
LL n,x;
LL a,b,c,ab,ac,bc,abc;
LL flag=;
a=b=c=ab=ac=bc=abc=;
scanf("%lld",&n);
for(i=;i<=n;i++)
{
scanf("%lld %s",&x,kind);
if((strcmp(kind,"BA")==)||(strcmp(kind,"AB")==))
{
if(x<ab)
ab=x;
}
else if((strcmp(kind,"AC")==)||(strcmp(kind,"CA")==))
{
if(x<ac)
ac=x;
}
else if((strcmp(kind,"BC")==)||(strcmp(kind,"CB")==))
{
if(x<bc)
bc=x;
}
else if(strcmp(kind,"A")==)
{
flag++;
if(x<a)
a=x;
}
else if(strcmp(kind,"B")==)
{
flag++;
if(x<b)
b=x;
}
else if(strcmp(kind,"C")==)
{
flag++;
if(x<c)
c=x;
}
else
{
if(x<abc)
abc=x;
}
kind[]=;
} LL head=;
if(a<&&b<&&c<)
head=a+b+c;
if(bc<&&a<&&(a+bc<head))
head=a+bc;
if(abc<&&a<&&(a+abc<head))
head=a+abc;
if(abc<&&b<&&(b+abc<head))
head=b+abc;
if(abc<&&c<&&(c+abc<head))
head=c+abc;
if(ac<&&b<&&(b+ac<head))
head=b+ac;
if(ab<&&c<&&(c+ab<head))
head=c+ab;
if(ac<&&ab<&&(ac+ab<head))
head=ac+ab;
if(ab<&&bc<&&(ab+bc<head))
head=ab+bc;
if(ac<&&bc<&&(ac+bc<head))
head=ac+bc;
if(abc<&&abc<head)
head=abc; if(head!=)
printf("%lld\n",head);
else
printf("-1\n");
return ; }

CodeForces - 1042B的更多相关文章

  1. 2018SDIBT_国庆个人第三场

    A - A CodeForces - 1042A There are nn benches in the Berland Central park. It is known that aiai peo ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. python杂谈:Python中\r的用法示例

    \r 默认表示将输出的内容返回到第一个指针,这样的话,后面的内容会覆盖前面的内容 import sys import time def view_bar(num,total): rate = floa ...

  2. Asp.Net Core实现文件上传

    1. Asp.Net Core Mvc方式 public class UploadController : Controller { private IHostingEnvironment _host ...

  3. 跳转不同包时候 需要先指定该包的namespace 注意 先跳转 即加上/

  4. Vue使用SCSS进行模块化开发

    原文地址:http://www.cnblogs.com/JimmyBright/p/7761531.html 个人认为scss最大的好处就是能将css属性设置为变量,这样让css一键更换主题成为可能. ...

  5. BZOJ 2732: [HNOI2012]射箭

    2732: [HNOI2012]射箭 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2532  Solved: 849[Submit][Status] ...

  6. C++模板源代码的三种组织方式

    模板代码和非模板代码是有区别的,如果像非模板代码那样把模板的声明放在头文件.h中,把模板的定义放在源文件.cpp中,那么使用这个模板时会得到一个链接错误.这个错误的原因在于,模板的定义还没有被实例化. ...

  7. 【Cf #502 H】The Films(莫队)

    题面的简述:总共有$m$种书,书架上共有$n$本书,给出$n$本书的种类,并有$Q$个询问,每次询问给出$l, r, k$.每次询问时都会先出现$k * m$本书,每种书各$k$本,然后再加入书架上的 ...

  8. Centos Python3安装共存

    安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlit ...

  9. 什么是MySQL

    数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据存储在文件中,但是在文件中读 ...

  10. bootstrap.yml与application.yml的区别

    说明:其实yml和properties文件是一样的原理,主要是说明application和bootstrap的加载顺序.且一个项目上要么yml或者properties,二选一的存在. Bootstra ...