This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "dungeons.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define ALL(v) v.begin(), v.end()
#define pb push_back
#define SZ(a) ((int)a.size())
struct node {
    int to;
    ll sum;
    node operator+(const node &a) const {
        return node{a.to, sum + a.sum};
    }
};
int N, lg = __lg(10000000);
vector<int> S, P, W, L;
vector<int> val;
vector<node> dp[10][30];
void init(int n, vector<int> s, vector<int> p, vector<int> w, vector<int> l) {
    N = n, S = s, P = p, W = w, L = l;
    for (int i : S)
        val.pb(i);
    val.pb(0);
    sort(ALL(val)), val.resize(unique(ALL(val)) - val.begin());
    if (SZ(val) > 6)
        return;
    for (int i = 0; i < SZ(val); ++i) {
        dp[i][0].resize(N + 1);
        dp[i][0][N] = node{N, 0LL};
        for (int j = 0; j < N; ++j)
            if (val[i] >= S[j])
                dp[i][0][j] = node{W[j], (ll)S[j]};
            else
                dp[i][0][j] = node{L[j], (ll)P[j]};
        for (int j = 1; j <= lg; ++j) {
            dp[i][j].resize(N + 1);
            for (int k = 0; k <= N; ++k)
                dp[i][j][k] = dp[i][j - 1][k] + dp[i][j - 1][dp[i][j - 1][k].to]; 
        }
    }
	return;
}
long long simulate(int x, int z) {
	if (SZ(val) > 6)
        return 0;
    int p;
    ll nw = z;
    for (p = 0; p < SZ(val);) {
        while (p + 1 < SZ(val) && nw >= val[p + 1])
            ++p;
        if (dp[p][lg][x].to == N)
            break;
        for (int i = lg; i >= 0; --i)
            if (nw + dp[p][i][x].sum < val[p + 1]) {
                nw += dp[p][i][x].sum;
                x = dp[p][i][x].to;
            }
        nw += dp[p][0][x].sum;
        x = dp[p][0][x].to;
    }
    for (int i = lg; i >= 0; --i)
        if (dp[p][i][x].to != N) {
            nw += dp[p][i][x].sum;
            x = dp[p][i][x].to;
        }
    nw += dp[p][0][x].sum;
    return nw;
}
| # | 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... |