Comments
+8

Yes. Rating will be recalculated and changed back. and it is generally more than previously calculated rating.( for you it will be more than 789 after recalculation, if your code is not plagiarized).

Yeah, it was hard to implement if you use greedy approach. but you can also solve it with DP and implementation wasn't that hard.

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
 
const int N2 = 1e2 + 5;
const int N3 = 1e3 + 5;
const int N5 = 1e5 + 5;
const int N6 = 1e6 + 5;
const int INF = 2e9 + 5;
 
vector<pair<int, int> > directions_8 {{-1, -1}, {1, 1}, {1, -1}, {-1, 1}, {0, 1}, {0, -1}, {-1, 0}, {1, 0}};
vector<pair<int, int> >directions_4 {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
 
 
int getMax(vector<vector<vector<int>>>& dp, string& str, int currIdx = 0, int currMax = 0, int rem = 1) {
    if(currIdx == str.length()) return 0;
 
    if(dp[currIdx][currMax][rem] != -INF) return dp[currIdx][currMax][rem];
 
    int charIdx = str[currIdx] - 'A' + 1;
 
    int possibleAnswer = - INF;
 
    if(rem == 1) {
        for(int i = 1; i <= 5; i ++) {
            int temp = pow(10, i - 1);
            if(i >= currMax) possibleAnswer = max(possibleAnswer, temp + getMax(dp, str, currIdx + 1, i, 0));
            else possibleAnswer = max(possibleAnswer, -temp + getMax(dp, str, currIdx + 1, currMax, 0));
        }
    }
    
    int temp = pow(10, charIdx - 1);
    if(charIdx >= currMax) possibleAnswer = max(possibleAnswer, temp + getMax(dp, str, currIdx + 1, charIdx, rem));
    else possibleAnswer = max(possibleAnswer, - temp + getMax(dp, str, currIdx + 1, currMax, rem));
    
    dp[currIdx][currMax][rem] =  possibleAnswer;
    return dp[currIdx][currMax][rem];
}
 
int main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 
    int t;
    cin >> t;
 
    while(t --) {
        string s;
        cin >> s;
 
        reverse(s.begin(), s.end());
 
        int n = s.length();
 
        vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(6, vector<int>(2, -INF)));
 
        int answer = getMax(dp, s);
 
        cout << answer << endl;
 
    }

.

Hi, Thanks for providing solution. I think in your solution you passed string without references so, it gets created every time function calls. try to pass it by reference, it'll reduce time and you can avoid TLE.