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. PAT 甲级 1142 Maximal Clique

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343979159552 A clique is a subset o ...

  2. 怎样使用ADO中的UpdateBatch方法(200分)

    诸位: 我在使用ADO组件(ADOQuery.ADODataSet)的BatchUpdate模式时,系统竟不认识UpdateBatch.CancelBatch方法.这是怎么回事?我的运行环境是Win2 ...

  3. Java的Bean

    Bean的定义 遵循Sun的Java Bean规范编写的特殊类 Java Bean的规范 类的访问控制权限是public 类提供有一个无参的构造函数 类的属性的访问控制权限是private,通过set ...

  4. 【Python】使用python操作mysql数据库

    这是我之前使用mysql时用到的一些库及开发的工具,这里记录下,也方便我查阅. python版本: 2.7.13 mysql版本: 5.5.36 几个python库 1.mysql-connector ...

  5. MachineLearning Exercise 4 :Neural Networks Learning

    nnCostFunction 消耗公式: a1 = [ones(m,) X]; z2 = a1*Theta1'; pre = sigmoid(a1*Theta1'); a2 = [ones(m,) p ...

  6. P3835 【模板】可持久化平衡树

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作(对于各个以往的历史版本): 插入x数 删除x数(若有多个相同的数,因只删除一个,如果没有请忽略该操作) 查询x数的 ...

  7. eclipse 重装了tomcat后配置路径

    在Windows->Preferences->Server->Runtime Environments把先前的工程Servers删除掉

  8. 虚拟主机、ECS云服务器、VPS区别汇总

    想做一个网站,但是在各种类型的服务器琳琅满目,现在总结一下市场上常见的几种服务器. 1.虚拟主机 虚拟主机就是利用虚拟化的技术,将一台服务器划分出一定大小的空间,每个空间都给予单独的 FTP 权限和 ...

  9. maven简单理解

    前言: maven项目也是一个项目,类似于javaProject,javaWebProject,就是多了些功能,其他也没啥,所以大家接触的时候不要害怕! 1 . 帮你下载jar包 maven项目会有一 ...

  10. MT【124】利用柯西求最值

    已知 \(a\) 为常数,函数\(f(x)=\dfrac{x}{\sqrt{a-x^2}-\sqrt{1-x^2}}\) 的最小值为\(-\dfrac{2}{3}\),则 \(a\) 的取值范围___ ...