#include <bits/stdc++.h>
using namespace std;
using i64=long long;
#define UNUSED -1
#define all(x) x.begin(),x.end()
#define pb push_back
const int mod=1e9+7,inf=1e9+1;
const int N=3e5;
vector<pair<int,int>>g[N];
multiset<i64>mp[N];
i64 rez[N];
void dfs(int nod)
{
if(g[nod].size()==0)
{
mp[nod].insert(0);
mp[nod].insert(0);
return;
}
for(auto &c:g[nod])
{
dfs(c.first);
i64 v1=*mp[c.first].rbegin();
mp[c.first].erase(mp[c.first].find(v1));
i64 v2=*mp[c.first].rbegin();
mp[c.first].erase(mp[c.first].find(v2));
mp[c.first].insert(v1+c.second);
mp[c.first].insert(v2+c.second);
if(mp[nod].size() < mp[c.first].size())
{
swap(mp[nod] , mp[c.first]);
}
for(auto &v:mp[c.first])
{
mp[nod].insert(v);
}
}
for(int i=1;i<(int)g[nod].size();i++)
{
mp[nod].erase(mp[nod].find(*mp[nod].rbegin()));
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
n+=m;
i64 rez=0;
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
rez+=y;
x--;
g[x].pb({i,y});
}
dfs(0);
i64 last=0;
for(int i=m;i>0;i--)
{
i64 it=*mp[0].begin();
mp[0].erase(mp[0].find(it));
rez+=(last-it)*i;
last=it;
}
cout<<rez;
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
21596 KB |
Output is correct |
2 |
Correct |
8 ms |
21340 KB |
Output is correct |
3 |
Correct |
9 ms |
21596 KB |
Output is correct |
4 |
Correct |
10 ms |
21596 KB |
Output is correct |
5 |
Correct |
8 ms |
21456 KB |
Output is correct |
6 |
Correct |
9 ms |
21596 KB |
Output is correct |
7 |
Correct |
9 ms |
21596 KB |
Output is correct |
8 |
Correct |
9 ms |
21504 KB |
Output is correct |
9 |
Correct |
9 ms |
21596 KB |
Output is correct |
10 |
Correct |
9 ms |
21596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
21596 KB |
Output is correct |
2 |
Correct |
8 ms |
21340 KB |
Output is correct |
3 |
Correct |
8 ms |
21432 KB |
Output is correct |
4 |
Correct |
9 ms |
21596 KB |
Output is correct |
5 |
Correct |
9 ms |
21596 KB |
Output is correct |
6 |
Correct |
9 ms |
21596 KB |
Output is correct |
7 |
Correct |
9 ms |
21592 KB |
Output is correct |
8 |
Correct |
9 ms |
21596 KB |
Output is correct |
9 |
Correct |
9 ms |
21596 KB |
Output is correct |
10 |
Correct |
9 ms |
21596 KB |
Output is correct |
11 |
Correct |
11 ms |
21596 KB |
Output is correct |
12 |
Correct |
11 ms |
21628 KB |
Output is correct |
13 |
Correct |
10 ms |
21596 KB |
Output is correct |
14 |
Correct |
10 ms |
21596 KB |
Output is correct |
15 |
Correct |
9 ms |
21596 KB |
Output is correct |
16 |
Correct |
11 ms |
21596 KB |
Output is correct |
17 |
Correct |
10 ms |
21596 KB |
Output is correct |
18 |
Correct |
9 ms |
21424 KB |
Output is correct |
19 |
Correct |
9 ms |
21596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
21596 KB |
Output is correct |
2 |
Correct |
8 ms |
21340 KB |
Output is correct |
3 |
Correct |
9 ms |
21596 KB |
Output is correct |
4 |
Correct |
10 ms |
21596 KB |
Output is correct |
5 |
Correct |
8 ms |
21456 KB |
Output is correct |
6 |
Correct |
9 ms |
21596 KB |
Output is correct |
7 |
Correct |
9 ms |
21596 KB |
Output is correct |
8 |
Correct |
9 ms |
21504 KB |
Output is correct |
9 |
Correct |
9 ms |
21596 KB |
Output is correct |
10 |
Correct |
9 ms |
21596 KB |
Output is correct |
11 |
Correct |
9 ms |
21596 KB |
Output is correct |
12 |
Correct |
8 ms |
21340 KB |
Output is correct |
13 |
Correct |
8 ms |
21432 KB |
Output is correct |
14 |
Correct |
9 ms |
21596 KB |
Output is correct |
15 |
Correct |
9 ms |
21596 KB |
Output is correct |
16 |
Correct |
9 ms |
21596 KB |
Output is correct |
17 |
Correct |
9 ms |
21592 KB |
Output is correct |
18 |
Correct |
9 ms |
21596 KB |
Output is correct |
19 |
Correct |
9 ms |
21596 KB |
Output is correct |
20 |
Correct |
9 ms |
21596 KB |
Output is correct |
21 |
Correct |
11 ms |
21596 KB |
Output is correct |
22 |
Correct |
11 ms |
21628 KB |
Output is correct |
23 |
Correct |
10 ms |
21596 KB |
Output is correct |
24 |
Correct |
10 ms |
21596 KB |
Output is correct |
25 |
Correct |
9 ms |
21596 KB |
Output is correct |
26 |
Correct |
11 ms |
21596 KB |
Output is correct |
27 |
Correct |
10 ms |
21596 KB |
Output is correct |
28 |
Correct |
9 ms |
21424 KB |
Output is correct |
29 |
Correct |
9 ms |
21596 KB |
Output is correct |
30 |
Correct |
9 ms |
21592 KB |
Output is correct |
31 |
Correct |
10 ms |
21592 KB |
Output is correct |
32 |
Correct |
10 ms |
21592 KB |
Output is correct |
33 |
Correct |
11 ms |
21852 KB |
Output is correct |
34 |
Correct |
15 ms |
21852 KB |
Output is correct |
35 |
Correct |
11 ms |
21964 KB |
Output is correct |
36 |
Correct |
12 ms |
21852 KB |
Output is correct |
37 |
Correct |
11 ms |
21956 KB |
Output is correct |
38 |
Correct |
11 ms |
22052 KB |
Output is correct |
39 |
Correct |
11 ms |
22108 KB |
Output is correct |
40 |
Correct |
11 ms |
22356 KB |
Output is correct |
41 |
Correct |
11 ms |
22528 KB |
Output is correct |
42 |
Correct |
10 ms |
21824 KB |
Output is correct |
43 |
Correct |
12 ms |
22364 KB |
Output is correct |
44 |
Correct |
11 ms |
22364 KB |
Output is correct |
45 |
Correct |
14 ms |
22364 KB |
Output is correct |
46 |
Correct |
12 ms |
22528 KB |
Output is correct |
47 |
Correct |
12 ms |
22620 KB |
Output is correct |
48 |
Correct |
11 ms |
22396 KB |
Output is correct |
49 |
Correct |
13 ms |
22364 KB |
Output is correct |
50 |
Correct |
11 ms |
22364 KB |
Output is correct |
51 |
Correct |
11 ms |
22360 KB |
Output is correct |
52 |
Correct |
11 ms |
22364 KB |
Output is correct |
53 |
Correct |
11 ms |
22364 KB |
Output is correct |
54 |
Correct |
11 ms |
22616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
21596 KB |
Output is correct |
2 |
Correct |
8 ms |
21340 KB |
Output is correct |
3 |
Correct |
9 ms |
21596 KB |
Output is correct |
4 |
Correct |
10 ms |
21596 KB |
Output is correct |
5 |
Correct |
8 ms |
21456 KB |
Output is correct |
6 |
Correct |
9 ms |
21596 KB |
Output is correct |
7 |
Correct |
9 ms |
21596 KB |
Output is correct |
8 |
Correct |
9 ms |
21504 KB |
Output is correct |
9 |
Correct |
9 ms |
21596 KB |
Output is correct |
10 |
Correct |
9 ms |
21596 KB |
Output is correct |
11 |
Correct |
9 ms |
21596 KB |
Output is correct |
12 |
Correct |
8 ms |
21340 KB |
Output is correct |
13 |
Correct |
8 ms |
21432 KB |
Output is correct |
14 |
Correct |
9 ms |
21596 KB |
Output is correct |
15 |
Correct |
9 ms |
21596 KB |
Output is correct |
16 |
Correct |
9 ms |
21596 KB |
Output is correct |
17 |
Correct |
9 ms |
21592 KB |
Output is correct |
18 |
Correct |
9 ms |
21596 KB |
Output is correct |
19 |
Correct |
9 ms |
21596 KB |
Output is correct |
20 |
Correct |
9 ms |
21596 KB |
Output is correct |
21 |
Correct |
11 ms |
21596 KB |
Output is correct |
22 |
Correct |
11 ms |
21628 KB |
Output is correct |
23 |
Correct |
10 ms |
21596 KB |
Output is correct |
24 |
Correct |
10 ms |
21596 KB |
Output is correct |
25 |
Correct |
9 ms |
21596 KB |
Output is correct |
26 |
Correct |
11 ms |
21596 KB |
Output is correct |
27 |
Correct |
10 ms |
21596 KB |
Output is correct |
28 |
Correct |
9 ms |
21424 KB |
Output is correct |
29 |
Correct |
9 ms |
21596 KB |
Output is correct |
30 |
Correct |
9 ms |
21592 KB |
Output is correct |
31 |
Correct |
10 ms |
21592 KB |
Output is correct |
32 |
Correct |
10 ms |
21592 KB |
Output is correct |
33 |
Correct |
11 ms |
21852 KB |
Output is correct |
34 |
Correct |
15 ms |
21852 KB |
Output is correct |
35 |
Correct |
11 ms |
21964 KB |
Output is correct |
36 |
Correct |
12 ms |
21852 KB |
Output is correct |
37 |
Correct |
11 ms |
21956 KB |
Output is correct |
38 |
Correct |
11 ms |
22052 KB |
Output is correct |
39 |
Correct |
11 ms |
22108 KB |
Output is correct |
40 |
Correct |
11 ms |
22356 KB |
Output is correct |
41 |
Correct |
11 ms |
22528 KB |
Output is correct |
42 |
Correct |
10 ms |
21824 KB |
Output is correct |
43 |
Correct |
12 ms |
22364 KB |
Output is correct |
44 |
Correct |
11 ms |
22364 KB |
Output is correct |
45 |
Correct |
14 ms |
22364 KB |
Output is correct |
46 |
Correct |
12 ms |
22528 KB |
Output is correct |
47 |
Correct |
12 ms |
22620 KB |
Output is correct |
48 |
Correct |
11 ms |
22396 KB |
Output is correct |
49 |
Correct |
13 ms |
22364 KB |
Output is correct |
50 |
Correct |
11 ms |
22364 KB |
Output is correct |
51 |
Correct |
11 ms |
22360 KB |
Output is correct |
52 |
Correct |
11 ms |
22364 KB |
Output is correct |
53 |
Correct |
11 ms |
22364 KB |
Output is correct |
54 |
Correct |
11 ms |
22616 KB |
Output is correct |
55 |
Correct |
16 ms |
23132 KB |
Output is correct |
56 |
Correct |
36 ms |
27884 KB |
Output is correct |
57 |
Correct |
54 ms |
32852 KB |
Output is correct |
58 |
Correct |
75 ms |
36436 KB |
Output is correct |
59 |
Correct |
98 ms |
41212 KB |
Output is correct |
60 |
Correct |
124 ms |
46420 KB |
Output is correct |
61 |
Correct |
147 ms |
49880 KB |
Output is correct |
62 |
Correct |
170 ms |
53328 KB |
Output is correct |
63 |
Correct |
204 ms |
60244 KB |
Output is correct |
64 |
Correct |
221 ms |
60752 KB |
Output is correct |
65 |
Correct |
105 ms |
77904 KB |
Output is correct |
66 |
Correct |
104 ms |
78020 KB |
Output is correct |
67 |
Correct |
91 ms |
35924 KB |
Output is correct |
68 |
Correct |
159 ms |
73040 KB |
Output is correct |
69 |
Correct |
191 ms |
70752 KB |
Output is correct |
70 |
Correct |
179 ms |
70612 KB |
Output is correct |
71 |
Correct |
315 ms |
98136 KB |
Output is correct |
72 |
Correct |
282 ms |
97620 KB |
Output is correct |
73 |
Correct |
264 ms |
84860 KB |
Output is correct |
74 |
Correct |
279 ms |
84816 KB |
Output is correct |
75 |
Correct |
264 ms |
83792 KB |
Output is correct |
76 |
Correct |
271 ms |
84304 KB |
Output is correct |
77 |
Correct |
275 ms |
82124 KB |
Output is correct |
78 |
Correct |
292 ms |
79176 KB |
Output is correct |
79 |
Correct |
293 ms |
83648 KB |
Output is correct |