#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
using ll = long long;
using ill = pair<int, ll>;
int N, x[300005];
ll s, profit;
vector<int> adj[300005];
priority_queue<ill, vector<ill>, greater<ill> > pq[300005];
void print(priority_queue<ill, vector<ill>, greater<ill> > tmp) {
while (!tmp.empty()) {
auto [r, p] = tmp.top();
cout << r << ' ' << p << '\n';
tmp.pop();
}
}
void dfs(int u) {
for (auto v : adj[u]) {
dfs(v);
if (pq[u].size() < pq[v].size()) {
swap(pq[u], pq[v]);
}
while (!pq[v].empty()) {
pq[u].push(pq[v].top());
pq[v].pop();
}
}
if (x[u] >= 0) {
pq[u].emplace(0, x[u]);
} else {
int r = -x[u];
ll p = x[u];
while (!pq[u].empty() && r >= pq[u].top().first) {
p += pq[u].top().second;
pq[u].pop();
}
while (!pq[u].empty() && p < 0) {
auto [nr, np] = pq[u].top();
r = max((ll)r, nr - p);
p += np;
pq[u].pop();
}
if (p >= 0) {
pq[u].emplace(r, p);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> s;
for (int i = 1, p; i <= N; i++) {
cin >> x[i] >> p;
adj[p].pb(i);
}
dfs(0);
while (!pq[0].empty() && s >= pq[0].top().first) {
s += pq[0].top().second;
profit += pq[0].top().second;
pq[0].pop();
}
cout << profit << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
47936 KB |
Output is correct |
2 |
Correct |
150 ms |
38080 KB |
Output is correct |
3 |
Correct |
114 ms |
32580 KB |
Output is correct |
4 |
Correct |
90 ms |
38596 KB |
Output is correct |
5 |
Correct |
107 ms |
45068 KB |
Output is correct |
6 |
Correct |
91 ms |
35528 KB |
Output is correct |
7 |
Correct |
152 ms |
42500 KB |
Output is correct |
8 |
Correct |
109 ms |
34120 KB |
Output is correct |
9 |
Correct |
84 ms |
41476 KB |
Output is correct |
10 |
Correct |
81 ms |
45604 KB |
Output is correct |
11 |
Correct |
190 ms |
49732 KB |
Output is correct |
12 |
Correct |
149 ms |
41152 KB |
Output is correct |
13 |
Correct |
181 ms |
46044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
17244 KB |
Output is correct |
6 |
Correct |
3 ms |
17240 KB |
Output is correct |
7 |
Correct |
4 ms |
16984 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Incorrect |
3 ms |
16988 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
17244 KB |
Output is correct |
6 |
Correct |
3 ms |
17240 KB |
Output is correct |
7 |
Correct |
4 ms |
16984 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Incorrect |
3 ms |
16988 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
2 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
17244 KB |
Output is correct |
6 |
Correct |
3 ms |
17240 KB |
Output is correct |
7 |
Correct |
4 ms |
16984 KB |
Output is correct |
8 |
Correct |
4 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Incorrect |
3 ms |
16988 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
47936 KB |
Output is correct |
2 |
Correct |
150 ms |
38080 KB |
Output is correct |
3 |
Correct |
114 ms |
32580 KB |
Output is correct |
4 |
Correct |
90 ms |
38596 KB |
Output is correct |
5 |
Correct |
107 ms |
45068 KB |
Output is correct |
6 |
Correct |
91 ms |
35528 KB |
Output is correct |
7 |
Correct |
152 ms |
42500 KB |
Output is correct |
8 |
Correct |
109 ms |
34120 KB |
Output is correct |
9 |
Correct |
84 ms |
41476 KB |
Output is correct |
10 |
Correct |
81 ms |
45604 KB |
Output is correct |
11 |
Correct |
190 ms |
49732 KB |
Output is correct |
12 |
Correct |
149 ms |
41152 KB |
Output is correct |
13 |
Correct |
181 ms |
46044 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
2 ms |
16988 KB |
Output is correct |
16 |
Correct |
2 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
17244 KB |
Output is correct |
19 |
Correct |
3 ms |
17240 KB |
Output is correct |
20 |
Correct |
4 ms |
16984 KB |
Output is correct |
21 |
Correct |
4 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16988 KB |
Output is correct |
23 |
Incorrect |
3 ms |
16988 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |