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;
using ll = long long;
inline void fastio() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
}
ll n, m, ans, sum;
stack<pair<string, ll>> s;
int main() {
fastio();
cin >> n >> m;
while (m--) {
string str; ll t;
cin >> str >> t;
s.emplace(str, t);
}
while (!s.empty()) {
auto [str, k] = s.top();
s.pop();
ll mx = 0, tmp = 0;
for (auto it = str.end() - 1; it >= str.begin(); it--) {
tmp += *it == 'M' ? 1 : -1;
mx = max(mx, tmp - 1);
}
ans = max(ans, sum + k * mx);
sum += tmp;
}
cout << (sum <= 0 ? ans : -1);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |