AlgoUniversity
  • Go Back
Discussion
Ray And Strings :

Author

Shikhar Mehrotra

Difficulty Level : Easy

Submissions : 382

Asked In : Palo-Alto-Networks

Marks :10

: 3 | : 0

Ray is playing with strings and he came up with a very weird question.

He gave you string $$$str$$$ of size $$$N$$$ having lowercase English alphabets and an array $$$a$$$ of size $$$N$$$ which contains $$$N$$$ non-negative integer value for every character in the string $$$str$$$.

Your task is to delete characters from the string following the given instructions.

Delete exactly one occurence of every character present in the string.

for e.g. str="aabba" and array is $$$[2,3,1,2,3]]$$$ so frequency of $$$'a'$$$ is 3 and $$$'b'$$$ is 2 let's say you deleted 0th index i.e $$$'a'$$$ and 2nd index i.e $$$'b'$$$ resulting in updated array as $$$[0,3,0,2,3]$$$.

Note here

1.Deletion means updating value of character as $$$0$$$ . You have to delete the characters in such a manner after all deletions remaining sum is minimum.

2.The value of every undeleted character remains same.

Input

The first line contain $$$1 \le T \le 10$$$ number of test cases.

For every test case.

  • The first line contains $$$ 1\le N \le 10^{5}$$$ i.e size of the string.
  • The second line contains a string consisting of lowercase English alphabets of size $$$N$$$ where $$$'a' \le str[i] \le 'z'$$$ where $$$(0\le i <n)$$$
  • The third line contains array $$$value$$$ of $$$N$$$ space separated non negative integer $$$(0\le value_i \le10^{9})$$$ value as mentioned in the question.

Output

For every test case output the minimum sum of the array you get in a new line.

Example

Input
2
5
aabba
2 3 1 2 3
6
aaabbb
5 7 2 4 8 10
Output
6
19

Note

You have to delete exactly one occurence of every character in the string.

In first test case it would be optimal to remove(0-based indexing) 1st character i.e $$$'a'$$$ and 3rd character i.e $$$'b'$$$ resulting array would be $$$[2,0,1,0,3]$$$ . which has minimum sum = $$$6$$$.

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.