제출 #1011568

#제출 시각아이디문제언어결과실행 시간메모리
1011568PringToilets (JOI16_toilets)C++17
100 / 100
14 ms5876 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif

#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;

const int MXN = 100005;
ll n;
int m;
string s[MXN];
ll mul[MXN];
ll now = 0, ans = 0;

void DO(string &s, ll k) {
    reverse(s.begin(), s.end());
    ll d = 0, low = 0;
    for (auto &i : s) {
        if (i == 'F') d++;
        else {
            d--;
            low = min(low, d);
        }
    }
    low = min(low, d * (k - 1) + low);
    d *= k;
    ans = min(ans, now + low);
    now += d;
}

void miku() {
    cin >> n >> m;
    FOR(i, 0, m) cin >> s[i] >> mul[i];
    for (int i = m - 1; i >= 0; i--) {
        DO(s[i], mul[i]);
    }
    if (now < 0) {
        cout << -1 << '\n';
        return;
    }
    cout << max(0LL, -ans - 1) << '\n';
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    miku();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...