215 lines
5.1 KiB
C++
215 lines
5.1 KiB
C++
/* Problem URL: https://codeforces.com/problemset/problem/1841/C */
|
|
|
|
#include <bits/stdc++.h>
|
|
#include <ext/pb_ds/assoc_container.hpp>
|
|
#include <ext/pb_ds/tree_policy.hpp>
|
|
|
|
using namespace std;
|
|
using namespace __gnu_pbds;
|
|
|
|
template <class T, class comp = less<>>
|
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
|
|
|
#define V vector
|
|
|
|
#define rmin(a, b) a = min(a, b)
|
|
#define rmax(a, b) a = max(a, b)
|
|
|
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
|
|
|
#define repv(i, v) for (auto &i : (v))
|
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
|
#define sortv(v) sort(v.begin(), v.end())
|
|
#define all(v) (v).begin(), (v).end()
|
|
|
|
using vi = vector<int>;
|
|
using vvi = vector<vi>;
|
|
using vvvi = vector<vvi>;
|
|
using vvvvi = vector<vvvi>;
|
|
|
|
using ll = long long;
|
|
|
|
using vl = vector<ll>;
|
|
using vvl = vector<vl>;
|
|
using vvvl = vector<vvl>;
|
|
using vvvvl = vector<vvvl>;
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
|
os << vec[0];
|
|
for (size_t i = 1; i < vec.size(); i++) {
|
|
os << ' ' << vec[i];
|
|
}
|
|
os << '\n';
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
is >> i;
|
|
}
|
|
return is;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
|
for (auto &i : vec) {
|
|
os << i[0];
|
|
for (size_t j = 1; j < i.size(); j++) {
|
|
os << ' ' << i[j];
|
|
}
|
|
os << '\n';
|
|
}
|
|
return os;
|
|
}
|
|
|
|
template<class v>
|
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
|
for (auto &i : vec) {
|
|
for (auto &j : i) {
|
|
is >> j;
|
|
}
|
|
}
|
|
return is;
|
|
}
|
|
|
|
const int oo = INT32_MAX >> 1;
|
|
const ll OO = INT64_MAX >> 1;
|
|
|
|
|
|
void pre()
|
|
{
|
|
|
|
}
|
|
|
|
#define TEST 1
|
|
|
|
void solve()
|
|
{
|
|
string a;
|
|
cin >> a;
|
|
int n = a.size();
|
|
|
|
vvi support(n, vi(4));
|
|
|
|
map<char, int> var;
|
|
|
|
vl val = {1, 10, 100, 1000, 10000};
|
|
|
|
vvi count(n + 1, vi(5));
|
|
vi cul(5);
|
|
|
|
vvi valid(5);
|
|
vvl rem(n, vl(5));
|
|
|
|
vl cur(n);
|
|
|
|
ll total = 0;
|
|
|
|
for (int i = n - 1; i >= 0; i--) {
|
|
int pos = a[i] - 'A';
|
|
|
|
count[i] = count[i + 1];
|
|
count[i][pos]++;
|
|
cul[pos] = i;
|
|
|
|
ll calc = accumulate(count[i].begin() + pos + 1, count[i].end(), 0);
|
|
if (calc == 0) {
|
|
valid[pos].push_back(i);
|
|
total += val[pos];
|
|
cur[i] = val[pos];
|
|
continue;
|
|
}
|
|
|
|
if (calc == 1) {
|
|
int it = find(count[i].begin() + pos + 1, count[i].end(), 1) - count[i].begin();
|
|
rem[cul[it]][pos]++;
|
|
total -= val[pos];
|
|
cur[i] = -val[pos];
|
|
continue;
|
|
}
|
|
|
|
total -= val[pos];
|
|
cur[i] = -val[pos];
|
|
}
|
|
|
|
rep(i, 4) {
|
|
reverse(all(valid[i]));
|
|
}
|
|
|
|
ll ans = total;
|
|
rep(i, n) {
|
|
count[i][a[i] - 'A']--;
|
|
auto testa = [&]() {
|
|
ll now = total - cur[i] + rem[i][0] * 2 + rem[i][1] * 20 + rem[i][2] * 200 + rem[i][3] * 2000;
|
|
if (accumulate(count[i].begin() + 1, count[i].end(), 0) > 0) {
|
|
return now - 1;
|
|
}
|
|
return now + 1;
|
|
};
|
|
|
|
auto testb = [&]() {
|
|
ll now = total - cur[i] + rem[i][1] * 20 + rem[i][2] * 200 + rem[i][3] * 2000;
|
|
now -= lower_bound(all(valid[0]), i) - valid[0].begin();
|
|
if (accumulate(count[i].begin() + 2, count[i].end(), 0) > 0) {
|
|
return now - 10;
|
|
}
|
|
return now + 10;
|
|
};
|
|
|
|
auto testc = [&]() {
|
|
ll now = total - cur[i] + rem[i][2] * 200 + rem[i][3] * 2000;
|
|
now -= lower_bound(all(valid[0]), i) - valid[0].begin();
|
|
now -= (lower_bound(all(valid[1]), i) - valid[1].begin()) * 20;
|
|
if (accumulate(count[i].begin() + 3, count[i].end(), 0) > 0) {
|
|
return now - 100;
|
|
}
|
|
return now + 100;
|
|
};
|
|
|
|
auto testd = [&]() {
|
|
ll now = total - cur[i] + rem[i][3] * 2000;
|
|
now -= lower_bound(all(valid[0]), i) - valid[0].begin();
|
|
now -= (lower_bound(all(valid[1]), i) - valid[1].begin()) * 20;
|
|
now -= (lower_bound(all(valid[2]), i) - valid[2].begin()) * 200;
|
|
if (accumulate(count[i].begin() + 4, count[i].end(), 0) > 0) {
|
|
return now - 1000;
|
|
}
|
|
return now + 1000;
|
|
};
|
|
|
|
auto teste = [&]() {
|
|
ll now = total - cur[i] + 10000;
|
|
now -= lower_bound(all(valid[0]), i) - valid[0].begin();
|
|
now -= (lower_bound(all(valid[1]), i) - valid[1].begin()) * 20;
|
|
now -= (lower_bound(all(valid[2]), i) - valid[2].begin()) * 200;
|
|
now -= (lower_bound(all(valid[3]), i) - valid[3].begin()) * 2000;
|
|
return now;
|
|
};
|
|
|
|
rmax(ans, testa());
|
|
rmax(ans, testb());
|
|
rmax(ans, testc());
|
|
rmax(ans, testd());
|
|
rmax(ans, teste());
|
|
}
|
|
|
|
cout << ans << '\n';
|
|
}
|
|
|
|
int main()
|
|
{
|
|
ios::sync_with_stdio(false);
|
|
cin.tie(nullptr);
|
|
|
|
pre();
|
|
|
|
int t;
|
|
(TEST && cin >> t) || (t = 1);
|
|
while (t--) {
|
|
solve();
|
|
}
|
|
}
|