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

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83716018

题干:

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的操作啊!!

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83716018