#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
ll N, M;
vector<pair<string,ll>> v;
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
ll male = 0;
for (ll i=0; i<M; i++) {
string s;
ll rep;
cin >> s >> rep;
male += count(s.begin(), s.end(), 'M') * rep;
reverse(s.begin(), s.end());
v.emplace_back(s, rep);
}
if (male > N) {
cout << "-1";
return 0;
}
reverse(v.begin(), v.end());
M = (ll) v.size();
ll val = 0;
ll ans = 0;
for (ll i=0; i<M; i++) {
auto &[s, rep] = v[i];
ll sum = 2 * count(s.begin(), s.end(), 'M') - (ll) s.size();
for (ll j=0; j<s.size(); j++) {
if (s[j] == 'M') val ++;
else val --;
ans = max(ans, val - 1);
ans = max(ans, (val + sum*(rep-1)) - 1);
}
val += sum * (rep-1);
}
cout << ans;
return 0;
}
Compilation message
toilets.cpp: In function 'int main()':
toilets.cpp:36:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (ll j=0; j<s.size(); j++) {
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
1000 KB |
Output is correct |
24 |
Correct |
2 ms |
1000 KB |
Output is correct |
25 |
Correct |
2 ms |
1000 KB |
Output is correct |
26 |
Correct |
1 ms |
1000 KB |
Output is correct |
27 |
Correct |
2 ms |
1000 KB |
Output is correct |
28 |
Correct |
2 ms |
984 KB |
Output is correct |
29 |
Correct |
2 ms |
996 KB |
Output is correct |
30 |
Correct |
2 ms |
1000 KB |
Output is correct |
31 |
Correct |
2 ms |
1000 KB |
Output is correct |
32 |
Correct |
2 ms |
996 KB |
Output is correct |
33 |
Correct |
1 ms |
1120 KB |
Output is correct |
34 |
Correct |
2 ms |
1000 KB |
Output is correct |
35 |
Correct |
2 ms |
1000 KB |
Output is correct |
36 |
Correct |
2 ms |
1000 KB |
Output is correct |
37 |
Correct |
1 ms |
984 KB |
Output is correct |
38 |
Correct |
1 ms |
1000 KB |
Output is correct |
39 |
Correct |
2 ms |
988 KB |
Output is correct |
40 |
Correct |
2 ms |
996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
1000 KB |
Output is correct |
24 |
Correct |
2 ms |
1000 KB |
Output is correct |
25 |
Correct |
2 ms |
1000 KB |
Output is correct |
26 |
Correct |
1 ms |
1000 KB |
Output is correct |
27 |
Correct |
2 ms |
1000 KB |
Output is correct |
28 |
Correct |
2 ms |
984 KB |
Output is correct |
29 |
Correct |
2 ms |
996 KB |
Output is correct |
30 |
Correct |
2 ms |
1000 KB |
Output is correct |
31 |
Correct |
2 ms |
1000 KB |
Output is correct |
32 |
Correct |
2 ms |
996 KB |
Output is correct |
33 |
Correct |
1 ms |
1120 KB |
Output is correct |
34 |
Correct |
2 ms |
1000 KB |
Output is correct |
35 |
Correct |
2 ms |
1000 KB |
Output is correct |
36 |
Correct |
2 ms |
1000 KB |
Output is correct |
37 |
Correct |
1 ms |
984 KB |
Output is correct |
38 |
Correct |
1 ms |
1000 KB |
Output is correct |
39 |
Correct |
2 ms |
988 KB |
Output is correct |
40 |
Correct |
2 ms |
996 KB |
Output is correct |
41 |
Correct |
19 ms |
7672 KB |
Output is correct |
42 |
Correct |
19 ms |
8648 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
344 KB |
Output is correct |
46 |
Correct |
5 ms |
1500 KB |
Output is correct |
47 |
Correct |
2 ms |
948 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
49 |
Correct |
2 ms |
860 KB |
Output is correct |
50 |
Correct |
2 ms |
852 KB |
Output is correct |
51 |
Correct |
5 ms |
1464 KB |
Output is correct |
52 |
Correct |
17 ms |
7436 KB |
Output is correct |
53 |
Correct |
17 ms |
8392 KB |
Output is correct |
54 |
Correct |
17 ms |
7624 KB |
Output is correct |
55 |
Correct |
1 ms |
860 KB |
Output is correct |
56 |
Correct |
2 ms |
860 KB |
Output is correct |