 # LEXSTRI - Editorial

Tester: Ram Agrawal
Editorialist: Prathamesh Sogale

MEDIUM

DP

# PROBLEM:

Meena loves cricket very much. So, he went to watch the recent world cup match at the stadium, but for this he missed his exams. After pleading a lot, the teacher agreed to pass him on a condition that he completes a task.

Meena is given N strings consisting of lowercase English letters. He has to sort them 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 Meena has to spend E_i units of energy.

Find the minimum energy required by Meena to complete this task.

# EXPLANATION:

We will solve the problem with the help of dynamic programming. dp[i][j] is the minimum amount of energy that should be spent to make first i strings sorted in lexicographical order and i-th of them will be reversed if j=1 and not reversed if j = 0. dp[i][j] is updated by dp[i - 1] and dp[i - 1]. It remains to verify that the i-th string is lexicographically greater than (i - 1)-th (if j = 1 then we should check reversed i-th string, similar to (i - 1)-th). Then we update dp[i][j] = min(dp[i][j], dp[i - 1] + c[i] * j), dp[i][j] = min(dp[i][j], dp[i - 1] + j * c[i]). The answer is a minimum of dp[n] and dp[n].

# SOLUTIONS:

Setter's Solution
``````#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll INF = 1e18;
ll n,a,dp,ans;
string s;
int main(int argc, char const *argv[])
{
int t;
cin>>t;
while(t--)
{
int n ;
cin >> n;
for(ll i=1;i<=n;i++) cin >> a[i];
for(ll i=1;i<=n;i++){
cin >> s[i];
s[i] = s[i];
reverse(s[i].begin(),s[i].end());
}
dp = 0 ;
dp = a;
for(ll i=2;i<=n;i++){
for(int j=0;j<2;j++){
dp[i][j] = INF;
for(int k=0;k<2;k++){
if(s[i][j] >= s[i-1][k])
dp[i][j] = min(dp[i][j],dp[i-1][k]+j*a[i]);
}
}
}
ans = min(dp[n],dp[n]);
cout << (ans==INF ? -1 : ans) << endl;
}
return 0;
}
``````