#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 + p >= 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);
}
}
//~ cout << "PRINTING " << u << '\n';
//~ print(pq[u]);
}
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 |
170 ms |
48088 KB |
Output is correct |
2 |
Correct |
136 ms |
39360 KB |
Output is correct |
3 |
Correct |
104 ms |
33860 KB |
Output is correct |
4 |
Correct |
88 ms |
39108 KB |
Output is correct |
5 |
Correct |
73 ms |
40628 KB |
Output is correct |
6 |
Correct |
92 ms |
35612 KB |
Output is correct |
7 |
Correct |
157 ms |
42936 KB |
Output is correct |
8 |
Correct |
97 ms |
32836 KB |
Output is correct |
9 |
Correct |
78 ms |
41156 KB |
Output is correct |
10 |
Correct |
67 ms |
41044 KB |
Output is correct |
11 |
Correct |
187 ms |
49480 KB |
Output is correct |
12 |
Correct |
146 ms |
41156 KB |
Output is correct |
13 |
Correct |
164 ms |
46148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
17012 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
17240 KB |
Output is correct |
6 |
Correct |
3 ms |
17244 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
17020 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
17244 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16984 KB |
Output is correct |
14 |
Correct |
3 ms |
17156 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
17244 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
3 ms |
16988 KB |
Output is correct |
20 |
Correct |
3 ms |
16988 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16996 KB |
Output is correct |
23 |
Correct |
4 ms |
17240 KB |
Output is correct |
24 |
Correct |
3 ms |
16984 KB |
Output is correct |
25 |
Correct |
3 ms |
16988 KB |
Output is correct |
26 |
Correct |
3 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
4 ms |
17240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
17012 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
17240 KB |
Output is correct |
6 |
Correct |
3 ms |
17244 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
17020 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
17244 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16984 KB |
Output is correct |
14 |
Correct |
3 ms |
17156 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
17244 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
3 ms |
16988 KB |
Output is correct |
20 |
Correct |
3 ms |
16988 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16996 KB |
Output is correct |
23 |
Correct |
4 ms |
17240 KB |
Output is correct |
24 |
Correct |
3 ms |
16984 KB |
Output is correct |
25 |
Correct |
3 ms |
16988 KB |
Output is correct |
26 |
Correct |
3 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
4 ms |
17240 KB |
Output is correct |
29 |
Correct |
74 ms |
35524 KB |
Output is correct |
30 |
Correct |
76 ms |
34412 KB |
Output is correct |
31 |
Correct |
67 ms |
32964 KB |
Output is correct |
32 |
Correct |
86 ms |
55376 KB |
Output is correct |
33 |
Correct |
76 ms |
44488 KB |
Output is correct |
34 |
Correct |
73 ms |
40276 KB |
Output is correct |
35 |
Correct |
36 ms |
24512 KB |
Output is correct |
36 |
Correct |
72 ms |
34764 KB |
Output is correct |
37 |
Correct |
82 ms |
36288 KB |
Output is correct |
38 |
Correct |
69 ms |
55380 KB |
Output is correct |
39 |
Correct |
84 ms |
46408 KB |
Output is correct |
40 |
Correct |
82 ms |
36064 KB |
Output is correct |
41 |
Correct |
74 ms |
31948 KB |
Output is correct |
42 |
Correct |
33 ms |
28112 KB |
Output is correct |
43 |
Correct |
74 ms |
40340 KB |
Output is correct |
44 |
Correct |
70 ms |
54764 KB |
Output is correct |
45 |
Correct |
74 ms |
45400 KB |
Output is correct |
46 |
Correct |
75 ms |
40012 KB |
Output is correct |
47 |
Correct |
84 ms |
32016 KB |
Output is correct |
48 |
Correct |
76 ms |
33728 KB |
Output is correct |
49 |
Correct |
78 ms |
35268 KB |
Output is correct |
50 |
Correct |
74 ms |
54960 KB |
Output is correct |
51 |
Correct |
68 ms |
38240 KB |
Output is correct |
52 |
Correct |
80 ms |
35848 KB |
Output is correct |
53 |
Correct |
49 ms |
32716 KB |
Output is correct |
54 |
Correct |
72 ms |
34004 KB |
Output is correct |
55 |
Correct |
79 ms |
34564 KB |
Output is correct |
56 |
Correct |
74 ms |
54812 KB |
Output is correct |
57 |
Correct |
81 ms |
42316 KB |
Output is correct |
58 |
Correct |
75 ms |
35736 KB |
Output is correct |
59 |
Correct |
78 ms |
36548 KB |
Output is correct |
60 |
Correct |
61 ms |
33472 KB |
Output is correct |
61 |
Correct |
72 ms |
39872 KB |
Output is correct |
62 |
Correct |
93 ms |
40260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
2 ms |
17012 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
17240 KB |
Output is correct |
6 |
Correct |
3 ms |
17244 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
17020 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
17244 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16984 KB |
Output is correct |
14 |
Correct |
3 ms |
17156 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
17244 KB |
Output is correct |
18 |
Correct |
3 ms |
16988 KB |
Output is correct |
19 |
Correct |
3 ms |
16988 KB |
Output is correct |
20 |
Correct |
3 ms |
16988 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16996 KB |
Output is correct |
23 |
Correct |
4 ms |
17240 KB |
Output is correct |
24 |
Correct |
3 ms |
16984 KB |
Output is correct |
25 |
Correct |
3 ms |
16988 KB |
Output is correct |
26 |
Correct |
3 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
4 ms |
17240 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
3 ms |
17012 KB |
Output is correct |
31 |
Correct |
3 ms |
16988 KB |
Output is correct |
32 |
Correct |
4 ms |
16988 KB |
Output is correct |
33 |
Correct |
3 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
16984 KB |
Output is correct |
35 |
Correct |
3 ms |
17140 KB |
Output is correct |
36 |
Correct |
3 ms |
16988 KB |
Output is correct |
37 |
Correct |
3 ms |
16988 KB |
Output is correct |
38 |
Correct |
3 ms |
16988 KB |
Output is correct |
39 |
Correct |
4 ms |
17148 KB |
Output is correct |
40 |
Correct |
3 ms |
16988 KB |
Output is correct |
41 |
Correct |
3 ms |
17080 KB |
Output is correct |
42 |
Correct |
3 ms |
16988 KB |
Output is correct |
43 |
Correct |
3 ms |
16988 KB |
Output is correct |
44 |
Correct |
4 ms |
17072 KB |
Output is correct |
45 |
Correct |
4 ms |
17068 KB |
Output is correct |
46 |
Correct |
3 ms |
17020 KB |
Output is correct |
47 |
Correct |
3 ms |
16984 KB |
Output is correct |
48 |
Correct |
3 ms |
16984 KB |
Output is correct |
49 |
Correct |
4 ms |
16984 KB |
Output is correct |
50 |
Correct |
3 ms |
16988 KB |
Output is correct |
51 |
Correct |
3 ms |
16988 KB |
Output is correct |
52 |
Correct |
3 ms |
16984 KB |
Output is correct |
53 |
Correct |
3 ms |
16988 KB |
Output is correct |
54 |
Correct |
4 ms |
16984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
48088 KB |
Output is correct |
2 |
Correct |
136 ms |
39360 KB |
Output is correct |
3 |
Correct |
104 ms |
33860 KB |
Output is correct |
4 |
Correct |
88 ms |
39108 KB |
Output is correct |
5 |
Correct |
73 ms |
40628 KB |
Output is correct |
6 |
Correct |
92 ms |
35612 KB |
Output is correct |
7 |
Correct |
157 ms |
42936 KB |
Output is correct |
8 |
Correct |
97 ms |
32836 KB |
Output is correct |
9 |
Correct |
78 ms |
41156 KB |
Output is correct |
10 |
Correct |
67 ms |
41044 KB |
Output is correct |
11 |
Correct |
187 ms |
49480 KB |
Output is correct |
12 |
Correct |
146 ms |
41156 KB |
Output is correct |
13 |
Correct |
164 ms |
46148 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
2 ms |
17012 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
17240 KB |
Output is correct |
19 |
Correct |
3 ms |
17244 KB |
Output is correct |
20 |
Correct |
3 ms |
16988 KB |
Output is correct |
21 |
Correct |
3 ms |
17020 KB |
Output is correct |
22 |
Correct |
3 ms |
16988 KB |
Output is correct |
23 |
Correct |
4 ms |
16988 KB |
Output is correct |
24 |
Correct |
3 ms |
17244 KB |
Output is correct |
25 |
Correct |
3 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16984 KB |
Output is correct |
27 |
Correct |
3 ms |
17156 KB |
Output is correct |
28 |
Correct |
3 ms |
16988 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
3 ms |
17244 KB |
Output is correct |
31 |
Correct |
3 ms |
16988 KB |
Output is correct |
32 |
Correct |
3 ms |
16988 KB |
Output is correct |
33 |
Correct |
3 ms |
16988 KB |
Output is correct |
34 |
Correct |
3 ms |
16988 KB |
Output is correct |
35 |
Correct |
3 ms |
16996 KB |
Output is correct |
36 |
Correct |
4 ms |
17240 KB |
Output is correct |
37 |
Correct |
3 ms |
16984 KB |
Output is correct |
38 |
Correct |
3 ms |
16988 KB |
Output is correct |
39 |
Correct |
3 ms |
16988 KB |
Output is correct |
40 |
Correct |
3 ms |
16988 KB |
Output is correct |
41 |
Correct |
4 ms |
17240 KB |
Output is correct |
42 |
Correct |
74 ms |
35524 KB |
Output is correct |
43 |
Correct |
76 ms |
34412 KB |
Output is correct |
44 |
Correct |
67 ms |
32964 KB |
Output is correct |
45 |
Correct |
86 ms |
55376 KB |
Output is correct |
46 |
Correct |
76 ms |
44488 KB |
Output is correct |
47 |
Correct |
73 ms |
40276 KB |
Output is correct |
48 |
Correct |
36 ms |
24512 KB |
Output is correct |
49 |
Correct |
72 ms |
34764 KB |
Output is correct |
50 |
Correct |
82 ms |
36288 KB |
Output is correct |
51 |
Correct |
69 ms |
55380 KB |
Output is correct |
52 |
Correct |
84 ms |
46408 KB |
Output is correct |
53 |
Correct |
82 ms |
36064 KB |
Output is correct |
54 |
Correct |
74 ms |
31948 KB |
Output is correct |
55 |
Correct |
33 ms |
28112 KB |
Output is correct |
56 |
Correct |
74 ms |
40340 KB |
Output is correct |
57 |
Correct |
70 ms |
54764 KB |
Output is correct |
58 |
Correct |
74 ms |
45400 KB |
Output is correct |
59 |
Correct |
75 ms |
40012 KB |
Output is correct |
60 |
Correct |
84 ms |
32016 KB |
Output is correct |
61 |
Correct |
76 ms |
33728 KB |
Output is correct |
62 |
Correct |
78 ms |
35268 KB |
Output is correct |
63 |
Correct |
74 ms |
54960 KB |
Output is correct |
64 |
Correct |
68 ms |
38240 KB |
Output is correct |
65 |
Correct |
80 ms |
35848 KB |
Output is correct |
66 |
Correct |
49 ms |
32716 KB |
Output is correct |
67 |
Correct |
72 ms |
34004 KB |
Output is correct |
68 |
Correct |
79 ms |
34564 KB |
Output is correct |
69 |
Correct |
74 ms |
54812 KB |
Output is correct |
70 |
Correct |
81 ms |
42316 KB |
Output is correct |
71 |
Correct |
75 ms |
35736 KB |
Output is correct |
72 |
Correct |
78 ms |
36548 KB |
Output is correct |
73 |
Correct |
61 ms |
33472 KB |
Output is correct |
74 |
Correct |
72 ms |
39872 KB |
Output is correct |
75 |
Correct |
93 ms |
40260 KB |
Output is correct |
76 |
Correct |
3 ms |
16988 KB |
Output is correct |
77 |
Correct |
3 ms |
17012 KB |
Output is correct |
78 |
Correct |
3 ms |
16988 KB |
Output is correct |
79 |
Correct |
4 ms |
16988 KB |
Output is correct |
80 |
Correct |
3 ms |
16988 KB |
Output is correct |
81 |
Correct |
4 ms |
16984 KB |
Output is correct |
82 |
Correct |
3 ms |
17140 KB |
Output is correct |
83 |
Correct |
3 ms |
16988 KB |
Output is correct |
84 |
Correct |
3 ms |
16988 KB |
Output is correct |
85 |
Correct |
3 ms |
16988 KB |
Output is correct |
86 |
Correct |
4 ms |
17148 KB |
Output is correct |
87 |
Correct |
3 ms |
16988 KB |
Output is correct |
88 |
Correct |
3 ms |
17080 KB |
Output is correct |
89 |
Correct |
3 ms |
16988 KB |
Output is correct |
90 |
Correct |
3 ms |
16988 KB |
Output is correct |
91 |
Correct |
4 ms |
17072 KB |
Output is correct |
92 |
Correct |
4 ms |
17068 KB |
Output is correct |
93 |
Correct |
3 ms |
17020 KB |
Output is correct |
94 |
Correct |
3 ms |
16984 KB |
Output is correct |
95 |
Correct |
3 ms |
16984 KB |
Output is correct |
96 |
Correct |
4 ms |
16984 KB |
Output is correct |
97 |
Correct |
3 ms |
16988 KB |
Output is correct |
98 |
Correct |
3 ms |
16988 KB |
Output is correct |
99 |
Correct |
3 ms |
16984 KB |
Output is correct |
100 |
Correct |
3 ms |
16988 KB |
Output is correct |
101 |
Correct |
4 ms |
16984 KB |
Output is correct |
102 |
Incorrect |
166 ms |
45896 KB |
Output isn't correct |
103 |
Halted |
0 ms |
0 KB |
- |