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. 获取SQL Server中连接的客户端IP地址[转]

    有时候需要获取连接到SQL Server服务器上的客户端IP地址,用什么办法呢? SELECT *FROM sys.dm_exec_connections WHERE session_id = @@S ...

  2. 视频剪辑软件-PR (Adobe Premiere)

    1.PR 是什么? Adobe Premiere 是一款常用的视频编辑软件,由Adobe公司推出.PR是一款编辑画面质量较好的软件,有较好的兼容性,且可以与Adobe公司推出的其他软件相互协作.目前这 ...

  3. 微信小程序组件 分页菜单点击请求

    //JS data: { navNum:0, navList: [ { id: 1, name: '已预约' }, { id: 2, name: '已消费' }, { id: 3, name: '已取 ...

  4. ajax极简教程

    一.什么是ajax ajax即异步JavaScript和XML,它是一种用于创建快速动态网页的技术.作用是通过在后台与服务器进行少量数据交换,使网页实现异步更新.这意味着可以在不重新加载整个网页的情况 ...

  5. JavaScript的setTimeout()和setInterval()

    1. setTimeout()方法 作用:在制定的毫秒数后调用函数或计算表达式 语法: setTimeout(code,millisec) 实例: function timedMsg() { var ...

  6. 快速配置java环境变量

    右键单击计算机--->属性 点击 “高级系统设置”--->"环境变量",出现环境变量设置窗口 系统变量--->新建 JAVA_HOME变量,变量值填写jdk安装路 ...

  7. MugLife app是一款可以将静态照片变成3D动画的手机应用

    MugLife app是一款可以将静态照片变成3D动画的手机应用,如下效果图所示: 大家可以看到,这个静态图具有了类3D的动画特效,是不是很好玩? 这种算法是如何实现的呢? 这里给出一篇论文“Brin ...

  8. Android Studio 导入系统 jar包

    1.当前需要导入系统jar包的module所对应build.gradle中添加如下依赖: provided files('libs/classes-full-debug.jar') 也可以图形化设置: ...

  9. win10不能被远程解决方案(开启远程桌面,防火墙仍不能被远程解决方案)

    开启远程桌面,防火墙仍不能被远程解决方案 1.“Win+R”→“gpedit.msc” 2.依次展开“计算机配置”→“管理模版”→“系统”→“凭据分配”→找到“允许分配保存的凭据用于仅NTLM服务器身 ...

  10. 深度学习网络层之 Pooling

    pooling 是仿照人的视觉系统进行降维(降采样),用更高层的抽象表示图像特征,这一部分内容从Hubel&wiesel视觉神经研究到Fukushima提出,再到LeCun的LeNet5首次采 ...