#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
#define sz(x) int(x.size())
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
cin >> N >> M;
vi children[1+N];
vi P(1+N+M);
vll C(1+N+M);
for(int i = 2; i <= N+M; i++)
{
cin >> P[i] >> C[i];
children[P[i]].push_back(i);
}
C[1] = 0;
vector< multiset<ll>* > dp(1+N+M);
vi subtree(1+N+M, 0);
vi expcount(1+N+M, 0);
for(int i = N+1; i <= N+M; i++)
{
dp[i] = new multiset<ll>;
dp[i]->insert(C[i]);
dp[i]->insert(C[i]);
expcount[i] = 1;
subtree[i] = 1;
}
for(int u = N; u >= 1; u--)
{
subtree[u] = 1;
expcount[u] = 0;
int max_child = children[u][0];
for(int v: children[u])
{
subtree[u] += subtree[v];
expcount[u] += expcount[v];
if(subtree[v] > subtree[max_child]) v = max_child;
}
dp[u] = dp[max_child];
for(int v: children[u])
{
if(v == max_child) continue;
for(ll x: *dp[v]) dp[u]->insert(x);
}
while(int(dp[u]->size()) > expcount[u] + 1)
{
dp[u]->erase(dp[u]->find(*dp[u]->rbegin()));
}
// lp[u] += C[u];
ll x1 = *dp[u]->rbegin();
dp[u]->erase(dp[u]->find(x1));
ll x2 = *dp[u]->rbegin();
dp[u]->erase(dp[u]->find(x2));
dp[u]->insert(x1 + C[u]);
dp[u]->insert(x2 + C[u]);
}
dp[1]->erase(dp[1]->find(*dp[1]->rbegin()));
ll ans = 0;
for(int i = 2; i <= N+M; i++) ans += C[i];
ll currx = 0;
ll currslope = -expcount[1];
while(!dp[1]->empty())
{
ll newx = *dp[1]->begin();
dp[1]->erase(dp[1]->find(newx));
ans += (newx - currx) * currslope;
currslope++;
currx = newx;
}
// while(dp[1].begin() <)
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
0 ms |
324 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
0 ms |
324 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
2 ms |
588 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
2 ms |
732 KB |
Output is correct |
35 |
Correct |
2 ms |
844 KB |
Output is correct |
36 |
Correct |
2 ms |
972 KB |
Output is correct |
37 |
Correct |
3 ms |
1064 KB |
Output is correct |
38 |
Correct |
3 ms |
1100 KB |
Output is correct |
39 |
Correct |
3 ms |
1172 KB |
Output is correct |
40 |
Correct |
2 ms |
716 KB |
Output is correct |
41 |
Correct |
2 ms |
708 KB |
Output is correct |
42 |
Correct |
2 ms |
716 KB |
Output is correct |
43 |
Correct |
3 ms |
1100 KB |
Output is correct |
44 |
Correct |
3 ms |
1100 KB |
Output is correct |
45 |
Correct |
3 ms |
1100 KB |
Output is correct |
46 |
Correct |
4 ms |
1740 KB |
Output is correct |
47 |
Correct |
4 ms |
1728 KB |
Output is correct |
48 |
Correct |
4 ms |
1484 KB |
Output is correct |
49 |
Correct |
4 ms |
1736 KB |
Output is correct |
50 |
Correct |
3 ms |
1356 KB |
Output is correct |
51 |
Correct |
3 ms |
1348 KB |
Output is correct |
52 |
Correct |
4 ms |
1600 KB |
Output is correct |
53 |
Correct |
4 ms |
1736 KB |
Output is correct |
54 |
Correct |
3 ms |
1740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
312 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
0 ms |
324 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
2 ms |
588 KB |
Output is correct |
33 |
Correct |
2 ms |
588 KB |
Output is correct |
34 |
Correct |
2 ms |
732 KB |
Output is correct |
35 |
Correct |
2 ms |
844 KB |
Output is correct |
36 |
Correct |
2 ms |
972 KB |
Output is correct |
37 |
Correct |
3 ms |
1064 KB |
Output is correct |
38 |
Correct |
3 ms |
1100 KB |
Output is correct |
39 |
Correct |
3 ms |
1172 KB |
Output is correct |
40 |
Correct |
2 ms |
716 KB |
Output is correct |
41 |
Correct |
2 ms |
708 KB |
Output is correct |
42 |
Correct |
2 ms |
716 KB |
Output is correct |
43 |
Correct |
3 ms |
1100 KB |
Output is correct |
44 |
Correct |
3 ms |
1100 KB |
Output is correct |
45 |
Correct |
3 ms |
1100 KB |
Output is correct |
46 |
Correct |
4 ms |
1740 KB |
Output is correct |
47 |
Correct |
4 ms |
1728 KB |
Output is correct |
48 |
Correct |
4 ms |
1484 KB |
Output is correct |
49 |
Correct |
4 ms |
1736 KB |
Output is correct |
50 |
Correct |
3 ms |
1356 KB |
Output is correct |
51 |
Correct |
3 ms |
1348 KB |
Output is correct |
52 |
Correct |
4 ms |
1600 KB |
Output is correct |
53 |
Correct |
4 ms |
1736 KB |
Output is correct |
54 |
Correct |
3 ms |
1740 KB |
Output is correct |
55 |
Correct |
8 ms |
2760 KB |
Output is correct |
56 |
Correct |
34 ms |
9968 KB |
Output is correct |
57 |
Correct |
58 ms |
17256 KB |
Output is correct |
58 |
Correct |
79 ms |
22188 KB |
Output is correct |
59 |
Correct |
108 ms |
29532 KB |
Output is correct |
60 |
Correct |
138 ms |
37156 KB |
Output is correct |
61 |
Correct |
171 ms |
42300 KB |
Output is correct |
62 |
Correct |
185 ms |
46572 KB |
Output is correct |
63 |
Correct |
251 ms |
57284 KB |
Output is correct |
64 |
Correct |
260 ms |
58280 KB |
Output is correct |
65 |
Correct |
120 ms |
29776 KB |
Output is correct |
66 |
Correct |
123 ms |
29800 KB |
Output is correct |
67 |
Correct |
145 ms |
29924 KB |
Output is correct |
68 |
Correct |
250 ms |
52068 KB |
Output is correct |
69 |
Correct |
221 ms |
52452 KB |
Output is correct |
70 |
Correct |
220 ms |
52332 KB |
Output is correct |
71 |
Correct |
273 ms |
102412 KB |
Output is correct |
72 |
Correct |
260 ms |
102072 KB |
Output is correct |
73 |
Correct |
274 ms |
89084 KB |
Output is correct |
74 |
Correct |
262 ms |
89064 KB |
Output is correct |
75 |
Correct |
321 ms |
88268 KB |
Output is correct |
76 |
Correct |
285 ms |
88644 KB |
Output is correct |
77 |
Correct |
277 ms |
86696 KB |
Output is correct |
78 |
Correct |
308 ms |
92088 KB |
Output is correct |
79 |
Correct |
335 ms |
88260 KB |
Output is correct |