#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
using ll = long long;
int N, idx, rv[300005], pre[300005], sz[300005], x[300005], p[300005];
ll s, os, wei[300005], mn[300005];
bool seen[300005];
vector<int> adj[300005];
int dfs(int u) {
wei[u] += x[u];
mn[u] = min(mn[u], wei[u]);
pre[u] = idx++;
rv[idx] = u;
sz[u] = 1;
for (auto v : adj[u]) {
wei[v] = wei[u];
mn[v] = min(mn[v], mn[u]);
sz[u] += dfs(v);
}
return sz[u];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> s;
os = s;
for (int i = 1; i <= N; i++) {
cin >> x[i] >> p[i];
adj[p[i]].pb(i);
}
seen[0] = 1;
while (1) {
for (int i = 1; i <= N; i++) {
mn[i] = LLONG_MAX;
}
dfs(0);
pair<ll, int> tk = mp(-1, -1);
for (int i = 1; i <= N; i++) {
if (!seen[i] && s + mn[i] >= 0) {
tk = max(tk, mp(wei[i], i));
}
}
if (tk.first < 0) {
break;
}
s += tk.first;
for (int j = tk.second; !seen[j]; j = p[j]) {
seen[j] = 1;
x[j] = 0;
}
}
cout << s - os << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2067 ms |
26512 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
15452 KB |
Output is correct |
5 |
Correct |
3 ms |
15196 KB |
Output is correct |
6 |
Correct |
12 ms |
16240 KB |
Output is correct |
7 |
Correct |
17 ms |
16220 KB |
Output is correct |
8 |
Correct |
13 ms |
16220 KB |
Output is correct |
9 |
Correct |
11 ms |
16080 KB |
Output is correct |
10 |
Correct |
3 ms |
15452 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
13 |
Correct |
13 ms |
16288 KB |
Output is correct |
14 |
Correct |
15 ms |
16220 KB |
Output is correct |
15 |
Correct |
3 ms |
15360 KB |
Output is correct |
16 |
Correct |
16 ms |
16220 KB |
Output is correct |
17 |
Correct |
3 ms |
15196 KB |
Output is correct |
18 |
Correct |
5 ms |
16220 KB |
Output is correct |
19 |
Correct |
11 ms |
16220 KB |
Output is correct |
20 |
Correct |
12 ms |
16116 KB |
Output is correct |
21 |
Correct |
10 ms |
16220 KB |
Output is correct |
22 |
Correct |
18 ms |
16260 KB |
Output is correct |
23 |
Correct |
2 ms |
15196 KB |
Output is correct |
24 |
Correct |
2 ms |
15196 KB |
Output is correct |
25 |
Correct |
3 ms |
15416 KB |
Output is correct |
26 |
Correct |
5 ms |
15964 KB |
Output is correct |
27 |
Correct |
4 ms |
15964 KB |
Output is correct |
28 |
Correct |
10 ms |
16324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
15452 KB |
Output is correct |
5 |
Correct |
3 ms |
15196 KB |
Output is correct |
6 |
Correct |
12 ms |
16240 KB |
Output is correct |
7 |
Correct |
17 ms |
16220 KB |
Output is correct |
8 |
Correct |
13 ms |
16220 KB |
Output is correct |
9 |
Correct |
11 ms |
16080 KB |
Output is correct |
10 |
Correct |
3 ms |
15452 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
13 |
Correct |
13 ms |
16288 KB |
Output is correct |
14 |
Correct |
15 ms |
16220 KB |
Output is correct |
15 |
Correct |
3 ms |
15360 KB |
Output is correct |
16 |
Correct |
16 ms |
16220 KB |
Output is correct |
17 |
Correct |
3 ms |
15196 KB |
Output is correct |
18 |
Correct |
5 ms |
16220 KB |
Output is correct |
19 |
Correct |
11 ms |
16220 KB |
Output is correct |
20 |
Correct |
12 ms |
16116 KB |
Output is correct |
21 |
Correct |
10 ms |
16220 KB |
Output is correct |
22 |
Correct |
18 ms |
16260 KB |
Output is correct |
23 |
Correct |
2 ms |
15196 KB |
Output is correct |
24 |
Correct |
2 ms |
15196 KB |
Output is correct |
25 |
Correct |
3 ms |
15416 KB |
Output is correct |
26 |
Correct |
5 ms |
15964 KB |
Output is correct |
27 |
Correct |
4 ms |
15964 KB |
Output is correct |
28 |
Correct |
10 ms |
16324 KB |
Output is correct |
29 |
Execution timed out |
2068 ms |
27504 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14936 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
15452 KB |
Output is correct |
5 |
Correct |
3 ms |
15196 KB |
Output is correct |
6 |
Correct |
12 ms |
16240 KB |
Output is correct |
7 |
Correct |
17 ms |
16220 KB |
Output is correct |
8 |
Correct |
13 ms |
16220 KB |
Output is correct |
9 |
Correct |
11 ms |
16080 KB |
Output is correct |
10 |
Correct |
3 ms |
15452 KB |
Output is correct |
11 |
Correct |
3 ms |
15196 KB |
Output is correct |
12 |
Correct |
3 ms |
15196 KB |
Output is correct |
13 |
Correct |
13 ms |
16288 KB |
Output is correct |
14 |
Correct |
15 ms |
16220 KB |
Output is correct |
15 |
Correct |
3 ms |
15360 KB |
Output is correct |
16 |
Correct |
16 ms |
16220 KB |
Output is correct |
17 |
Correct |
3 ms |
15196 KB |
Output is correct |
18 |
Correct |
5 ms |
16220 KB |
Output is correct |
19 |
Correct |
11 ms |
16220 KB |
Output is correct |
20 |
Correct |
12 ms |
16116 KB |
Output is correct |
21 |
Correct |
10 ms |
16220 KB |
Output is correct |
22 |
Correct |
18 ms |
16260 KB |
Output is correct |
23 |
Correct |
2 ms |
15196 KB |
Output is correct |
24 |
Correct |
2 ms |
15196 KB |
Output is correct |
25 |
Correct |
3 ms |
15416 KB |
Output is correct |
26 |
Correct |
5 ms |
15964 KB |
Output is correct |
27 |
Correct |
4 ms |
15964 KB |
Output is correct |
28 |
Correct |
10 ms |
16324 KB |
Output is correct |
29 |
Correct |
2 ms |
14940 KB |
Output is correct |
30 |
Correct |
18 ms |
16256 KB |
Output is correct |
31 |
Correct |
13 ms |
16220 KB |
Output is correct |
32 |
Correct |
4 ms |
15196 KB |
Output is correct |
33 |
Incorrect |
16 ms |
16220 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2067 ms |
26512 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |