#include <bits/stdc++.h>
using namespace std;
string s[100005];
int A[100005];
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
int M;
cin >> N >> M;
int i, j;
for(i=0;i<M;i++) cin >> s[i] >> A[i];
int ma = 0;
int sum = 0;
for(i=M-1;i>=0;i--) {
int cnt = 0;
int ma2 = 0;
for(j=s[i].length()-1;j>=0;j--) {
if(s[i][j]=='M') cnt++;
else cnt--;
ma2 = max(ma2, cnt);
}
if(cnt<=0) ma = max(ma, sum + ma2);
else ma = max(ma, sum + cnt + ma2 + (A[i]-1) * cnt);
sum += cnt * A[i];
}
cout << (sum <= 0 ? max(0,ma-1) : -1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
3 ms |
3796 KB |
Output is correct |
24 |
Correct |
3 ms |
3796 KB |
Output is correct |
25 |
Correct |
3 ms |
3796 KB |
Output is correct |
26 |
Correct |
3 ms |
3796 KB |
Output is correct |
27 |
Correct |
3 ms |
3796 KB |
Output is correct |
28 |
Correct |
3 ms |
3796 KB |
Output is correct |
29 |
Correct |
3 ms |
3796 KB |
Output is correct |
30 |
Correct |
3 ms |
3796 KB |
Output is correct |
31 |
Correct |
4 ms |
3796 KB |
Output is correct |
32 |
Correct |
3 ms |
3796 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
3 ms |
3796 KB |
Output is correct |
35 |
Correct |
3 ms |
3796 KB |
Output is correct |
36 |
Correct |
3 ms |
3796 KB |
Output is correct |
37 |
Correct |
2 ms |
3796 KB |
Output is correct |
38 |
Correct |
3 ms |
3796 KB |
Output is correct |
39 |
Correct |
3 ms |
3796 KB |
Output is correct |
40 |
Correct |
3 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
2 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
2 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3412 KB |
Output is correct |
13 |
Correct |
2 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3412 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
3 ms |
3796 KB |
Output is correct |
24 |
Correct |
3 ms |
3796 KB |
Output is correct |
25 |
Correct |
3 ms |
3796 KB |
Output is correct |
26 |
Correct |
3 ms |
3796 KB |
Output is correct |
27 |
Correct |
3 ms |
3796 KB |
Output is correct |
28 |
Correct |
3 ms |
3796 KB |
Output is correct |
29 |
Correct |
3 ms |
3796 KB |
Output is correct |
30 |
Correct |
3 ms |
3796 KB |
Output is correct |
31 |
Correct |
4 ms |
3796 KB |
Output is correct |
32 |
Correct |
3 ms |
3796 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
3 ms |
3796 KB |
Output is correct |
35 |
Correct |
3 ms |
3796 KB |
Output is correct |
36 |
Correct |
3 ms |
3796 KB |
Output is correct |
37 |
Correct |
2 ms |
3796 KB |
Output is correct |
38 |
Correct |
3 ms |
3796 KB |
Output is correct |
39 |
Correct |
3 ms |
3796 KB |
Output is correct |
40 |
Correct |
3 ms |
3796 KB |
Output is correct |
41 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |