Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.
Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).
To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.
String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its 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, two equal strings nearby do not break the condition of sequence being sorted lexicographically.
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers ci (0 ≤ ci ≤ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string.
Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed100 000.
If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.
2 1 2 ba ac
1
3 1 3 1 aa ba ac
1
2 5 5 bbb aaa
-1
2 3 3 aaa aa
-1
In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.
In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is - 1.
In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is - 1.
题意:给定n个字符串,翻转第i个需要vi的代价,问你使这些字符穿按字典序排序,最少代价是多少。
题解:
简单的DP,DP[i][0]表示不翻转第i个的代价,DP[i][1]表示翻转第i个的代价。
//************************************************************************//
//*Author : Handsome How *//
//************************************************************************//
//#pragma comment(linker, "/STA CK:1024000000,1024000000")
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define foreach(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define fur(i,a,b) for(int i=(a);i<=(b);i++)
#define furr(i,a,b) for(int i=(a);i>=(b);i--)
#define cl(a) memset((a),0,sizeof(a))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#ifdef HandsomeHow
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define dbg(x) cout << #x << " = " << x << endl
#else
#define debug(...)
#define dbg(x)
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
const ll inf=(long long)(1e18);
const double eps=1e-8;
const int mod=1000000007;
const double pi=acos(-1);
inline void gn(long long&x){
int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
ll gcd(ll a,ll b){return a? gcd(b%a,a):b;}
ll powmod(ll a,ll x,ll mod){ll t=1ll;while(x){if(x&1)t=t*a%mod;a=a*a%mod;x>>=1;}return t;}
// (づ°ω°)づe★
//-----------------------------------------------------------------
const int maxn = 111111;
string ori[maxn],rev[maxn];
ll dp[maxn][2];
ll v[maxn];
int main(){
#ifdef HandsomeHow
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
time_t beginttt = clock();
#endif
int n;
gn(n);
fur(i,1,n) gn(v[i]);
fur(i,1,n){
cin>>ori[i];
rev[i] = ori[i];
reverse(rev[i].begin(),rev[i].end());
}
fur(i,1,n)fur(j,0,1)dp[i][j] = inf;
dp[1][0] = 0;
dp[1][1] = v[1];
fur(i,2,n){
/*
if(dp[i-1][0] != inf){
if(ori[i]>ori[i-1]) dp[i][0] = min(dp[i][0],dp[i-1][0]);
if(rev[i]>ori[i-1]) dp[i][1] = min(dp[i][1],dp[i-1][0]+v[i]);
}
if(dp[i-1][1] != inf){
if(ori[i]>rev[i-1]) dp[i][0] = min(dp[i][0],dp[i-1][1]);
if(rev[i]>rev[i-1]) dp[i][1] = min(dp[i][1],dp[i-1][1]+v[i]);
}*/
if(ori[i]>=ori[i-1]) dp[i][0] = min(dp[i][0],dp[i-1][0]);
if(ori[i]>=rev[i-1]) dp[i][0] = min(dp[i][0],dp[i-1][1]);
if(rev[i]>=ori[i-1]) dp[i][1] = min(dp[i][1],dp[i-1][0]+v[i]);
if(rev[i]>=rev[i-1]) dp[i][1] = min(dp[i][1],dp[i-1][1]+v[i]);
dp[i][0] = min(inf,dp[i][0]);
dp[i][1] = min(inf,dp[i][1]);
}
ll ans = min(dp[n][0],dp[n][1]);
if(ans == inf) cout<<-1<<endl;
else cout<<ans<<endl;
#ifdef HandsomeHow
time_t endttt = clock();
debug("time: %d\n",(int)(endttt - beginttt));
#endif
return 0;
}