#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, k;
vector<pair<string, ll> > vec;
ll m, f;
ll ans;
ll depth;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for(int i=1; i<=k; i++){
string ts;
ll tt;
cin >> ts >> tt;
reverse(ts.begin(), ts.end());
for(int i=0; i<(int)ts.size(); i++){
if(ts[i] == 'M') m += tt;
else f += tt;
}
vec.push_back(make_pair(ts, tt));
}
if(m > n){
printf("-1");
return 0;
}
reverse(vec.begin(), vec.end());
for(int i=0; i<k; i++){
ll thisDepth = 0;
ll maxDepth = 0;
for(auto &x: vec[i].first){
if(x == 'M') thisDepth--;
else thisDepth++;
if(thisDepth < maxDepth) maxDepth = thisDepth;
}
if(thisDepth >= 0){
ans = min(ans, depth + maxDepth);
depth += thisDepth * vec[i].second;
}
else{
ans = min(ans, depth + thisDepth * (vec[i].second - 1) + maxDepth);
depth += thisDepth * vec[i].second;
}
}
printf("%lld", max(0LL, -ans-1));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |