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. 《深入理解java虚拟机》笔记(4)对象已死吗

    一.垃圾回收器回收的对象 虚拟机内存区域中程序计数器.虚拟机栈.本地方法栈随线程而生,随线程而灭.这3个区域内存分配和回收都具备确定性.因此不需要过多考虑回收问题. 而Java堆和方法区不一样,这部分 ...

  2. JAVA 框架之面向对象设计原则

     面向对象设计原则:  单一职责原则 SRP :   一个类或者行为只做一件事 .  降低代码冗余,提高可重用性,可维护性,可扩展性,可读性 使用组合形式   里氏替换原则 LSP :  所有引用基类 ...

  3. Spring 设计原则

    Spring 框架有四大原则(Spring所有的功能和设计和实现都基于四大原则): 1. 使用POJO进行轻量级和最小侵入式开发. 2. 通过依赖注入和基本接口编程实现松耦合. 3. 通过AOP和基于 ...

  4. Ubuntu 自动获取ip地址

    $ sudo dhclient -r               //release ip 释放IP$ sudo dhclient                  //获取IP手動使用 DHCP 自 ...

  5. (一)我的Javascript系列:Javascript的面向对象旅程(上)

    今宵酒醒何处,杨柳岸,晓风残月 导引 我的JavaScript系列文章是我自己对JavaScript语言的感悟所撰写的系列文章.现在还没有写完.目前一共出了下面的系列: (三)我的JavaScript ...

  6. [学习总结] python语言学习总结 (三)

    函数闭包 定义 延伸了作用域的函数(能访问定义体之外定义的非全局变量 作用 共享变量的时候避免使用了不安全的全局变量 允许将函数与某些数据关联起来,类似于简化版面向对象编程 相同代码每次生成的闭包,其 ...

  7. mysql命令行导出导入,附加数据库

    MySQL命令行导出数据库:1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server ...

  8. Hbase 完全分布式 高可用 集群搭建

    1.准备 Hadoop 版本:2.7.7 ZooKeeper 版本:3.4.14 Hbase 版本:2.0.5 四台主机: s0, s1, s2, s3 搭建目标如下: HMaster:s0,s1(备 ...

  9. Python——字典dict()详解

    一.字典 字典是Python提供的一种数据类型,用于存放有映射关系的数据,字典相当于两组数据,其中一组是key,是关键数据(程序对字典的操作都是基于key),另一组数据是value,可以通过key来进 ...

  10. springboot 内置tomcat maxPostSizs 无法设置

    ++++++++++++++++++ RT +++++++++++++++++ 如下代码方可解决: /** * tomcat配置类 * 解决post数据体大于2048kb无法接收问题 * 解决tomc ...