C-hard problemTime
limit:1000MS
Memory Limit:262144KB
64bit IO Format:%i64d &%i6 4u SubmitStatusPracticecodeforces 706C
Description
Vasiliy is fond of solving different tasks. Today He found one he wasn ' t able to solve himself, so he asks the help.
Vasiliy is given N strings consisting of lowercase 中文版 letters. He wants them to being sorted in lexicographical order (as in the dictionary), but he was not allowed to swap any of them. The only operation he's allowed to do are to reverse any of the them (first character becomes last, second becomes one before Last and so on).
To reverse the i-th string Vasiliy have to spent Ci units of energy. He was interested in the minimum amount of energy he had to spent on order to had strings sorted in lexicographical order.
String A is lexicographically smaller than string b If it is shorter than B (|< C7>a| < | B|) and is it prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.
For the purpose of this problem, the other equal strings nearby do not break the condition of sequence being sorted Lexicographi Cally.
Input
The first line of the input contains a single integer n (2≤ n ≤100)-the number of Strings.
The second line contains n integers ci (0≤ ci ≤10 9), the i-th of them is equal to the amount of energy vasiliy have to spent in order to reverse the i-th string.
Then follow n lines, each containing a string consisting of lowercase 中文 letters. The total length of these strings doesn ' t exceed.
Output
If It is impossible to reverse some of the strings such that they would be located in lexicographical order, print -1 . Otherwise, print the minimum total amount of energy vasiliy have to spent.
Sample Input
Input
2
1 2
Ba
Ac
Output
1
Input
3
1 3 1
Aa
Ba
Ac
Output
1
Input
2
5 5
Bbb
Aaa
Output
-1
Input
2
3 3
Aaa
Aa
Output
-1
Hint
In the second sample one have to reverse string 2 or String 3. To amount of required to reverse the string 3 is smaller.
In the third sample, both strings don't change after reverse and they go on the wrong order, so the answer is -1.
In the fourth sample, both strings consists of characters 'a ' only, and in the sorted order string "AA" should go Before string "aaa", thus the answer is -1.
Test instructions
For a given n string, you can spend c[i] to reverse it, asking if it can be sorted into a dictionary order from large to small, and what is the minimum cost
Exercises
The Idiot DP, (however silly I will not do), DP[I][0/1] Indicates whether the first string needs to be inverted, and the cost of the minimum is how much, then XJB transfer a bit, this problem by the way let me up a bit posture, details see: String Basic Law
#include <cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Long Longc[200000],f[100005][2];intN;stringa[200000],b[2000000];intMain () {CIN>>N; for(intI=1; i<=n;i++) scanf ("%lld",&C[i]); for(intI=1; i<=n;i++) {cin>>A[i]; B[i]=A[i]; Reverse (B[i].begin (), B[i].end ()); } memset (F,-1,sizeof(f)); f[1][1]=c[1]; f[1][0]=0; for(intI=2; i<=n;i++) { if(a[i]>=a[i-1]&&f[i-1][0]!=-1) f[i][0]=f[i-1][0]; if(a[i]>=b[i-1]&&f[i-1][1]!=-1) { if(f[i][0]!=-1) f[i][0]=min (f[i][0],f[i-1][1]); Elsef[i][0]=f[i-1][1]; } if(b[i]>=a[i-1]&&f[i-1][0]!=-1) f[i][1]=f[i-1][0]+C[i]; if(b[i]>=b[i-1]&&f[i-1][1]!=-1) { if(f[i][1]!=-1) {f[i][1]=min (f[i-1][1],f[i][1]-c[i]) +C[i]; } Elsef[i][1]=f[i-1][1]+C[i]; } if(f[i][0]==-1&&f[i][1]==-1) Break; } if(f[n][0]==-1&&f[n][1]==-1) cout<<-1<<Endl; Else if(f[n][0]!=-1&&f[n][1]!=-1) Cout<<min (f[n][0],f[n][1]) <<Endl; Else if(f[n][0]==-1) cout<<f[n][1]<<Endl; Elsecout<<f[n][0]<<Endl; return 0;}
Codeforces 706C DP