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. 从I/O事件到阻塞、非阻塞、poll到epoll的理解过程

    I/O事件   I/O事件 非阻塞I/O.在了解非阻塞I/O之前,需要先了解I/O事件 我们知道,内核有缓冲区.假设有两个进程A,B,进程B想读进程A写入的东西(即进程A做写操作,B做读操作).进程A ...

  2. shell下批量除去文件名中的空格

    rename 's/ /_/g' * 上述命令可以将当前文件夹内所有文件的名字中得所有空格替换为_.其中g代表所有,如果不加g,如果文件名字中有多个空格,仅替换第一个.

  3. spring和springmvc是单例还是多例

    这么说其实不规范 spring的bean    默认是单例 springmvc的controller    默认是单例 所以最好不要在controller里定义成员变量 都可通过注解 @scope=p ...

  4. 用canvas裁剪图片

    var selectObj = null; function ImageCrop(canvasId, imageSource, x, y, width, height) { var canvas = ...

  5. 【机器学习实战】第2章 K-近邻算法(k-NearestNeighbor,KNN)

    第2章 k-近邻算法 KNN 概述 k-近邻(kNN, k-NearestNeighbor)算法主要是用来进行分类的. KNN 场景 电影可以按照题材分类,那么如何区分 动作片 和 爱情片 呢? 动作 ...

  6. Yii2.0 两次奇葩的数据库连接经历

    经历一: 公司的项目经过阿里云的ECS升级后,发现在Yii2.0框架中,凡是数据库新增的字段(当然相关的表模型肯定是加了相应字段的),老是报“属性找不到”的问题,最后排查是数据库连接的问题.把127. ...

  7. 轮播插件unslider.min.js使用demo

    有两种应用方式: 1.轮播图片作为<img>标签使用 HTML代码: <html> <head> <meta charset="utf-8" ...

  8. UVA 11134 FabledRooks 传说中的车 (问题分解)

    摘要:贪心,问题分解. 因为行列无关,所以这个二维问题可以分解成两个一维问题. 优先队列实现:类似区间点覆盖的问题,先按照左端点排序,相同然后在按右端点排序(灵活性小的优先选).最优的选法,当然是要使 ...

  9. 2018.5.14 PHP基础学习

    1.使用PHP输出HTML 使用PHP输出一个表格,并且通过style标签改变字体 <!--思考与练习--> <style type="text/css"> ...

  10. stixel-world和psmnet结合出现的问题

    float32位,4字节 原本的stixel-world是用sgbm生成深度图,并且转成了float型 psmnet保存最终的disparity图是保存成uint16的,skimage.io.imsa ...