#include <bits/stdc++.h>
#define ll long long
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
using namespace std;
const int MAXN = 5e4 + 1;
const int LOG = 20;
ll ma[MAXN][LOG], up2[MAXN][LOG], sum2[MAXN][LOG];
ll up3[25][MAXN][LOG], sum3[25][MAXN][LOG];
vector<int> S, P, W, L;
vector<ll>dif;
int N;
void init(int n, vector<int> s, vector<int> p, vector<int> w, vector<int> l) {
    ll pot = 1;
    for (int i = 0; i < 25; i++) {
        dif.pb(pot);
        pot *= 2;
    }
    s.pb(0); p.pb(0); w.pb(n); l.pb(n);
    N = n; S = s; P = p; W = w; L = l;
    for (int i = 0; i <= n; i++) {
        up2[i][0] = w[i];
        ma[i][0] = s[i];
        sum2[i][0] = s[i];
        for (int j = 0; j < sz(dif); j++) {
            ll x, y;
            if (s[i] < dif[j]) x = w[i], y = s[i];
            else x = l[i], y = p[i];
            up3[j][i][0] = x;
            sum3[j][i][0] = y;
        }
    }
    for (int i = 1; i < LOG; i++) {
        for (int j = 0; j <= n; j++) {
            up2[j][i] = up2[up2[j][i-1]][i-1];
            sum2[j][i] = sum2[j][i-1] + sum2[up2[j][i-1]][i-1];
            ma[j][i] = max(ma[j][i-1], ma[up2[j][i-1]][i-1]);
            for (int k = 0; k < sz(dif); k++) {
                up3[k][j][i] = up3[k][up3[k][j][i-1]][i-1];
                sum3[k][j][i] = sum3[k][j][i-1] + sum3[k][up3[k][j][i-1]][i-1];
            }
        }
    }
}
long long simulate(int x, int z) {
    ll act = z;
    while (x != N) {
        if (act >= dif.back()) { // fuerza tan grande que gana siempre
            while (x != N) act += S[x], x = W[x];
            return act;
        }
        if (S[x] <= act) {
            ll aum = 0;
            for (int i = LOG-1; i >= 0; i--) {
                if (ma[x][i] <= act) {
                    aum += sum2[x][i];
                    x = up2[x][i];
                }
            }
            act += aum;
        } else {
            int pos = upper_bound(all(dif), act) - dif.begin() - 1;
            for (int i = LOG-1; i >= 0; i--) {
                if (sum3[pos][x][i] + act < dif[pos+1]) {
                    act += sum3[pos][x][i];
                    x = up3[pos][x][i];
                }
            }
            if (x != N) {
                if (S[x] <= act) act += S[x], x = W[x];
                else act += P[x], x = L[x];
            }
        }
    }
    return act;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |