题目链接:http://codeforces.com/contest/706/problem/C

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+3;
const ll INF=1e18;
ll dp[N][2];
string a[N],b[N];
int c[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",c+i);
for(int i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
reverse(b[i].begin(),b[i].end());
}
for(int i=1;i<n;i++)
dp[i][0]=dp[i][1]=INF;
dp[0][1]=c[0];
int i;
for(i=1;i<n;i++)
{
if(a[i]>=a[i-1])
dp[i][0]=dp[i-1][0];
if(b[i]>=a[i-1])
dp[i][1]=dp[i-1][0]+c[i];
if(a[i]>=b[i-1])
dp[i][0]=min(dp[i][0],dp[i-1][1]);
if(b[i]>=b[i-1])
dp[i][1]=min(dp[i][1],dp[i-1][1]+c[i]);
if(dp[i][0]==INF&&dp[i][1]==INF)
break;
}
if(i==n)
printf("%I64d\n",min(dp[n-1][0],dp[n-1][1]));
else
printf("-1\n");
return 0;
}

codeforces round367 div2.C (DP)的更多相关文章

  1. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

  2. codeforces 369 div2 C dp

    http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per t ...

  3. CodeForces #363 div2 Vacations DP

    题目链接:C. Vacations 题意:现在有n天的假期,对于第i天有四种情况: 0  gym没开,contest没开 1  gym没开,contest开了 2 gym开了,contest没开 3 ...

  4. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  5. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  6. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  7. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  8. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  9. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

随机推荐

  1. 网络编程之socket(转)

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价 值,那网络中进程之间如何通信,如我们每天打开浏 ...

  2. 初学java之异常类

    //异常类 package st; public class example_1 { public static void main(String args[]) { int n=0,m=0,t=10 ...

  3. Git删除远程分支

    查看 git branch -a 删除远程分支 git branch -r -d origin/branch-name  //只是删除的本地对该远程分支的track git push origin : ...

  4. 如何在win7下配置IIS?

  5. HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置

    答案:在将WebDataHelper升级到VS2013是出现的这个错误,这个程序使用了URL重写的技术, 解决方法是:需要将重写的配置,迁移到system.webServer配置节中

  6. freemarker实例2

    下面演示一个简单的使用项目过程:1. 使用myeclipse创建一个web项目testFM2. 把下载到的jar包(freemarker-2.3.9.jar)放到/WebRoot/WEB-INF/li ...

  7. WPF TreeView的使用

    WPF提供了treeView控件,利用该控件开发者可以将数据分层显示在树结构中.当然其中需要用到Binding的机制,有用的类包括:ObjectDataProvider.DataTemplate.Hi ...

  8. 表单form的属性,单行文本框、密码框、单选多选按钮

    基础表单结构: <body> <h1> <hr /> <form action="" name="myFrom" en ...

  9. 深入理解Redis:底层数据结构

    简介 redis[1]是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...

  10. bzoj 2152: 聪聪可可

    #include<cstdio> #include<algorithm> using namespace std; ; ],head[N],son[N],f[N],d[N],r ...