#include <bits/stdc++.h>
#define int long long
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 + ma2 + (A[i]-1) * cnt);
sum += cnt * A[i];
}
cout << (sum <= 0 ? max(0LL,ma-1) : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
3 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3336 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 |
4 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3372 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 |
3416 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
3 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3336 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 |
4 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3372 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 |
3416 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 |
3732 KB |
Output is correct |
24 |
Correct |
3 ms |
3796 KB |
Output is correct |
25 |
Correct |
5 ms |
3796 KB |
Output is correct |
26 |
Correct |
4 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 |
3 ms |
3796 KB |
Output is correct |
32 |
Correct |
4 ms |
3796 KB |
Output is correct |
33 |
Correct |
4 ms |
3788 KB |
Output is correct |
34 |
Correct |
3 ms |
3824 KB |
Output is correct |
35 |
Correct |
3 ms |
3796 KB |
Output is correct |
36 |
Correct |
5 ms |
3796 KB |
Output is correct |
37 |
Correct |
4 ms |
3796 KB |
Output is correct |
38 |
Correct |
3 ms |
3796 KB |
Output is correct |
39 |
Correct |
5 ms |
3796 KB |
Output is correct |
40 |
Correct |
4 ms |
3796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3412 KB |
Output is correct |
2 |
Correct |
3 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3336 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 |
4 ms |
3412 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3372 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 |
3416 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 |
3732 KB |
Output is correct |
24 |
Correct |
3 ms |
3796 KB |
Output is correct |
25 |
Correct |
5 ms |
3796 KB |
Output is correct |
26 |
Correct |
4 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 |
3 ms |
3796 KB |
Output is correct |
32 |
Correct |
4 ms |
3796 KB |
Output is correct |
33 |
Correct |
4 ms |
3788 KB |
Output is correct |
34 |
Correct |
3 ms |
3824 KB |
Output is correct |
35 |
Correct |
3 ms |
3796 KB |
Output is correct |
36 |
Correct |
5 ms |
3796 KB |
Output is correct |
37 |
Correct |
4 ms |
3796 KB |
Output is correct |
38 |
Correct |
3 ms |
3796 KB |
Output is correct |
39 |
Correct |
5 ms |
3796 KB |
Output is correct |
40 |
Correct |
4 ms |
3796 KB |
Output is correct |
41 |
Correct |
27 ms |
4148 KB |
Output is correct |
42 |
Correct |
22 ms |
4096 KB |
Output is correct |
43 |
Correct |
3 ms |
3456 KB |
Output is correct |
44 |
Correct |
3 ms |
3456 KB |
Output is correct |
45 |
Correct |
2 ms |
3460 KB |
Output is correct |
46 |
Correct |
9 ms |
4120 KB |
Output is correct |
47 |
Correct |
4 ms |
3844 KB |
Output is correct |
48 |
Correct |
4 ms |
3856 KB |
Output is correct |
49 |
Correct |
4 ms |
3856 KB |
Output is correct |
50 |
Correct |
3 ms |
3780 KB |
Output is correct |
51 |
Correct |
11 ms |
4052 KB |
Output is correct |
52 |
Correct |
20 ms |
5260 KB |
Output is correct |
53 |
Correct |
20 ms |
5196 KB |
Output is correct |
54 |
Correct |
27 ms |
5424 KB |
Output is correct |
55 |
Correct |
3 ms |
3788 KB |
Output is correct |
56 |
Correct |
3 ms |
3796 KB |
Output is correct |