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


只有ABC三位,如果按位或是不错的选择

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include<unordered_set>
#define ll long long
using namespace std;
int dir[][] = { {,},{-,},{,},{,-} }; int main()
{
map<string,int> mp;
int n;
cin >> n;
vector<int> dp(,1e9);
dp[] = ;
while (n--)
{
int cost;
string vit;
cin >> cost >> vit;
int m = ;
for (int i = ; i < vit.size(); i++)
m |= << (vit[i] - 'A');
for (int i = ; i < ; i++)
{
dp[i | m] = min(dp[i | m], dp[i] + cost);
}
}
if (dp[] == 1e9)
dp[] = -;
cout << dp[];
//system("pause");
return ;
}

1042B. Vitamins的更多相关文章

  1. CodeForces - 1042B

    Berland shop sells nn kinds of juices. Each juice has its price cici. Each juice includes some set o ...

  2. Codeforces Round #510 (Div. 2) B. Vitamins

    B. Vitamins 题目链接:https://codeforces.com/contest/1042/problem/B 题意: 给出几种药,没种可能包含一种或多种(最多三种)维生素,现在问要吃到 ...

  3. Gym - 101243F Vitamins(思维+并查集)

    题意 有三种药丸,白色W>红色R>蓝色B,给你m个约束条件,问你n个药丸的颜色,不能确定颜色输出‘?’ 题解 如果1<2<3,只要找到2就能确定1和3的颜色 如果2=4,只要确 ...

  4. 【枚举】【并查集】Gym - 101243F - Vitamins

    题意:有n片药,有三种颜色,白色比红色重,红色比蓝色重,给你一些它们之间的重量关系,比如1>3,2=4之类,问你它们的颜色,如果没法判断的输出?. 先并查集把等于号全缩起来,然后按照大于号建图, ...

  5. CF1042B 【Vitamins】(去重,状压搜索)

    由题意,我们其实会发现 对于每一种果汁,其对应的状态只有可能有7种 VA​ VB​ VC​ VA+VB​ VA+VC​ VB+VC​ VA+VB+VC 这道题就大大简化了 我们把所有果汁都读进来 每种 ...

  6. 2018SDIBT_国庆个人第三场

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

  7. 《利用python进行数据分析》读书笔记--第七章 数据规整化:清理、转换、合并、重塑(三)

    http://www.cnblogs.com/batteryhp/p/5046433.html 5.示例:usda食品数据库 下面是一个具体的例子,书中最重要的就是例子. #-*- encoding: ...

  8. HTML & CSS 小总结

    1. web 主机代理商 web hosting company, 让他们的服务器为你的页面服务2. 选择网站名字 例如: www.1234.com3. 寻找 把文件从电脑传到主机的途径4. 把新网站 ...

  9. Diet

    Dialogue 1   Healthy diet 关于健康饮食 F:Bob, look at this sentence. 'Healthy eating is not about strict n ...

随机推荐

  1. Xshell 5

    Xshell 5安装步骤(带安装包):http://www.cnblogs.com/ysocean/p/7702243.html

  2. vue使用饿了么element-ui框架中的上传组件进度条无法使用,:on-progress钩子无法触发的原因

    自己写的例子都是好好的,调试了半天,在项目里怎么都出不来 最终终于找到原因: 在上传文件时要做进度显示需要用到xhr.upload.onprogress事件,此时如果你的项目里用到mock.js模拟数 ...

  3. C语言 删除排序链表中的重复元素

    给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2示例 2: 输入: 1->1->2->3-> ...

  4. AntDesign(React)学习-14 使用UMI提供的antd模板

    1.UMI提供了可视化antd模板,可以直接添加到项目中修改用 比如将个人中心添加到项目中 2.选择个人中心,确定 3.成功 4.打开项目 5.Route文件也自动添加 根路由有exact:true后 ...

  5. javascript当中静态方法和prototype用法

    6)静态方法和prototype(难) 例 3.6.1 <head>    <meta http-equiv="content-type" content=&qu ...

  6. rest_framework:解析器

    一.解析器的作用 根据请求头content-type选择对应的解析器对请求体内容进行处理. 有application/json,x-www-form-urlencoded,form-data等格式 二 ...

  7. VB断点拷贝大文件(WIN7系统需要更改某个API函数,具体我也忘了)

    小弟以前租碟在电脑上看VCD,有时候拷贝经典的影片到硬盘上可惜碰到比较粗糙的碟子就很难拷贝过去,因此编了个断点拷贝文件的程序.本程序用于拷贝大文件,并可在旧文件上接着拷贝本程序能在无法读取数据的情况下 ...

  8. 动图演示23个鲜为人知的VSCode快捷键

    动图演示23个鲜为人知的VSCode快捷键 原文地址:dev.to/devmount/23… 代码同步浏览器 安装vccode 安装live server插件 尽管我在VS Code中经常使用许多快捷 ...

  9. 【转】常见的hash算法及其原理

    Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是 ...

  10. 小匠第一周期打卡笔记-Task02

    一.文本预处理 预处理通常包括四个步骤: 读入文本 分词 建立字典,将每个词映射到一个唯一的索引(index) 将文本从词的序列转换为索引的序列,方便输入模型 读入文本: import collect ...