#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define FOR(i, s, e) for(int i = s; i < e; i++)
#define REP(i, n) for(int i = 0; i < int(n); i++)
#define all(v) v.begin(), v.end()
#define fir first
#define sec second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
vll x;
vvi c;
vpll res;
ll dfs(int a) {
ll sum = x[a];
REP(i, c[a].size()) {
sum += dfs(c[a][i]);
}
return max(sum, 0ll);
}
void dfs2(int a, ll pr,ll mp, ll lo) {
ll nex = pr + x[a];
if(nex > mp) {
res.push_back({lo-mp, nex-mp});
}
REP(i, c[a].size()) {
dfs2(c[a][i], nex, max(mp, nex), min(lo, nex));
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, a;
ll s;
cin>>n>>s;
x.resize(n+1);
c.resize(n+1);
x[0] = 0ll;
FOR(i, 1, n+1) {
cin>>x[i]>>a;
c[a].push_back(i);
}
if(s == 1e18) {
cout<<dfs(0);
}
else {
ll ans = 0;
REP(i, c[0].size()) {
dfs2(c[0][i], 0ll, 0ll, 0ll);
}
sort(all(res), greater<pll>());
REP(i, res.size()) {
if(res[i].fir + s < 0) {
break;
}
s += res[i].sec;
ans += res[i].sec;
}
cout << ans;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
17832 KB |
Output is correct |
2 |
Correct |
58 ms |
16468 KB |
Output is correct |
3 |
Correct |
54 ms |
16204 KB |
Output is correct |
4 |
Correct |
54 ms |
19692 KB |
Output is correct |
5 |
Correct |
56 ms |
20816 KB |
Output is correct |
6 |
Correct |
44 ms |
11348 KB |
Output is correct |
7 |
Correct |
64 ms |
16516 KB |
Output is correct |
8 |
Correct |
59 ms |
16176 KB |
Output is correct |
9 |
Correct |
48 ms |
19792 KB |
Output is correct |
10 |
Correct |
51 ms |
21332 KB |
Output is correct |
11 |
Correct |
66 ms |
16780 KB |
Output is correct |
12 |
Correct |
54 ms |
14160 KB |
Output is correct |
13 |
Correct |
66 ms |
16968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
556 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
556 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
58 ms |
24172 KB |
Output is correct |
30 |
Correct |
58 ms |
23232 KB |
Output is correct |
31 |
Correct |
55 ms |
21316 KB |
Output is correct |
32 |
Correct |
74 ms |
53688 KB |
Output is correct |
33 |
Correct |
72 ms |
39880 KB |
Output is correct |
34 |
Correct |
57 ms |
32520 KB |
Output is correct |
35 |
Correct |
29 ms |
11724 KB |
Output is correct |
36 |
Correct |
60 ms |
25552 KB |
Output is correct |
37 |
Correct |
66 ms |
23776 KB |
Output is correct |
38 |
Correct |
76 ms |
53960 KB |
Output is correct |
39 |
Correct |
72 ms |
40392 KB |
Output is correct |
40 |
Correct |
63 ms |
29124 KB |
Output is correct |
41 |
Correct |
55 ms |
21964 KB |
Output is correct |
42 |
Correct |
31 ms |
15616 KB |
Output is correct |
43 |
Correct |
61 ms |
31432 KB |
Output is correct |
44 |
Correct |
101 ms |
53956 KB |
Output is correct |
45 |
Correct |
70 ms |
40392 KB |
Output is correct |
46 |
Correct |
61 ms |
31688 KB |
Output is correct |
47 |
Correct |
58 ms |
21964 KB |
Output is correct |
48 |
Correct |
63 ms |
21828 KB |
Output is correct |
49 |
Correct |
64 ms |
24264 KB |
Output is correct |
50 |
Correct |
80 ms |
54208 KB |
Output is correct |
51 |
Correct |
46 ms |
29640 KB |
Output is correct |
52 |
Correct |
59 ms |
30408 KB |
Output is correct |
53 |
Correct |
41 ms |
22988 KB |
Output is correct |
54 |
Correct |
56 ms |
22180 KB |
Output is correct |
55 |
Correct |
64 ms |
22984 KB |
Output is correct |
56 |
Correct |
81 ms |
53440 KB |
Output is correct |
57 |
Correct |
75 ms |
41676 KB |
Output is correct |
58 |
Correct |
59 ms |
29124 KB |
Output is correct |
59 |
Correct |
61 ms |
27084 KB |
Output is correct |
60 |
Correct |
56 ms |
21328 KB |
Output is correct |
61 |
Correct |
61 ms |
31436 KB |
Output is correct |
62 |
Correct |
70 ms |
31688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
556 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
17832 KB |
Output is correct |
2 |
Correct |
58 ms |
16468 KB |
Output is correct |
3 |
Correct |
54 ms |
16204 KB |
Output is correct |
4 |
Correct |
54 ms |
19692 KB |
Output is correct |
5 |
Correct |
56 ms |
20816 KB |
Output is correct |
6 |
Correct |
44 ms |
11348 KB |
Output is correct |
7 |
Correct |
64 ms |
16516 KB |
Output is correct |
8 |
Correct |
59 ms |
16176 KB |
Output is correct |
9 |
Correct |
48 ms |
19792 KB |
Output is correct |
10 |
Correct |
51 ms |
21332 KB |
Output is correct |
11 |
Correct |
66 ms |
16780 KB |
Output is correct |
12 |
Correct |
54 ms |
14160 KB |
Output is correct |
13 |
Correct |
66 ms |
16968 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
0 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
556 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
58 ms |
24172 KB |
Output is correct |
43 |
Correct |
58 ms |
23232 KB |
Output is correct |
44 |
Correct |
55 ms |
21316 KB |
Output is correct |
45 |
Correct |
74 ms |
53688 KB |
Output is correct |
46 |
Correct |
72 ms |
39880 KB |
Output is correct |
47 |
Correct |
57 ms |
32520 KB |
Output is correct |
48 |
Correct |
29 ms |
11724 KB |
Output is correct |
49 |
Correct |
60 ms |
25552 KB |
Output is correct |
50 |
Correct |
66 ms |
23776 KB |
Output is correct |
51 |
Correct |
76 ms |
53960 KB |
Output is correct |
52 |
Correct |
72 ms |
40392 KB |
Output is correct |
53 |
Correct |
63 ms |
29124 KB |
Output is correct |
54 |
Correct |
55 ms |
21964 KB |
Output is correct |
55 |
Correct |
31 ms |
15616 KB |
Output is correct |
56 |
Correct |
61 ms |
31432 KB |
Output is correct |
57 |
Correct |
101 ms |
53956 KB |
Output is correct |
58 |
Correct |
70 ms |
40392 KB |
Output is correct |
59 |
Correct |
61 ms |
31688 KB |
Output is correct |
60 |
Correct |
58 ms |
21964 KB |
Output is correct |
61 |
Correct |
63 ms |
21828 KB |
Output is correct |
62 |
Correct |
64 ms |
24264 KB |
Output is correct |
63 |
Correct |
80 ms |
54208 KB |
Output is correct |
64 |
Correct |
46 ms |
29640 KB |
Output is correct |
65 |
Correct |
59 ms |
30408 KB |
Output is correct |
66 |
Correct |
41 ms |
22988 KB |
Output is correct |
67 |
Correct |
56 ms |
22180 KB |
Output is correct |
68 |
Correct |
64 ms |
22984 KB |
Output is correct |
69 |
Correct |
81 ms |
53440 KB |
Output is correct |
70 |
Correct |
75 ms |
41676 KB |
Output is correct |
71 |
Correct |
59 ms |
29124 KB |
Output is correct |
72 |
Correct |
61 ms |
27084 KB |
Output is correct |
73 |
Correct |
56 ms |
21328 KB |
Output is correct |
74 |
Correct |
61 ms |
31436 KB |
Output is correct |
75 |
Correct |
70 ms |
31688 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
600 KB |
Output is correct |
78 |
Correct |
1 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
348 KB |
Output is correct |
80 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
81 |
Halted |
0 ms |
0 KB |
- |