Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. f = input().split(' ')
  2. n, m = int(f[0]), int(f[1])
  3.  
  4. s = input()
  5. allSet = set()
  6. a = []
  7. for i in range(m):
  8.     f = input().split(' ')
  9.     l, c = int(f[0]), int(f[1])
  10.     word = input()
  11.     cur = set(word)
  12.     pair = (cur, c)
  13.     a.append(pair)
  14.     allSet = allSet.union(cur)
  15.  
  16. a = sorted(a, key=lambda x: x[1], reverse=False)
  17.  
  18. needSet = set(s)
  19. # print(needSet)
  20. # print(allSet)
  21. if '*' in needSet and len(needSet) == 1:
  22.     print(0)
  23.     exit(0)
  24.  
  25. if not needSet.issubset(allSet):
  26.     print(-1)
  27.     exit(0)
  28.  
  29. k = 0
  30. for ch in s:
  31.     if ch != '*':
  32.         for p in a:
  33.             if ch in p[0]:
  34.                 k += p[1]
  35.  
  36. print(k)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement