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. AD中添加中文字符丝印的方法:

    一 一般中文丝印: 用快捷键L打开层管理,在View options中勾选convert special 选项: 用快捷键P,S文本中输入你要的汉字,选中ture type,在select ture ...

  2. JDBC连接中Class.forName("")到底干了什么?

    思考了一个问题,Class.forName("***");到底干了什么? 我们知道Class.forName( )静态方法的目的是为了动态加载类,但是一般来说,一个类forName ...

  3. arcgis python 保存当前窗口图形为jpg

    1,第一步打开arcgis 将图形加载进去 第二步,将要保存的图形调到合适的比例尺,然后点击下面按钮 第三步,将写好的python 语句放到里面去: import arcpy mxd = arcpy. ...

  4. P3818 小A和uim之大逃离 II

    题目背景 话说上回……还是参见 https://www.luogu.org/problem/show?pid=1373 吧 小a和uim再次来到雨林中探险.突然一阵南风吹来,一片乌云从南部天边急涌过来 ...

  5. Netty之WebSocket和四种IO介绍

    Netty简介 一.什么是netty? 高性能 事件驱动 异步非堵塞 基于NIO的客户端,服务器端编程框架 稳定性和伸缩性 二.Netty的使用场景 高性能领域   多线程并发领域   异步通信领域 ...

  6. Java开发笔记(九十七)利用Runnable启动线程

    前面介绍了线程的基本用法,按理说足够一般的场合使用了,只是每次开辟新线程,都得单独定义专门的线程类,着实开销不小.注意到新线程内部真正需要开发者重写的仅有run方法,其实就是一段代码块,分线程启动之后 ...

  7. Git操作(及操作github)

    一.git操作: 1.创建一个项目: 2.打开项目,在项目目录下右键单击--打开 git bash: 3.初始化项目仓库  创建项目仓库 命令:   git init ; git status 查看文 ...

  8. spring中自动装配bean

    首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...

  9. strust2的10种type类型

    <result-types> <result-type name="chain" class="com.opensymphony.xwork2.Acti ...

  10. /pentest/enumeration/irpas/itrace

    /pentest/enumeration/irpas/itrace 追踪防火墙内部路由