#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5+5;
struct slope {
ll m, c;
priority_queue<ll> pq;
slope& operator+=(slope& ot) {
m += ot.m, c += ot.c;
if(pq.size() < ot.pq.size()) swap(pq, ot.pq);
while(!ot.pq.empty())
pq.push(ot.pq.top()), ot.pq.pop();
return *this;
}
};
slope f[N];
ll cost[N];
int n, m;
vector<int> adj[N];
void read() {
cin >> n >> m;
for(int i = 2; i <= n+m; i++) {
int p; cin >> p >> cost[i];
adj[p].push_back(i);
}
}
void dfs(int u) {
if(adj[u].empty()) {
f[u].m = 1, f[u].c = -cost[u];
f[u].pq.push(cost[u]);
f[u].pq.push(cost[u]);
return;
}
for(int v : adj[u]) {
dfs(v);
f[u] += f[v];
}
//reducing largest slope
while(f[u].m > 1) {
f[u].m -= 1;
f[u].c += f[u].pq.top();
f[u].pq.pop();
}
//adding parent edge
ll x = f[u].pq.top() + cost[u]; f[u].pq.pop();
ll y = f[u].pq.top() + cost[u]; f[u].pq.pop();
f[u].pq.push(x); f[u].pq.push(y);
f[u].c -= cost[u];
}
void solve() {
dfs(1);
ll ans = f[1].m * f[1].pq.top() + f[1].c;
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
read();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21480 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
16 ms |
21504 KB |
Output is correct |
10 |
Correct |
16 ms |
21504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21504 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
16 ms |
21504 KB |
Output is correct |
11 |
Correct |
17 ms |
21504 KB |
Output is correct |
12 |
Correct |
16 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21524 KB |
Output is correct |
14 |
Correct |
17 ms |
21632 KB |
Output is correct |
15 |
Correct |
17 ms |
21504 KB |
Output is correct |
16 |
Correct |
17 ms |
21504 KB |
Output is correct |
17 |
Correct |
16 ms |
21504 KB |
Output is correct |
18 |
Correct |
16 ms |
21504 KB |
Output is correct |
19 |
Correct |
22 ms |
21504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21480 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
16 ms |
21504 KB |
Output is correct |
10 |
Correct |
16 ms |
21504 KB |
Output is correct |
11 |
Correct |
17 ms |
21504 KB |
Output is correct |
12 |
Correct |
17 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
17 ms |
21504 KB |
Output is correct |
15 |
Correct |
17 ms |
21504 KB |
Output is correct |
16 |
Correct |
17 ms |
21504 KB |
Output is correct |
17 |
Correct |
16 ms |
21504 KB |
Output is correct |
18 |
Correct |
16 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
16 ms |
21504 KB |
Output is correct |
21 |
Correct |
17 ms |
21504 KB |
Output is correct |
22 |
Correct |
16 ms |
21504 KB |
Output is correct |
23 |
Correct |
17 ms |
21524 KB |
Output is correct |
24 |
Correct |
17 ms |
21632 KB |
Output is correct |
25 |
Correct |
17 ms |
21504 KB |
Output is correct |
26 |
Correct |
17 ms |
21504 KB |
Output is correct |
27 |
Correct |
16 ms |
21504 KB |
Output is correct |
28 |
Correct |
16 ms |
21504 KB |
Output is correct |
29 |
Correct |
22 ms |
21504 KB |
Output is correct |
30 |
Correct |
17 ms |
21504 KB |
Output is correct |
31 |
Correct |
17 ms |
21632 KB |
Output is correct |
32 |
Correct |
19 ms |
21632 KB |
Output is correct |
33 |
Correct |
18 ms |
21632 KB |
Output is correct |
34 |
Correct |
18 ms |
21760 KB |
Output is correct |
35 |
Correct |
23 ms |
21760 KB |
Output is correct |
36 |
Correct |
19 ms |
21760 KB |
Output is correct |
37 |
Correct |
20 ms |
21760 KB |
Output is correct |
38 |
Correct |
20 ms |
21760 KB |
Output is correct |
39 |
Correct |
20 ms |
21888 KB |
Output is correct |
40 |
Correct |
19 ms |
22272 KB |
Output is correct |
41 |
Correct |
19 ms |
22272 KB |
Output is correct |
42 |
Correct |
18 ms |
21760 KB |
Output is correct |
43 |
Correct |
25 ms |
22092 KB |
Output is correct |
44 |
Correct |
19 ms |
22016 KB |
Output is correct |
45 |
Correct |
19 ms |
22016 KB |
Output is correct |
46 |
Correct |
19 ms |
21888 KB |
Output is correct |
47 |
Correct |
20 ms |
21888 KB |
Output is correct |
48 |
Correct |
19 ms |
21888 KB |
Output is correct |
49 |
Correct |
19 ms |
21888 KB |
Output is correct |
50 |
Correct |
19 ms |
21760 KB |
Output is correct |
51 |
Correct |
19 ms |
21760 KB |
Output is correct |
52 |
Correct |
19 ms |
21760 KB |
Output is correct |
53 |
Correct |
20 ms |
21888 KB |
Output is correct |
54 |
Correct |
20 ms |
21888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
17 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21480 KB |
Output is correct |
7 |
Correct |
17 ms |
21504 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
16 ms |
21504 KB |
Output is correct |
10 |
Correct |
16 ms |
21504 KB |
Output is correct |
11 |
Correct |
17 ms |
21504 KB |
Output is correct |
12 |
Correct |
17 ms |
21504 KB |
Output is correct |
13 |
Correct |
17 ms |
21504 KB |
Output is correct |
14 |
Correct |
17 ms |
21504 KB |
Output is correct |
15 |
Correct |
17 ms |
21504 KB |
Output is correct |
16 |
Correct |
17 ms |
21504 KB |
Output is correct |
17 |
Correct |
16 ms |
21504 KB |
Output is correct |
18 |
Correct |
16 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
16 ms |
21504 KB |
Output is correct |
21 |
Correct |
17 ms |
21504 KB |
Output is correct |
22 |
Correct |
16 ms |
21504 KB |
Output is correct |
23 |
Correct |
17 ms |
21524 KB |
Output is correct |
24 |
Correct |
17 ms |
21632 KB |
Output is correct |
25 |
Correct |
17 ms |
21504 KB |
Output is correct |
26 |
Correct |
17 ms |
21504 KB |
Output is correct |
27 |
Correct |
16 ms |
21504 KB |
Output is correct |
28 |
Correct |
16 ms |
21504 KB |
Output is correct |
29 |
Correct |
22 ms |
21504 KB |
Output is correct |
30 |
Correct |
17 ms |
21504 KB |
Output is correct |
31 |
Correct |
17 ms |
21632 KB |
Output is correct |
32 |
Correct |
19 ms |
21632 KB |
Output is correct |
33 |
Correct |
18 ms |
21632 KB |
Output is correct |
34 |
Correct |
18 ms |
21760 KB |
Output is correct |
35 |
Correct |
23 ms |
21760 KB |
Output is correct |
36 |
Correct |
19 ms |
21760 KB |
Output is correct |
37 |
Correct |
20 ms |
21760 KB |
Output is correct |
38 |
Correct |
20 ms |
21760 KB |
Output is correct |
39 |
Correct |
20 ms |
21888 KB |
Output is correct |
40 |
Correct |
19 ms |
22272 KB |
Output is correct |
41 |
Correct |
19 ms |
22272 KB |
Output is correct |
42 |
Correct |
18 ms |
21760 KB |
Output is correct |
43 |
Correct |
25 ms |
22092 KB |
Output is correct |
44 |
Correct |
19 ms |
22016 KB |
Output is correct |
45 |
Correct |
19 ms |
22016 KB |
Output is correct |
46 |
Correct |
19 ms |
21888 KB |
Output is correct |
47 |
Correct |
20 ms |
21888 KB |
Output is correct |
48 |
Correct |
19 ms |
21888 KB |
Output is correct |
49 |
Correct |
19 ms |
21888 KB |
Output is correct |
50 |
Correct |
19 ms |
21760 KB |
Output is correct |
51 |
Correct |
19 ms |
21760 KB |
Output is correct |
52 |
Correct |
19 ms |
21760 KB |
Output is correct |
53 |
Correct |
20 ms |
21888 KB |
Output is correct |
54 |
Correct |
20 ms |
21888 KB |
Output is correct |
55 |
Correct |
24 ms |
22400 KB |
Output is correct |
56 |
Correct |
52 ms |
24984 KB |
Output is correct |
57 |
Correct |
81 ms |
27384 KB |
Output is correct |
58 |
Correct |
106 ms |
29040 KB |
Output is correct |
59 |
Correct |
142 ms |
31680 KB |
Output is correct |
60 |
Correct |
183 ms |
34028 KB |
Output is correct |
61 |
Correct |
206 ms |
36208 KB |
Output is correct |
62 |
Correct |
225 ms |
37616 KB |
Output is correct |
63 |
Correct |
274 ms |
40556 KB |
Output is correct |
64 |
Correct |
286 ms |
41400 KB |
Output is correct |
65 |
Correct |
149 ms |
71032 KB |
Output is correct |
66 |
Correct |
147 ms |
70960 KB |
Output is correct |
67 |
Correct |
158 ms |
38136 KB |
Output is correct |
68 |
Correct |
198 ms |
55916 KB |
Output is correct |
69 |
Correct |
235 ms |
53140 KB |
Output is correct |
70 |
Correct |
223 ms |
53132 KB |
Output is correct |
71 |
Correct |
228 ms |
50660 KB |
Output is correct |
72 |
Correct |
226 ms |
51300 KB |
Output is correct |
73 |
Correct |
185 ms |
47200 KB |
Output is correct |
74 |
Correct |
189 ms |
47200 KB |
Output is correct |
75 |
Correct |
198 ms |
46332 KB |
Output is correct |
76 |
Correct |
192 ms |
46180 KB |
Output is correct |
77 |
Correct |
200 ms |
45800 KB |
Output is correct |
78 |
Correct |
203 ms |
43828 KB |
Output is correct |
79 |
Correct |
184 ms |
44900 KB |
Output is correct |