题目链接:

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
题意:
 
问把这些字符串按字典序排好,如果第i个需要倒转,花费为a[i],现在求最小花费;
 
思路:
 
dp[i][j]表示第i个串的状态为j时的最小花费,j==0表示不倒转,j==1表示倒转;转移方程看代码;
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <bits/stdc++.h>
using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=998244353;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+10;
const int maxn=1e3+10;
const double eps=1e-4; int n;
LL a[N],dp[N][2];
string s[N]; int main()
{
read(n);
For(i,1,n)read(a[i]);
For(i,0,n)dp[i][0]=dp[i][1]=inf;
For(i,1,n)
{
cin>>s[i];
if(i==1){dp[i][0]=0,dp[i][1]=a[1];continue;}
if(s[i]>=s[i-1])dp[i][0]=min(dp[i][0],dp[i-1][0]);
string temp="";
int len=s[i-1].length();
for(int j=len-1;j>=0;j--)
{
temp=temp+s[i-1][j];
}
if(s[i]>=temp)dp[i][0]=min(dp[i][0],dp[i-1][1]);
string t="";
len=s[i].length();
for(int j=len-1;j>=0;j--)t=t+s[i][j];
if(t>=s[i-1])dp[i][1]=min(dp[i][1],dp[i-1][0]+a[i]);
if(t>=temp)dp[i][1]=min(dp[i][1],dp[i-1][1]+a[i]);
}
LL ans=min(dp[n][0],dp[n][1]);
if(ans==inf)cout<<"-1";
else cout<<ans;
return 0;
}

  

最新文章

  1. CSS浮动、定位
  2. linuxqq
  3. 有用的MySQL语句
  4. 最大后验估计(MAP)
  5. 汇总:Linux下svn命令大全
  6. CSS3 :nth-child() 选择器
  7. SimpleDateFormat线程不安全问题处理
  8. UML01-概述
  9. [SignalR]异常信息捕获以及处理
  10. 7-days-asp-dotnet-mvc-day1
  11. Tyvj P3276
  12. STM32驱动DHT11温湿度传感器
  13. Codeforces 802L Send the Fool Further! (hard)
  14. 深入理解springAOP切面的特性
  15. css块居中
  16. 使用electron开发指静脉客户端遇到的问题总结
  17. CAS (15) — CAS 线上环境 Ehcache Replication 的非稳定重现错误 java.util.ConcurrentModificationException
  18. Locust 分布式测试
  19. Android ViewPager设置监听注意事项
  20. 13 Calculating Expected Offspring

热门文章

  1. SQL EXISTS 与 IN
  2. Deploying Docker images via SSH
  3. uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型 - 大总结
  4. Hibernate学习一----------Hibernate初实现
  5. eclipse adt开发android ndk没有NDK选项问题的解决方案
  6. 迁移EXT4
  7. WARN util.NativeCodeLoader: Unable to load native-hadoop l... using builtin-java classes where applicable(附编译脚本)
  8. HDFS源码分析心跳汇报之数据结构初始化
  9. WebStorm 常用功能
  10. JavaScript -- JavaScript高级程序设计