1. 程式人生 > >【CodeForces - 706C】Hard problem(dp,字典序)

【CodeForces - 706C】Hard problem(dp,字典序)

題幹:

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.

題目大意:

題意1:給出n個字串(n<1e5,且字串總長度<1e5),可以顛倒任意一個字串但是不能交換字串的順序,顛倒每個字串都有其對應的花費ci。現需要經過一系列操作滿足字串按照字典序排列,如果能的話輸出最小花費,不能輸出-1。

題意2:現在有 n 個由小寫字母組成的字串。他想要讓這些字串按字典序排列,但是他不能交換任意兩個字串。他唯一能做的事是翻轉字串。翻轉第 i 個字串需要花費 ci 的能量。他想知道將所有字串排序最少要多少能量。兩個相鄰的字串可以相等,不一定要嚴格遞增。如果不可能有序,輸出  - 1。否則輸出最小所需的能量。

解題報告:

   其實在封裝好的string類中,是過載了>=等一系列運算子的,,而且這題不知道每個字元的長度,顯然是需要用string的。不難發現,如果兩個字串s,ss滿足字典序,s >= ss 就可以了。又因為這題不能交換字串不難想到可以dp求解、、然後亂搞一下就好了。。

AC程式碼:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 200000 + 5;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll c[MAX];
string a[MAX],b[MAX];
ll dp[MAX][2];
int main() 
{
	int n,flag = 1;
	cin>>n;
	for(int i = 1; i<=n; i++) scanf("%lld",c+i);
	for(int i = 1; i<=n; i++) {
		cin>>a[i];
		b[i] = a[i];
		reverse(b[i].begin(),b[i].end());
	}
	memset(dp,0x3f3f,sizeof dp);
	dp[1][0]=0;
	dp[1][1]=c[1];
	for(int i = 2; i<=n; i++) {
		if(a[i]>=a[i-1]) dp[i][0]=dp[i-1][0];
		if(b[i]>=a[i-1]) dp[i][1]=dp[i-1][0]+c[i];
		if(a[i]>=b[i-1]) dp[i][0]=min(dp[i][0],dp[i-1][1]);
		if(b[i]>=b[i-1]) dp[i][1]=min(dp[i][1],dp[i-1][1]+c[i]);
		if(dp[i][1]==INF && dp[i][0]==INF) {
			flag=0;break;
		}
	}
	if(flag) printf("%lld\n",min(dp[n][0],dp[n][1]));
	else printf("-1\n");
	return 0;
}

總結:  然後這題,初始化的時候用INF啊,別用-1標記非法狀態,,不然還得分一堆情況,因為你有取min的操作啊!!