Vitamins
Problem
Berland shop sells n kinds of juices. Each juice has its price ci. 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 n (1≤n≤1000) — the number of juices.
Each of the next n lines contains an integer ci (1≤ci≤100000) and a string si — the price of the i-th juice and the vitamins it contains. String si contains from 1 to 3 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 si. The order of letters in strings si 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=15 and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is 16, which isn’t optimal.
In the second example Petya can’t obtain all three vitamins, as no juice contains vitamin “C”.
思路:枚举所有情况,比较出最小值即可
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//A,B,C,AB,AC,BC,ABC 所有维生素组合情况(顺序无所谓)
int[] help = new int[7];
for (int i = 0; i < help.length; i++) {
help[i] = 1000000;//设定为一个非常大的数,这样计算时结果不会受影响
}
StringBuilder sb = new StringBuilder(3000);
int t = sc.nextInt();
int[] arr = new int[t];
String[] str = new String[t];
for (int i = 0; i < t; i++) {
arr[i] = sc.nextInt();
// sc.next();
str[i] = sc.nextLine().trim();
int len = str[i].length();
if(len == 1) {
com1(str[i] , help , arr[i]);
}else if(len == 2) {
com2(str[i], help,arr[i]);
}else {
com3(str[i],help,arr[i]);
}
sb.append(str[i]);
}
if(sb.indexOf("A") == -1 || sb.indexOf("B") == -1 || sb.indexOf("C") == -1) {//缺少任何一种维生素即可输出-1
System.out.println(-1);
}else {
int sum1 = help[0]+help[1]+help[2];//A+B+C
int sum2 = help[0] + help[5];//BC+A
int sum3 = help[1] + help[4];//B+AC
int sum4 = help[2] + help[3];//C+BA
int sum5 = help[3] + help[4];//AB+AC,别忘了这些情况
int sum6 = help[4] + help[5];//AC+BC
int sum7 = help[3] + help[5];//AB+BC
int sum = help[6];//ABC
int min = sum1 > sum2 ? sum2 : sum1;
min = min > sum3 ? sum3 : min;
min = min > sum4 ? sum4 : min;
min = min > sum5 ? sum5 : min;
min = min > sum6 ? sum6 : min;
min = min > sum7 ? sum7 : min;
min = min > sum ? sum : min;
System.out.println(min);
}
sc.close();
}
private static void com1(String s, int[] help , int price) {//A或B或C
if(s.equals("A")) {
if(help[0] > price) help[0] = price;
}else if(s.equals("B")) {
if(help[1] > price) help[1] = price;
}else {
if(help[2] > price) help[2] = price;
}
}
private static void com2(String s, int[] help , int price) {//长度为2
if(s.equals("AB") || s.equals("BA")) {
if(help[3] > price) help[3] = price;
}else if(s.equals("AC") || s.equals("CA")) {
if(help[4] > price) help[4] = price;
}else {
if(help[5] > price) help[5] = price;
}
}
private static void com3(String s, int[] help , int price) {//长度为3,三种维生素都有,不需要多余的判断了
if(help[6] > price) help[6] = price;
}
}