-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2734.cpp
49 lines (41 loc) · 1012 Bytes
/
2734.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @file 2734.cpp
* @author Macesuted ([email protected])
* @date 2022-12-07
*
* @copyright Copyright (c) 2022
*
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif
bool mem1;
void solve(void) {
int64_t n, m, pre = 0, minp = 0;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
string a;
int64_t k, s = 0, mp = pre;
cin >> a >> k;
for (auto j : a) mp = min(mp, pre + (s += (j == 'M' ? +1 : -1)));
minp = min({minp, mp, mp + s * (k - 1)}), pre += s * k;
}
if (pre > 0) return cout << -1 << endl, void();
cout << max((int64_t)0, (pre - 1) - minp) << endl;
return;
}
bool mem2;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif
int _ = 1;
while (_--) solve();
#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}