Time limit1000 ms

Memory limit262144 kB

题目:

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.

题意:反转字符串所需要的话费,问最小花费使得它为升序

题解dp,分dp[i][0]和dp[i][1],INF要开超级大我也是醉了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
using namespace std;
#define PI 3.14159265358979323846264338327950
#define INF 0x3f3f3f3f3f3f3f3f; const int MAX_N=;
long long int n;
string str[MAX_N],rev[MAX_N];
long long int cost[MAX_N],dp[MAX_N][]; int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%lld",&cost[i]);
for(int i=;i<=n;i++)
{
cin>>str[i];
rev[i]=str[i];
reverse(rev[i].begin(), rev[i].end()); //反转字符串
}
dp[][]=; dp[][]=cost[]; //dp[i][0]:第i个字符串不反转且使得前i个串升序的最小代价.
//dp[i][1]:第i个字符串反转且使得前i个串升序的最小代价.
for(int i=;i<=n;i++)
{
dp[i][]=dp[i][]=INF;
if(str[i]>=str[i-])
{
dp[i][]=min(dp[i][],dp[i-][]);
}
if(str[i]>=rev[i-])
{
dp[i][] = min(dp[i][], dp[i-][]);
} if(rev[i] >= str[i-])
{
dp[i][] = min(dp[i][], dp[i-][]+cost[i]);
}
if(rev[i] >= rev[i-])
{
dp[i][] = min(dp[i][], dp[i-][]+cost[i]);
} }
if(dp[n][] == 0x3f3f3f3f3f3f3f3f && dp[n][] == 0x3f3f3f3f3f3f3f3f)
printf("-1\n");
else
printf("%lld\n",min(dp[n][],dp[n][])); }
return ;
}

Hard problem CodeForces - 706C的更多相关文章

  1. Military Problem CodeForces 1006E (dfs序)

    J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...

  2. B - Save the problem! CodeForces - 867B 构造题

    B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...

  3. 【动态规划】Codeforces 706C Hard problem

    题目链接: http://codeforces.com/contest/706/problem/C 题目大意: n(2 ≤ n ≤ 100 000)个字符串(长度不超过100000),翻转费用为Ci( ...

  4. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  5. Codeforces 706C Hard problem 2016-09-28 19:47 90人阅读 评论(0) 收藏

    C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. codeforces 706C C. Hard problem(dp)

    题目链接: C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. CodeForces 706C Hard problem (水DP)

    题意:对于给定的n个字符串,可以花费a[i]  将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][max ...

  8. CodeForces 706C Hard problem

    简单$dp$. $dp[i][0]$:第$i$个串放置完毕,并且第$i$个串不反转,前$i$个串字典序呈非递减的状态下的最小费用. $dp[i][1]$:第$i$个串放置完毕,并且第$i$个串反转,前 ...

  9. CodeForces - 706C Hard problem(dp+字符串)

    题意:有n个字符串,只能将其逆转,不能交换位置,且已知逆转某字符串需要消耗的能量,问将这n个字符串按字典序从小到大排序所需消耗的最少能量. 分析:每个字符串要么逆转,要么不逆转,相邻两个字符串进行比较 ...

随机推荐

  1. 045 Jump Game II 跳跃游戏 II

    给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...

  2. c#线程倒计时器源码

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  3. A(光圈)S(快门) P(程序) M(手动)曝光模式

    摄影笔记:http://mp.weixin.qq.com/s/SCzXybbCCE8VzfAQKTqlDw 曝光模式,指的就是M档(手动曝光).A档(Av档,光圈优先).S档(Tv档,快门优先),Au ...

  4. 记一次NegativeArraySizeException

    问题描述:服务器接收后台返回的报文时,提示java.lang.NegativeArraySizeException 分析:这种异常返回的原因,一般情况下没有报文提示为返回空报文,初步分析是响应报文流长 ...

  5. ArrayList 练习题

    1点名器 import java.util.ArrayList; import java.util.Random; import java.util.Scanner; class CallName3 ...

  6. Java中protected方法访问权限的问题

    先看Test.java 此时出现上文提到的错误:The method clone from the type Object is not visiuable. 我们已经清楚Object.clone() ...

  7. 【TensorFlow入门完全指南】神经网络篇·自动编码机

    自动编码机(Autoencoder)属于非监督学习,不需要对训练样本进行标记.自动编码机(Autoencoder)由三层网络组成,其中输入层神经元数量与输出层神经元数量相等,中间层神经元数量少于输入层 ...

  8. 数据分析R&Python-Rpy2包环境配置

    Rpy2环境配置 最近想将R整合到以flask为后端框架的web系统中,在服务器端做数据统计分析.需要将R语言整合到Python中,发现Python中的Rpy2可以调用R语言,所以花了一些时间配置了一 ...

  9. 程序员的智囊库系列之2----网站框架(framework)

    程序员的智囊库系列之2--网站框架(framework) 这是程序员的智囊库系列的第二篇文章.上一篇文章讲了服务器与运维相关的工具,这篇文章我们将介绍几个搭建网站的框架: django express ...

  10. APT和它的超级牛力

    当你在使用apt时,例如“apt -h”会提示“本APT具有超级牛” 先把牛放一放,先学习以下关于APT的知识. APT 高级打包工具(英语:Advanced Packaging Tools,缩写为A ...