Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /*
- 7 2
- culture
- 5 1
- delta
- 8 3
- accuracy
- */
- public class E_Doc {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int m = in.nextInt();
- Pair[] a = new Pair[m];
- String s = in.next();
- Set<Character> all = new HashSet<>();
- for (int i = 0; i < m; i++) {
- int l = in.nextInt();
- int c = in.nextInt();
- String soch = in.next();
- Set<Character> set = new HashSet();
- for (int j = 0; j < l; j++) {
- set.add(soch.charAt(j));
- }
- Pair p = new Pair(set, c);
- a[i] = p;
- all.addAll(set);
- }
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < n; i++) {
- if (s.charAt(i) != '*') {
- sb.append(s.charAt(i));
- }
- }
- String ans = sb.toString();
- Arrays.sort(a, Comparator.comparingInt(pair -> pair.cost));
- for (int i = 0; i < n; i++) {
- if (!all.contains(s.charAt(i))) {
- System.out.println(-1);
- return;
- }
- }
- long sum = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (a[j].set.contains(s.charAt(i))) {
- sum += a[j].cost;
- break;
- }
- }
- }
- System.out.println(sum);
- // System.out.println();
- // for (int i = 0; i < m; i++) {
- // System.out.println(a[i].set);
- // System.out.println(a[i].cost);
- // System.out.println();
- // }
- }
- }
- class Pair {
- Set<Character> set;
- int cost;
- public Pair(Set<Character> set, int cost) {
- this.set = set;
- this.cost = cost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement