C. Hard problem
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

题目的意思是给出n个字符串,要求通过逆置字符串的操作是所有字符串呈字典序,每次逆置消耗费用a[i],问最小花费

每个字符串只有两个操作,变或不变,开2维数组记录花费即可;

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
using namespace std;
#define inf 0x3f3f3f3f long long dp[100005][2];
int a[100005];
string s[100005];
int n; int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
cin>>s[i]; memset(dp,4557430888798830399,sizeof(dp));
dp[0][0]=dp[0][1]=0;
for(int i=1;i<=n;i++)
{
if(s[i]>=s[i-1])
dp[i][0]=min(dp[i][0],dp[i-1][0]);
reverse(s[i-1].begin(),s[i-1].end()); if(s[i]>=s[i-1])
dp[i][0]=min(dp[i][0],dp[i-1][1]);
reverse(s[i-1].begin(),s[i-1].end());
reverse(s[i].begin(),s[i].end()); if(s[i]>=s[i-1])
dp[i][1]=min(dp[i][1],dp[i-1][0]+a[i]); reverse(s[i-1].begin(),s[i-1].end()); if(s[i]>=s[i-1])
dp[i][1]=min(dp[i][1],dp[i-1][1]+a[i]);
reverse(s[i-1].begin(),s[i-1].end());
reverse(s[i].begin(),s[i].end());
}
if(dp[n][0]==4557430888798830399&&dp[n][1]==4557430888798830399)
printf("-1\n");
else
printf("%lld\n",min(dp[n][0],dp[n][1]));
}
return 0;
}

Codeforces 706C Hard problem 2016-09-28 19:47 90人阅读 评论(0) 收藏的更多相关文章

  1. Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...

  2. POJ3320 Jessica's Reading Problem 2017-05-25 19:55 38人阅读 评论(0) 收藏

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12346   Accep ...

  3. HDU6029 Happy Necklace 2017-05-07 19:11 45人阅读 评论(0) 收藏

    Happy Necklace                                                                           Time Limit: ...

  4. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  5. hdu 1036 (I/O routines, fgets, sscanf, %02d, rounding, atoi, strtol) 分类: hdoj 2015-06-16 19:37 32人阅读 评论(0) 收藏

    thanks to http://stackoverflow.com/questions/2144459/using-scanf-to-accept-user-input and http://sta ...

  6. Power Strings 分类: POJ 串 2015-07-31 19:05 8人阅读 评论(0) 收藏

    Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ ...

  7. 多校赛3- Painter 分类: 比赛 2015-07-29 19:58 3人阅读 评论(0) 收藏

    D - Painter Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status P ...

  8. 滑雪 分类: POJ 2015-07-23 19:48 9人阅读 评论(0) 收藏

    滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 83276 Accepted: 31159 Description Mich ...

  9. Let the Balloon Rise 分类: HDU 2015-06-19 19:11 7人阅读 评论(0) 收藏

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. TEXT 3 Food firms and fat-fighters

    TEXT 3 Food firms and fat-fighters 食品公司与减肥斗士 Feb 9th 2006 From The Economist Global Agenda Five lead ...

  2. Golang 获取MD5的方法

    import ( "crypto/md5" "encoding/hex" ) //生成32位md5字串 func Md5(s string) string { ...

  3. 2014年可用的TRACKER服务器大全

    udp://tracker.openbittorrent.com:80/announceudp://tracker.publicbt.com:80/announcehttp://pubt.net:27 ...

  4. C & C++ 宏与const

    1.宏定义函数: 例:#define do{exp} while(0)与#define exp有什么不同,好处在哪里? 定义复杂代码,防止分号,或是括号不匹配等错误.比如: 定义: #define s ...

  5. DataTable学习笔记---排序细则、列隐藏

    耽误了好几天,因为要做一个嵌入式的实验-android内核编译与裁剪,很久之前装的wubi不知道为什么运行出错了,然后看着当前的win7系统觉得有点讨厌了,也是因为快1年半没装机了,所以就重新装机了, ...

  6. oracle JDeveloper学习

    1>oracle JDeveloper官方地址,官方的学习资源包括视频和教材,很全面,很多不知道从和入手. 2>oracle JDeveloper 12C 教程,一步步学习jdev,可以此 ...

  7. DataTable的序列化和反序列化

    /// <summary> /// DataTable序列化 /// </summary> /// <param name="dt"></ ...

  8. 七大排序的个人总结(二) 归并排序(Merge

    七大排序的个人总结(二)   归并排序(Merge  归并排序(Merge Sort): 归并排序是一个相当“稳定”的算法对于其它排序算法,比如希尔排序,快速排序和堆排序而言,这些算法有所谓的最好与最 ...

  9. Laravel Tinker 使用笔记

    我们知道,Laravel Tinker 提供了命令行式的交互调试途径.使用极其方便直观. 使用: #php artisan tinker 要点: 命令要在一行上输入完成,回车执行.>>&g ...

  10. Java NIO系列教程(四) Scatter 和 Gather

    Java NIO系列教程(四) Scatter 和 Gather Java NIO 开始支持 scatter/gather,scatter/gather 用于描述从 Channel(译者注:Chann ...