This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* ************************************************************************** */
/* */
/* ::: ::: ::: */
/* Problem Number: 17709 :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: js9028 <boj.kr/u/js9028> +#+ +#+ +#+ */
/* +#+ +#+ +#+ */
/* https://boj.kr/17709 #+# #+# #+# */
/* Solved: 2024/09/12 19:48:56 by js9028 ### ### ##.kr */
/* */
/* ************************************************************************** */
#include <bits/stdc++.h>
using namespace std;
#define fastio (ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL))
typedef long long ll;
typedef long double lld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const int MAXSIZE = 2000000;
const long long INF = 1e18 + 5;
const double EPSILON = 1e-14;
const ll DIV = 2000003;
const long double pi = 3.14159265358979323846264338327950288419716939937510L;
ll n, m;
string s[200055];
ll cnt[200055];
int main()
{
fastio;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> s[i];
cin >> cnt[i];
}
ll glt = 0;
ll mxt = 0;
for (int i = m - 1; i >= 0; i--)
{
string &cs = s[i];
ll nt = 0;
ll mm = 0;
for (int j = cs.size() - 1; j >= 0; j--)
{
nt += (cs[j] == 'M' ? -1 : 1);
mxt = min(mxt, nt + glt);
mm = min(mm, nt);
}
nt = 0;
mm = 0;
glt += nt * (cnt[i] - 1);
for (int j = cs.size() - 1; j >= 0; j--)
{
nt += (cs[j] == 'M' ? -1 : 1);
mxt = min(mxt, nt + glt);
mm = min(mm, nt);
}
mxt = min(mxt, glt + (cnt[i] - 1) * nt + mm);
glt += nt;
mxt = min(mxt, glt);
}
if (glt < 0)
{
cout << -1;
}
else
{
cout << -mxt - 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... |