Advertisement
jbn6972

Untitled

Oct 15th, 2022
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #Code Written by : John Nixon
  2. #Date: 15:10:2022  Time: 14:24:18
  3. #Copyrights are applicable
  4. import sys
  5. import os
  6. from collections import defaultdict
  7. sys.setrecursionlimit(10000)
  8. try:
  9.     sys.stdin = open('./input.txt', 'r')
  10.     sys.stdout = open('./output.txt', 'w')
  11. except:
  12.     pass
  13.  
  14. for _ in range(int(input())):
  15.     n = int(input())
  16.     freq = defaultdict(list)
  17.    
  18.    
  19.     if n == 0:
  20.         print(0)
  21.         continue
  22.    
  23.     str = input()
  24.     costs = list(map(int,input().split()))
  25.    
  26.     for char,cost in zip(str,costs):
  27.         freq[char].append(cost)
  28.    
  29.     ans = 0    
  30.     for char in freq:
  31.         freq[char].sort()
  32.         freq[char].pop(-1)
  33.         # print(freq[char])[::-1]
  34.         ans += sum(freq[char])
  35.     print(ans)
  36.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement