This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
const int inf = (int) 1e9, N = (int) 70000 + 22, A = 10;
int n, need[N], add = 0, ans = inf, all = 0;
string s, t = "";
vector<int> pos;
void upd(int& x, int y) {
    x = min(x, y);
}
void solve() {
    cin >> n >> s;
    add = std::count(s.begin(), s.end(), 'e');
    if (!add) {
        cout << 0 << '\n';
        return;
    }
    for (int i = 0; i < n; i++) {
        if (s[i] != 'e') {
            if (i && s[i - 1] == 'e') {
                need[(int) t.size()] = 1;
                pos.push_back((int) t.size());
            }
            t += s[i];
        }
    }
    t.push_back('e');
    s = t, n = (int) s.size(), all = (int) pos.size();
    vector dp(n, vector<int> (10, inf));
    vector dp2(n, vector (10, vector<int> (10, inf)));
    for (int j = 0; j < 10; j++) {
        dp[0][j] = 0;
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < 10; j++) {
            if (j != s[i - 1] - 'a' && need[i - 1]) {}
            else {
                upd(dp[i][j], dp[i - 1][j] + 2 * int(s[i] - 'a' == j));
            }
            upd(dp[i][j], dp[i - 1][s[i - 1] - 'a'] + 2 * int(s[i] - 'a' == j));
            upd(dp[i][j], dp2[i - 1][s[i - 1] - 'a'][j] + 2 * int(s[i] - 'a' == j));
            upd(dp[i][j], dp2[i - 1][s[i - 1] - 'a'][s[i - 1] - 'a'] + 2 * int(s[i] - 'a' == j));
            for (int k = 0; k < 10; k++) {
                upd(dp2[i][j][k], dp[i - 1][j] + 2 * int(s[i] - 'a' == j) + 1 + 2 * int(s[i] - 'a' == k));
                upd(dp2[i][j][k], dp[i - 1][s[i - 1] - 'a'] + 2 * int(s[i] - 'a' == j) + 1 + 2 * int(s[i] - 'a' == k));
                upd(dp2[i][j][k], dp2[i - 1][j][k] + 2 * int(s[i] - 'a' == j) + 1 + 2 * int(s[i] - 'a' == k));
                upd(dp2[i][j][k], dp2[i - 1][s[i - 1] - 'a'][k] + 2 * int(s[i] - 'a' == j) + 1 + 2 * int(s[i] - 'a' == k));
                upd(dp2[i][j][k], dp2[i - 1][j][s[i - 1] - 'a'] + 2 * int(s[i] - 'a' == j) + 1 + 2 * int(s[i] - 'a' == k));
                upd(dp2[i][j][k], dp2[i - 1][s[i - 1] - 'a'][s[i - 1] - 'a'] + 2 * int(s[i] - 'a' == j) + 1 + 2 * int(s[i] - 'a' == k));
            }
        }
    }
    cout << dp[n - 1][4] - 2 + 2 * add << '\n';
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |