-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeforces_Hard_Problem.cpp
More file actions
58 lines (48 loc) · 1.24 KB
/
Copy pathCodeforces_Hard_Problem.cpp
File metadata and controls
58 lines (48 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
void solve(){
ll n;
cin>>n;
vector<ll> c(n);
for(ll i=0; i<n; i++){
cin>>c[i];
}
vector<string> a(n);
for(ll i=0; i<n; i++){
cin>>a[i];
}
vector<vector<ll>> dp(n, vector<ll>(2, 1e18));
dp[0][0] = 0ll;
dp[0][1] = c[0];
string last = a[0];
reverse(a[0].begin(), a[0].end());
string tsal = a[0];
for(ll i=1; i<n; i++){
string cur = a[i];
reverse(a[i].begin(), a[i].end());
string ruc = a[i];
if(cur >= last)
dp[i][0] = min(dp[i][0], dp[i-1][0]);
if(cur >= tsal)
dp[i][0] = min(dp[i][0], dp[i-1][1]);
if(ruc >= last)
dp[i][1] = min(dp[i][1], dp[i-1][0]+c[i]);
if(ruc >= tsal)
dp[i][1] = min(dp[i][1], dp[i-1][1]+c[i]);
last = cur;
tsal = ruc;
}
ll result = min(dp[n-1][0],dp[n-1][1]);
if(result == 1e18) cout<<-1<<endl;
else cout<<result<<endl;
}
signed main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
optimize();
solve();
return 0;
}