G - Hard problem CodeForces - 706C DP

 

 

 

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
string a[100000 +10], b[100000 +10];
ll dp[100000 + 10][2];
ll f[100000 + 10];
const ll INF = 1e15;
int main(){
ll n ;
cin >> n;
for(ll i=0;i<100000 +10;i++)    //初始化为无穷大。
    {
        dp[i][0]=INF;
        dp[i][1]=INF;
    }

for(ll i = 1 ; i <= n ;  i++)
    cin >> f[i];
for(ll i = 1 ; i <= n ; i ++){
    cin >> a[i];
    b[i] = a[i];
    reverse(b[i].begin(),b[i].end());
}
dp[1][0] = 0;
dp[1][1] = f[1];
for(ll i = 2; i <= n ;  i++ )
{
    if(a[i]  >= a[i-1])
        dp[i][0] = min(dp[i][0],dp[i-1][0]);
    if(a[i] >= b[i-1] )
    dp[i][0] = min(dp[i][0],dp[i-1][1]) ;
    if(b[i] >= a[i-1])
        dp[i][1] = min(dp[i][1],dp[i-1][0] + f[i]);
    if(b[i] >= b[i-1])
        dp[i][1] = min(dp[i-1][1] + f[i] , dp[i][1]);

}
ll minn=min(dp[n][0],dp[n][1]);
    if(minn<INF)
        printf("%lld\n",minn);
    else
        printf("-1\n");

return 0;
}

 

posted @ 2019-04-18 18:51  Agnel_Cynthia  阅读(113)  评论(0编辑  收藏  举报
Live2D