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;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=200010, inf=2e9;
ll n;
int m;
void NOPE(){
cout<<-1;
exit(0);
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
ll mn=0, sum=0;
char S[MX]; int k;
for(int i=1; i<=m; i++){
cin>>(S+1)>>k;
ll now=0, low=0;
for(int i=1; S[i]!=0; i++){
if(S[i]=='M'){
now++;
}
else{
now--;
}
if(low>=now) low=now;
}
mn=min({mn, sum+low, sum+low+now*(k-1)});
sum+=now*k;
// cout<<now<<' '<<low<<' '<<cnt<<' '<<tmp<<'\n';
}
if(sum>0) NOPE();
ll need=max(0LL, sum-1-mn);
cout<<need;
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... |