#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;
const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 101010; // PLZ CHK!
ll N,M,K[MAX];
string A[MAX];
int main() {
fio();
cin>>N>>M;
for (int i=0; i<M; i++) cin>>A[i]>>K[i];
ll am=0,af=0,ans=0;
for (int i=M-1; i>=0; i--) {
ll m=0,f=0,mx=0;
string s=A[i];
for (int j=sz(s)-1; j>=0; j--) {
if (s[j]=='M') m++;
else f++;
if (m>=f+2) {
mx=max(mx, m-f-1);
}
}
am+=(K[i]-1)*m,af+=(K[i]-1)*f;
ll tm=0,tf=0;
for (int j=sz(s)-1; j>=0; j--) {
if (s[j]=='M') tm++;
else tf++;
ans=max(ans, am+tm-af-tf-1);
}
am+=m, af+=f;
}
if (am>af) {
cout<<-1;
}
else cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3492 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3444 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
1 ms |
3632 KB |
Output is correct |
16 |
Correct |
2 ms |
3624 KB |
Output is correct |
17 |
Correct |
2 ms |
3420 KB |
Output is correct |
18 |
Correct |
2 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3628 KB |
Output is correct |
21 |
Correct |
2 ms |
3420 KB |
Output is correct |
22 |
Correct |
1 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3492 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3444 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
1 ms |
3632 KB |
Output is correct |
16 |
Correct |
2 ms |
3624 KB |
Output is correct |
17 |
Correct |
2 ms |
3420 KB |
Output is correct |
18 |
Correct |
2 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3628 KB |
Output is correct |
21 |
Correct |
2 ms |
3420 KB |
Output is correct |
22 |
Correct |
1 ms |
3420 KB |
Output is correct |
23 |
Correct |
4 ms |
4324 KB |
Output is correct |
24 |
Correct |
4 ms |
4328 KB |
Output is correct |
25 |
Correct |
3 ms |
4328 KB |
Output is correct |
26 |
Correct |
2 ms |
4328 KB |
Output is correct |
27 |
Correct |
2 ms |
4328 KB |
Output is correct |
28 |
Correct |
4 ms |
4332 KB |
Output is correct |
29 |
Correct |
5 ms |
4328 KB |
Output is correct |
30 |
Correct |
3 ms |
4328 KB |
Output is correct |
31 |
Correct |
4 ms |
4328 KB |
Output is correct |
32 |
Correct |
3 ms |
4352 KB |
Output is correct |
33 |
Correct |
2 ms |
4328 KB |
Output is correct |
34 |
Correct |
2 ms |
4328 KB |
Output is correct |
35 |
Correct |
3 ms |
4328 KB |
Output is correct |
36 |
Correct |
4 ms |
4328 KB |
Output is correct |
37 |
Correct |
2 ms |
4328 KB |
Output is correct |
38 |
Correct |
4 ms |
4292 KB |
Output is correct |
39 |
Correct |
3 ms |
4324 KB |
Output is correct |
40 |
Correct |
3 ms |
4328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3492 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
2 ms |
3444 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
2 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
1 ms |
3632 KB |
Output is correct |
16 |
Correct |
2 ms |
3624 KB |
Output is correct |
17 |
Correct |
2 ms |
3420 KB |
Output is correct |
18 |
Correct |
2 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3628 KB |
Output is correct |
21 |
Correct |
2 ms |
3420 KB |
Output is correct |
22 |
Correct |
1 ms |
3420 KB |
Output is correct |
23 |
Correct |
4 ms |
4324 KB |
Output is correct |
24 |
Correct |
4 ms |
4328 KB |
Output is correct |
25 |
Correct |
3 ms |
4328 KB |
Output is correct |
26 |
Correct |
2 ms |
4328 KB |
Output is correct |
27 |
Correct |
2 ms |
4328 KB |
Output is correct |
28 |
Correct |
4 ms |
4332 KB |
Output is correct |
29 |
Correct |
5 ms |
4328 KB |
Output is correct |
30 |
Correct |
3 ms |
4328 KB |
Output is correct |
31 |
Correct |
4 ms |
4328 KB |
Output is correct |
32 |
Correct |
3 ms |
4352 KB |
Output is correct |
33 |
Correct |
2 ms |
4328 KB |
Output is correct |
34 |
Correct |
2 ms |
4328 KB |
Output is correct |
35 |
Correct |
3 ms |
4328 KB |
Output is correct |
36 |
Correct |
4 ms |
4328 KB |
Output is correct |
37 |
Correct |
2 ms |
4328 KB |
Output is correct |
38 |
Correct |
4 ms |
4292 KB |
Output is correct |
39 |
Correct |
3 ms |
4324 KB |
Output is correct |
40 |
Correct |
3 ms |
4328 KB |
Output is correct |
41 |
Correct |
15 ms |
6024 KB |
Output is correct |
42 |
Correct |
18 ms |
5980 KB |
Output is correct |
43 |
Correct |
2 ms |
3416 KB |
Output is correct |
44 |
Correct |
2 ms |
3416 KB |
Output is correct |
45 |
Correct |
1 ms |
3420 KB |
Output is correct |
46 |
Correct |
5 ms |
4316 KB |
Output is correct |
47 |
Incorrect |
4 ms |
3932 KB |
Output isn't correct |
48 |
Halted |
0 ms |
0 KB |
- |