Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Cosmic Rays

#include <bits/stdc++.h>
using namespace std;

#define color first
#define freq second

void solve() {
    int n;
    cin >> n;

    stack<pair<long long, long long>> stk;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        long long f, v;
        cin >> f >> v;

        long long alive_rounds = f;
        while (!stk.empty()) {
            if (stk.top().color == v) {
                alive_rounds += stk.top().freq;
                stk.pop();
            } else if (f > 0) {
                auto t = min(f, stk.top().freq);
                f -= t;

                auto old = stk.top();
                stk.pop();

                old.freq -= t;
                if (old.freq != 0) {
                    stk.push(old);
                }
            } else {
                break;
            }
        }
        ans = max(ans, alive_rounds);
        stk.push({v, alive_rounds});
        cout << ans << " \n"[i == n - 1];
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}