Ninja has an array, βSβ, of βNβ words, each word consisting of lowercase English letters. An array of words is good if they are in lexicographical order. Ninja would like to make the array βSβ good. However, the only move Ninja can perform is to reverse any number of them as many times as he wants.
Let βA[i]β be the cost to reverse the word βS[i]β. Let βCβ be the minimum total cost to make the array βSβ good. Calculate the value of βCβ. If itβs impossible to make the array good, return the integer β-1β.
N = 3, A = [ 4, 8, 6 ], S = [ βtbβ, βayβ, βybβ ]
For each string, we have 2 choices, whether to reverse it or not.
For [ βtbβ, βayβ, βybβ], not sorted in lexicographical order.
For [ βtbβ, βayβ, βbyβ], not sorted in lexicographical order.
For [ βtbβ, βyaβ, βybβ], sorted in lexicographical order and the cost is 0 + 8 + 0 = 8.
For [ βtbβ, βyaβ, βbyβ], not sorted in lexicographical order.
For [ βbtβ, βayβ, βybβ], not sorted in lexicographical order.
For [ βbtβ, βayβ, βbyβ], not sorted in lexicographical order.
For [ βbtβ, βyaβ, βybβ], sorted in lexicographical order and the cost is 4 + 8 + 0 = 12.
For [ βbtβ, βyaβ, βbyβ], not sorted in lexicographical order.
Hence, the answer is 8.
The first line contains a single integer βTβ denoting the number of test cases, then the test case follows.
The first line of each test case contains a single integer, βNβ, denoting the number of strings.
The second line contains βNβ space-separated integers denoting the elements of the cost array βAβ.
The next βNβ lines contain the array βSβ elements, a single string containing lowercase English letters per line.
Output Format :
For each test case, calculate the minimum total cost to make the array βSβ good. Return the integer β-1β if itβs impossible.
Output for each test case will be printed on a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 β€ T β€ 10
1 β€ N β€ 10^5
0 β€ A[i] β€ 10^4
1 β€ β len(S[i]) β€ 10^6
Time limit: 1 sec
2
4
4 1 1 5
txmt
gyzw
rsvp
dhyj
6
5 4 4 2 3 5
sla
brog
hqt
qsp
lqhw
may
-1
13
For test case 1:
We will print -1 because there is no way to make the array good.
For test case 2:
One possible arrangement is [ βalsβ, βbrogβ, βhqtβ, βqspβ, βwhqlβ, βyamβ ], i.e., reverse the string numbers 1, 5, and 6th, then the cost is 5 + 3 + 5 = 13, which is the minimum cost possible.
2
4
5 1 5 2
wivig
lq
xjp
ep
5
3 5 5 3 5
azyjd
fm
pm
vnwwl
hryx
-1
5
For test case 1:
We will print -1 because there is no way to make the array good.
For test case 2:
We can reverse the last string with a cost of 5. It can be proved that this is the minimum possible cost. Hence, we will print 5.
For the i-th string, it only needs to be checked with βi-1β-th string assuming the strings are sorted till βi-1β-th string.
Approach:
β’ Consider a string at position βiβ. Let dp[i][0] be the minimum amount to sort the strings till position βiβ without reversing the i-th string and dp[i][1] be the minimum amount to sort the strings till position βiβ and reversing the i-th string.
β’ We have dp[i-1][0] and the dp[i-1][1], the minimum amount to sort the strings till 'i-1'-th position by reversing or not reversing the 'i-1'-th string
β’ Now, the string at position βiβ needs to be lexographically greater than the string at position βi-1β.
β’ The previous string may or may not have been reversed.
β’ So, we calculate the answer for both.
β’ The current string may or may not have to be reversed.
β’ Again we will calculate the answer for both.
β’ So, we will check for all these ways and calculate the answer for dp[i][0] and dp[i][1].
β’ The final answer will be the minimum of dp[n][0] and dp[n][1].
We only traverse the array of strings once, hence the time complexity will be O(n).
We create a dp table of size N*2, hence the space complexity will be O(n).