#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define pb push_back
#define ld long double
#define ll long long
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 3e5 + 10;
vector<int> g[N];
multiset<ll> st[N];
ll k[N], b[N];
int c[N];
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i < n + m; i++) {
int p;
cin >> p >> c[i];
p--;
g[p].pb(i);
}
for (int i = n; i < n + m; i++) {
k[i] = 1;
b[i] = -c[i];
st[i].insert(c[i]);
st[i].insert(c[i]);
}
for (int i = n - 1; i >= 0; i--) {
for (auto to : g[i]) {
k[i] += k[to];
b[i] += b[to];
if (st[to].size() > st[i].size()) {
swap(st[i], st[to]);
}
for (auto x : st[to]) {
st[i].insert(x);
}
}
while (k[i] > 1) {
k[i]--;
b[i] += *st[i].rbegin();
st[i].erase(st[i].find(*st[i].rbegin()));
}
if (c[i] > 0) {
ll slope1 = *st[i].rbegin();
st[i].erase(st[i].find(slope1));
ll slope0 = *st[i].rbegin();
st[i].erase(st[i].find(slope0));
st[i].insert(slope0 + c[i]);
st[i].insert(slope1 + c[i]);
b[i] -= c[i];
}
}
while (k[0] > 0) {
k[0]--;
b[0] += *st[0].rbegin();
st[0].erase(st[0].find(*st[0].rbegin()));
}
cout << b[0] << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
10 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21400 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21500 KB |
Output is correct |
9 |
Correct |
10 ms |
21504 KB |
Output is correct |
10 |
Correct |
12 ms |
21464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
11 ms |
21460 KB |
Output is correct |
3 |
Correct |
10 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21460 KB |
Output is correct |
5 |
Correct |
10 ms |
21496 KB |
Output is correct |
6 |
Correct |
10 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21456 KB |
Output is correct |
8 |
Correct |
10 ms |
21440 KB |
Output is correct |
9 |
Correct |
10 ms |
21388 KB |
Output is correct |
10 |
Correct |
11 ms |
21484 KB |
Output is correct |
11 |
Correct |
10 ms |
21460 KB |
Output is correct |
12 |
Correct |
11 ms |
21460 KB |
Output is correct |
13 |
Correct |
10 ms |
21460 KB |
Output is correct |
14 |
Correct |
12 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21464 KB |
Output is correct |
16 |
Correct |
11 ms |
21460 KB |
Output is correct |
17 |
Correct |
12 ms |
21460 KB |
Output is correct |
18 |
Correct |
13 ms |
21432 KB |
Output is correct |
19 |
Correct |
11 ms |
21408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
10 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21400 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21500 KB |
Output is correct |
9 |
Correct |
10 ms |
21504 KB |
Output is correct |
10 |
Correct |
12 ms |
21464 KB |
Output is correct |
11 |
Correct |
11 ms |
21460 KB |
Output is correct |
12 |
Correct |
11 ms |
21460 KB |
Output is correct |
13 |
Correct |
10 ms |
21460 KB |
Output is correct |
14 |
Correct |
10 ms |
21460 KB |
Output is correct |
15 |
Correct |
10 ms |
21496 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
11 ms |
21456 KB |
Output is correct |
18 |
Correct |
10 ms |
21440 KB |
Output is correct |
19 |
Correct |
10 ms |
21388 KB |
Output is correct |
20 |
Correct |
11 ms |
21484 KB |
Output is correct |
21 |
Correct |
10 ms |
21460 KB |
Output is correct |
22 |
Correct |
11 ms |
21460 KB |
Output is correct |
23 |
Correct |
10 ms |
21460 KB |
Output is correct |
24 |
Correct |
12 ms |
21460 KB |
Output is correct |
25 |
Correct |
11 ms |
21464 KB |
Output is correct |
26 |
Correct |
11 ms |
21460 KB |
Output is correct |
27 |
Correct |
12 ms |
21460 KB |
Output is correct |
28 |
Correct |
13 ms |
21432 KB |
Output is correct |
29 |
Correct |
11 ms |
21408 KB |
Output is correct |
30 |
Correct |
13 ms |
21460 KB |
Output is correct |
31 |
Correct |
14 ms |
21516 KB |
Output is correct |
32 |
Correct |
12 ms |
21620 KB |
Output is correct |
33 |
Correct |
13 ms |
21716 KB |
Output is correct |
34 |
Correct |
14 ms |
21872 KB |
Output is correct |
35 |
Correct |
14 ms |
21872 KB |
Output is correct |
36 |
Correct |
13 ms |
21936 KB |
Output is correct |
37 |
Correct |
14 ms |
22072 KB |
Output is correct |
38 |
Correct |
14 ms |
22100 KB |
Output is correct |
39 |
Correct |
14 ms |
22148 KB |
Output is correct |
40 |
Correct |
17 ms |
21716 KB |
Output is correct |
41 |
Correct |
17 ms |
21748 KB |
Output is correct |
42 |
Correct |
13 ms |
21716 KB |
Output is correct |
43 |
Correct |
15 ms |
21972 KB |
Output is correct |
44 |
Correct |
14 ms |
22072 KB |
Output is correct |
45 |
Correct |
16 ms |
21992 KB |
Output is correct |
46 |
Correct |
14 ms |
22612 KB |
Output is correct |
47 |
Correct |
15 ms |
22516 KB |
Output is correct |
48 |
Correct |
14 ms |
22384 KB |
Output is correct |
49 |
Correct |
15 ms |
22508 KB |
Output is correct |
50 |
Correct |
14 ms |
22228 KB |
Output is correct |
51 |
Correct |
13 ms |
22276 KB |
Output is correct |
52 |
Correct |
14 ms |
22328 KB |
Output is correct |
53 |
Correct |
16 ms |
22388 KB |
Output is correct |
54 |
Correct |
17 ms |
22612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21460 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
10 ms |
21460 KB |
Output is correct |
4 |
Correct |
10 ms |
21460 KB |
Output is correct |
5 |
Correct |
11 ms |
21400 KB |
Output is correct |
6 |
Correct |
11 ms |
21460 KB |
Output is correct |
7 |
Correct |
11 ms |
21460 KB |
Output is correct |
8 |
Correct |
11 ms |
21500 KB |
Output is correct |
9 |
Correct |
10 ms |
21504 KB |
Output is correct |
10 |
Correct |
12 ms |
21464 KB |
Output is correct |
11 |
Correct |
11 ms |
21460 KB |
Output is correct |
12 |
Correct |
11 ms |
21460 KB |
Output is correct |
13 |
Correct |
10 ms |
21460 KB |
Output is correct |
14 |
Correct |
10 ms |
21460 KB |
Output is correct |
15 |
Correct |
10 ms |
21496 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
11 ms |
21456 KB |
Output is correct |
18 |
Correct |
10 ms |
21440 KB |
Output is correct |
19 |
Correct |
10 ms |
21388 KB |
Output is correct |
20 |
Correct |
11 ms |
21484 KB |
Output is correct |
21 |
Correct |
10 ms |
21460 KB |
Output is correct |
22 |
Correct |
11 ms |
21460 KB |
Output is correct |
23 |
Correct |
10 ms |
21460 KB |
Output is correct |
24 |
Correct |
12 ms |
21460 KB |
Output is correct |
25 |
Correct |
11 ms |
21464 KB |
Output is correct |
26 |
Correct |
11 ms |
21460 KB |
Output is correct |
27 |
Correct |
12 ms |
21460 KB |
Output is correct |
28 |
Correct |
13 ms |
21432 KB |
Output is correct |
29 |
Correct |
11 ms |
21408 KB |
Output is correct |
30 |
Correct |
13 ms |
21460 KB |
Output is correct |
31 |
Correct |
14 ms |
21516 KB |
Output is correct |
32 |
Correct |
12 ms |
21620 KB |
Output is correct |
33 |
Correct |
13 ms |
21716 KB |
Output is correct |
34 |
Correct |
14 ms |
21872 KB |
Output is correct |
35 |
Correct |
14 ms |
21872 KB |
Output is correct |
36 |
Correct |
13 ms |
21936 KB |
Output is correct |
37 |
Correct |
14 ms |
22072 KB |
Output is correct |
38 |
Correct |
14 ms |
22100 KB |
Output is correct |
39 |
Correct |
14 ms |
22148 KB |
Output is correct |
40 |
Correct |
17 ms |
21716 KB |
Output is correct |
41 |
Correct |
17 ms |
21748 KB |
Output is correct |
42 |
Correct |
13 ms |
21716 KB |
Output is correct |
43 |
Correct |
15 ms |
21972 KB |
Output is correct |
44 |
Correct |
14 ms |
22072 KB |
Output is correct |
45 |
Correct |
16 ms |
21992 KB |
Output is correct |
46 |
Correct |
14 ms |
22612 KB |
Output is correct |
47 |
Correct |
15 ms |
22516 KB |
Output is correct |
48 |
Correct |
14 ms |
22384 KB |
Output is correct |
49 |
Correct |
15 ms |
22508 KB |
Output is correct |
50 |
Correct |
14 ms |
22228 KB |
Output is correct |
51 |
Correct |
13 ms |
22276 KB |
Output is correct |
52 |
Correct |
14 ms |
22328 KB |
Output is correct |
53 |
Correct |
16 ms |
22388 KB |
Output is correct |
54 |
Correct |
17 ms |
22612 KB |
Output is correct |
55 |
Correct |
22 ms |
23408 KB |
Output is correct |
56 |
Correct |
49 ms |
28724 KB |
Output is correct |
57 |
Correct |
78 ms |
34344 KB |
Output is correct |
58 |
Correct |
95 ms |
38132 KB |
Output is correct |
59 |
Correct |
124 ms |
43684 KB |
Output is correct |
60 |
Correct |
158 ms |
49484 KB |
Output is correct |
61 |
Correct |
179 ms |
53312 KB |
Output is correct |
62 |
Correct |
199 ms |
57148 KB |
Output is correct |
63 |
Correct |
260 ms |
64964 KB |
Output is correct |
64 |
Correct |
259 ms |
65740 KB |
Output is correct |
65 |
Correct |
113 ms |
41488 KB |
Output is correct |
66 |
Correct |
111 ms |
41628 KB |
Output is correct |
67 |
Correct |
106 ms |
41668 KB |
Output is correct |
68 |
Correct |
202 ms |
57872 KB |
Output is correct |
69 |
Correct |
227 ms |
58176 KB |
Output is correct |
70 |
Correct |
223 ms |
58272 KB |
Output is correct |
71 |
Correct |
276 ms |
102328 KB |
Output is correct |
72 |
Correct |
280 ms |
101740 KB |
Output is correct |
73 |
Correct |
284 ms |
88968 KB |
Output is correct |
74 |
Correct |
272 ms |
89116 KB |
Output is correct |
75 |
Correct |
289 ms |
88448 KB |
Output is correct |
76 |
Correct |
269 ms |
88744 KB |
Output is correct |
77 |
Correct |
304 ms |
86600 KB |
Output is correct |
78 |
Correct |
318 ms |
83788 KB |
Output is correct |
79 |
Correct |
329 ms |
88376 KB |
Output is correct |