C. Hard problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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 exceed 100 000.
Output
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.
Examples
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
Note
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个字符串,然后这个字符串可以完全翻转,且给出花费,求将这些字符串按字典序排列最少需要的花费
思路:用
ac代码:
/* ***********************************************
Author : AnICoo1
Created Time : 2016-08-20-15.35 Saturday
File Name : D:\MyCode\2016-8月\2016-8-20-2.cpp
LANGUAGE : C++
Copyright 2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define inf 0x3f3f3f3f
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define gn (sqrt(5.0)+1)/2
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
string str[MAXN][2];
ll dp[MAXN][2];
ll a[MAXN];
ll ans[MAXN];
int main()
{
int n;cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++)
{
cin>>str[i][1];
str[i][0]=str[i][1];
reverse(str[i][1].begin(),str[i][1].end());
}
mem(dp,-1);
dp[1][0]=0;dp[1][1]=a[1];
int flag=0;
for(int i=2;i<=n;i++)
{
//四种情况:1.前面不翻转,当前不翻转 2.前面翻转,当前不翻转
//3.前面不翻转,当前翻转 4.前面翻转,当前翻转
if(str[i1][0]>=str[i-1][0]&&dp[i-1][0]!=-1)
{
if(dp[i][0]==-1)
dp[i][0]=dp[i-1][0];
else
dp[i][0]=min(dp[i][0],dp[i-1][0]);
}
if(str[i][0]>=str[i-1][1]&&dp[i-1][1]!=-1)
{
if(dp[i][0]==-1)
dp[i][0]=dp[i-1][1];
else
dp[i][0]=min(dp[i][0],dp[i-1][1]);
}
if(str[i][1]>=str[i-1][0]&&dp[i-1][0]!=-1)
{
if(dp[i][1]==-1)
dp[i][1]=dp[i-1][0]+a[i];
else
dp[i][1]=min(dp[i][1],dp[i-1][0]+a[i]);
}
if(str[i][1]>=str[i-1][1]&&dp[i-1][1]!=-1)
{
if(dp[i][1]==-1)
dp[i][1]=dp[i-1][1]+a[i];
else
dp[i][1]=min(dp[i][1],dp[i-1][1]+a[i]);
}
if(dp[i][0]==-1&&dp[i][1]==-1)
{flag=1;break;}
if(dp[i][0]==-1)
ans[i]=dp[i][1];
else if(dp[i][1]==-1)
ans[i]=dp[i][0];
else
ans[i]=min(dp[i][0],dp[i][1]);
}
if(flag)
printf("-1\n");
else
printf("%I64d\n",ans[n]);
return 0;
}