Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4. 7 2
  5. culture
  6. 5 1
  7. delta
  8. 8 3
  9. accuracy
  10.  */
  11.  
  12. public class E_Doc {
  13.     public static void main(String[] args) {
  14.         Scanner in = new Scanner(System.in);
  15.         int n = in.nextInt();
  16.         int m = in.nextInt();
  17.  
  18.         Pair[] a = new Pair[m];
  19.         String s = in.next();
  20.         Set<Character> all = new HashSet<>();
  21.         for (int i = 0; i < m; i++) {
  22.             int l = in.nextInt();
  23.             int c = in.nextInt();
  24.             String soch = in.next();
  25.             Set<Character> set = new HashSet();
  26.             for (int j = 0; j < l; j++) {
  27.                 set.add(soch.charAt(j));
  28.             }
  29.             Pair p = new Pair(set, c);
  30.             a[i] = p;
  31.             all.addAll(set);
  32.  
  33.         }
  34.         StringBuilder sb = new StringBuilder();
  35.         for (int i = 0; i < n; i++) {
  36.             if (s.charAt(i) != '*') {
  37.                 sb.append(s.charAt(i));
  38.             }
  39.         }
  40.         String ans = sb.toString();
  41.         Arrays.sort(a, Comparator.comparingInt(pair -> pair.cost));
  42.  
  43.         for (int i = 0; i < n; i++) {
  44.             if (!all.contains(s.charAt(i))) {
  45.                 System.out.println(-1);
  46.                 return;
  47.             }
  48.         }
  49.  
  50.         long sum = 0;
  51.         for (int i = 0; i < n; i++) {
  52.             for (int j = 0; j < m; j++) {
  53.                 if (a[j].set.contains(s.charAt(i))) {
  54.                     sum += a[j].cost;
  55.                     break;
  56.                 }
  57.             }
  58.         }
  59.  
  60.         System.out.println(sum);
  61. //        System.out.println();
  62. //        for (int i = 0; i < m; i++) {
  63. //            System.out.println(a[i].set);
  64. //            System.out.println(a[i].cost);
  65. //            System.out.println();
  66. //        }
  67.     }
  68. }
  69.  
  70. class Pair {
  71.     Set<Character> set;
  72.     int cost;
  73.  
  74.     public Pair(Set<Character> set, int cost) {
  75.         this.set = set;
  76.         this.cost = cost;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement