浙江财经大学
信息管理与工程学院

Codeforces Round #367 (Div. 2).C – Hard problem

本文由 Ocrosoft 于 2016-08-27 15:23:42 发表

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 exceed100 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.

Solution

题意:给出一些字符串,通过反转其中的一些字符串,使这些字符串按照字典序升序排列,但字符串本身不需要字典序。

思路:用dp[i][0]表示第i个字符串不反转,dp[i][1]表示字符串反转。状态转移如下:
dp[i][0] = min(dp[i][0], dp[i – 1][0]);
dp[i][0] = min(dp[i][0], dp[i – 1][1]);
dp[i][1] = min(dp[i][1], dp[i – 1][0] + v[i]);
dp[i][1] = min(dp[i][1], dp[i – 1][1] + v[i]);

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define ms(a) memset(a,0,sizeof(a))
typedef long long ll;
const ll INF = LLONG_MAX / 2;
const int MAXN = 100000 + 10;
const int MOD = 100000;
using namespace std;
string s1[MAXN], s2[MAXN];//保存原字符串、反转字符串
ll v[MAXN], dp[MAXN][3];
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)scanf("%I64d", &v[i]);
	for (int i = 0; i < MAXN; i++)dp[i][0] = dp[i][1] = INF;
	for (int i = 0; i < n; i++)
	{
		cin >> s1[i];
		s2[i] = s1[i];
		reverse(s2[i].begin(), s2[i].end());
	}
	dp[0][0] = 0;
	dp[0][1] = v[0];
	for (int i = 1; i < n; i++)
	{
		if (s1[i] >= s1[i - 1])dp[i][0] = min(dp[i][0], dp[i - 1][0]);//两行都不反转
		if (s1[i] >= s2[i - 1])dp[i][0] = min(dp[i][0], dp[i - 1][1]);//上一行反转
		if (s2[i] >= s1[i - 1])dp[i][1] = min(dp[i][1], dp[i - 1][0] + v[i]);//这一行反转,上一行不反转
		if (s2[i] >= s2[i - 1])dp[i][1] = min(dp[i][1], dp[i - 1][1] + v[i]);//两行都反转
	}
	ll ans = min(dp[n - 1][0], dp[n - 1][1]);
	printf("%I64d\n", ans == INF ? (ll)-1 : ans);
	return 0;
}

欢迎分享与转载,请保留链接与出处。Ocrosoft » Codeforces Round #367 (Div. 2).C – Hard problem

点赞 (1)or拍砖 (0)

评论 2

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    其实 我们等你更新游戏王呢

    2335个月前 (08-29)回复
    • _(:зゝ∠)_
      更新了,但是主页是看不到的…要到目录去看。
      公安局在备案,主页暂时就不显示了。

      Ocrosoft5个月前 (08-30)回复