#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define semicolon ;
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18+7;
vector<pl>adj[300010];
priority_queue<ll,vector<ll>,less<ll> >pq1[300010];
priority_queue<ll,vector<ll>,greater<ll> >pq2[300010];
int pct;
int pn[300010];
ll b1[300010],b2[300010];
ll ans;
inline void in1(int n,ll x)
{
if(x>pq2[n].top()+b2[n])
{
ans+=x-(pq2[n].top()+b2[n]);
pq2[n].ep(x-b2[n]);
pq1[n].ep(pq2[n].top()+b2[n]-b1[n]);
pq2[n].pop();
}
else
pq1[n].ep(x-b1[n]);
return;
}
inline void in2(int n,ll x)
{
if(x<pq1[n].top()+b1[n])
{
ans+=(pq1[n].top()+b1[n])-x;
pq1[n].ep(x-b1[n]);
pq2[n].ep(pq1[n].top()+b1[n]-b2[n]);
pq1[n].pop();
}
else
pq2[n].ep(x-b2[n]);
return;
}
void dfs(int x)
{
if(adj[x].empty())
{
pn[x]=pct++;
pq1[pn[x]].ep(0);
pq2[pn[x]].ep(0);
return;
}
int sz=-1;
for(pl&t:adj[x])
{
dfs(t.fi);
b2[pn[t.fi]]+=t.se;
ll l=pq1[pn[t.fi]].top();
pq1[pn[t.fi]].pop();
pq1[pn[t.fi]].ep(l+t.se);
l=pq2[pn[t.fi]].top();
while(!pq2[pn[t.fi]].empty())
pq2[pn[t.fi]].pop();
pq2[pn[t.fi]].ep(l);
if(sz<(int)pq1[pn[t.fi]].size())
sz=pq1[pn[x]=pn[t.fi]].size();
}
for(pl&t:adj[x])
{
if(pn[x]==pn[t.fi])
continue;
while(!pq1[pn[t.fi]].empty())
in1(pn[x],pq1[pn[t.fi]].top()+b1[pn[t.fi]]),pq1[pn[t.fi]].pop();
while(!pq2[pn[t.fi]].empty())
in2(pn[x],pq2[pn[t.fi]].top()+b2[pn[t.fi]]),pq2[pn[t.fi]].pop();
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int v,c,p;
cin>>v>>c;
v+=c;
for(int i=1;i++<v;)
cin>>p>>c,adj[p].eb(i,c);
dfs(1);
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
26104 KB |
Output is correct |
2 |
Correct |
20 ms |
26104 KB |
Output is correct |
3 |
Correct |
21 ms |
26360 KB |
Output is correct |
4 |
Correct |
20 ms |
26232 KB |
Output is correct |
5 |
Correct |
20 ms |
26104 KB |
Output is correct |
6 |
Correct |
20 ms |
26232 KB |
Output is correct |
7 |
Correct |
20 ms |
26232 KB |
Output is correct |
8 |
Correct |
21 ms |
26236 KB |
Output is correct |
9 |
Correct |
22 ms |
26232 KB |
Output is correct |
10 |
Correct |
21 ms |
26232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
26360 KB |
Output is correct |
2 |
Correct |
23 ms |
26108 KB |
Output is correct |
3 |
Correct |
25 ms |
26104 KB |
Output is correct |
4 |
Correct |
20 ms |
26232 KB |
Output is correct |
5 |
Correct |
23 ms |
26232 KB |
Output is correct |
6 |
Correct |
20 ms |
26232 KB |
Output is correct |
7 |
Correct |
20 ms |
26232 KB |
Output is correct |
8 |
Correct |
21 ms |
26232 KB |
Output is correct |
9 |
Correct |
21 ms |
26232 KB |
Output is correct |
10 |
Correct |
21 ms |
26232 KB |
Output is correct |
11 |
Correct |
20 ms |
26232 KB |
Output is correct |
12 |
Correct |
20 ms |
26232 KB |
Output is correct |
13 |
Correct |
21 ms |
26232 KB |
Output is correct |
14 |
Correct |
20 ms |
26232 KB |
Output is correct |
15 |
Correct |
20 ms |
26232 KB |
Output is correct |
16 |
Correct |
21 ms |
26228 KB |
Output is correct |
17 |
Correct |
21 ms |
26232 KB |
Output is correct |
18 |
Correct |
20 ms |
26232 KB |
Output is correct |
19 |
Correct |
20 ms |
26232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
26104 KB |
Output is correct |
2 |
Correct |
20 ms |
26104 KB |
Output is correct |
3 |
Correct |
21 ms |
26360 KB |
Output is correct |
4 |
Correct |
20 ms |
26232 KB |
Output is correct |
5 |
Correct |
20 ms |
26104 KB |
Output is correct |
6 |
Correct |
20 ms |
26232 KB |
Output is correct |
7 |
Correct |
20 ms |
26232 KB |
Output is correct |
8 |
Correct |
21 ms |
26236 KB |
Output is correct |
9 |
Correct |
22 ms |
26232 KB |
Output is correct |
10 |
Correct |
21 ms |
26232 KB |
Output is correct |
11 |
Correct |
27 ms |
26360 KB |
Output is correct |
12 |
Correct |
23 ms |
26108 KB |
Output is correct |
13 |
Correct |
25 ms |
26104 KB |
Output is correct |
14 |
Correct |
20 ms |
26232 KB |
Output is correct |
15 |
Correct |
23 ms |
26232 KB |
Output is correct |
16 |
Correct |
20 ms |
26232 KB |
Output is correct |
17 |
Correct |
20 ms |
26232 KB |
Output is correct |
18 |
Correct |
21 ms |
26232 KB |
Output is correct |
19 |
Correct |
21 ms |
26232 KB |
Output is correct |
20 |
Correct |
21 ms |
26232 KB |
Output is correct |
21 |
Correct |
20 ms |
26232 KB |
Output is correct |
22 |
Correct |
20 ms |
26232 KB |
Output is correct |
23 |
Correct |
21 ms |
26232 KB |
Output is correct |
24 |
Correct |
20 ms |
26232 KB |
Output is correct |
25 |
Correct |
20 ms |
26232 KB |
Output is correct |
26 |
Correct |
21 ms |
26228 KB |
Output is correct |
27 |
Correct |
21 ms |
26232 KB |
Output is correct |
28 |
Correct |
20 ms |
26232 KB |
Output is correct |
29 |
Correct |
20 ms |
26232 KB |
Output is correct |
30 |
Correct |
20 ms |
26236 KB |
Output is correct |
31 |
Correct |
23 ms |
26280 KB |
Output is correct |
32 |
Correct |
21 ms |
26360 KB |
Output is correct |
33 |
Correct |
24 ms |
26416 KB |
Output is correct |
34 |
Correct |
23 ms |
26488 KB |
Output is correct |
35 |
Correct |
23 ms |
26492 KB |
Output is correct |
36 |
Correct |
23 ms |
26488 KB |
Output is correct |
37 |
Correct |
24 ms |
26616 KB |
Output is correct |
38 |
Correct |
24 ms |
26616 KB |
Output is correct |
39 |
Correct |
23 ms |
26616 KB |
Output is correct |
40 |
Correct |
22 ms |
27128 KB |
Output is correct |
41 |
Correct |
22 ms |
27128 KB |
Output is correct |
42 |
Correct |
23 ms |
26488 KB |
Output is correct |
43 |
Correct |
23 ms |
26872 KB |
Output is correct |
44 |
Correct |
24 ms |
26872 KB |
Output is correct |
45 |
Correct |
23 ms |
26872 KB |
Output is correct |
46 |
Correct |
25 ms |
26876 KB |
Output is correct |
47 |
Correct |
24 ms |
26872 KB |
Output is correct |
48 |
Correct |
25 ms |
26744 KB |
Output is correct |
49 |
Correct |
24 ms |
26872 KB |
Output is correct |
50 |
Correct |
24 ms |
26744 KB |
Output is correct |
51 |
Correct |
23 ms |
26744 KB |
Output is correct |
52 |
Correct |
26 ms |
26744 KB |
Output is correct |
53 |
Correct |
24 ms |
26872 KB |
Output is correct |
54 |
Correct |
24 ms |
26872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
26104 KB |
Output is correct |
2 |
Correct |
20 ms |
26104 KB |
Output is correct |
3 |
Correct |
21 ms |
26360 KB |
Output is correct |
4 |
Correct |
20 ms |
26232 KB |
Output is correct |
5 |
Correct |
20 ms |
26104 KB |
Output is correct |
6 |
Correct |
20 ms |
26232 KB |
Output is correct |
7 |
Correct |
20 ms |
26232 KB |
Output is correct |
8 |
Correct |
21 ms |
26236 KB |
Output is correct |
9 |
Correct |
22 ms |
26232 KB |
Output is correct |
10 |
Correct |
21 ms |
26232 KB |
Output is correct |
11 |
Correct |
27 ms |
26360 KB |
Output is correct |
12 |
Correct |
23 ms |
26108 KB |
Output is correct |
13 |
Correct |
25 ms |
26104 KB |
Output is correct |
14 |
Correct |
20 ms |
26232 KB |
Output is correct |
15 |
Correct |
23 ms |
26232 KB |
Output is correct |
16 |
Correct |
20 ms |
26232 KB |
Output is correct |
17 |
Correct |
20 ms |
26232 KB |
Output is correct |
18 |
Correct |
21 ms |
26232 KB |
Output is correct |
19 |
Correct |
21 ms |
26232 KB |
Output is correct |
20 |
Correct |
21 ms |
26232 KB |
Output is correct |
21 |
Correct |
20 ms |
26232 KB |
Output is correct |
22 |
Correct |
20 ms |
26232 KB |
Output is correct |
23 |
Correct |
21 ms |
26232 KB |
Output is correct |
24 |
Correct |
20 ms |
26232 KB |
Output is correct |
25 |
Correct |
20 ms |
26232 KB |
Output is correct |
26 |
Correct |
21 ms |
26228 KB |
Output is correct |
27 |
Correct |
21 ms |
26232 KB |
Output is correct |
28 |
Correct |
20 ms |
26232 KB |
Output is correct |
29 |
Correct |
20 ms |
26232 KB |
Output is correct |
30 |
Correct |
20 ms |
26236 KB |
Output is correct |
31 |
Correct |
23 ms |
26280 KB |
Output is correct |
32 |
Correct |
21 ms |
26360 KB |
Output is correct |
33 |
Correct |
24 ms |
26416 KB |
Output is correct |
34 |
Correct |
23 ms |
26488 KB |
Output is correct |
35 |
Correct |
23 ms |
26492 KB |
Output is correct |
36 |
Correct |
23 ms |
26488 KB |
Output is correct |
37 |
Correct |
24 ms |
26616 KB |
Output is correct |
38 |
Correct |
24 ms |
26616 KB |
Output is correct |
39 |
Correct |
23 ms |
26616 KB |
Output is correct |
40 |
Correct |
22 ms |
27128 KB |
Output is correct |
41 |
Correct |
22 ms |
27128 KB |
Output is correct |
42 |
Correct |
23 ms |
26488 KB |
Output is correct |
43 |
Correct |
23 ms |
26872 KB |
Output is correct |
44 |
Correct |
24 ms |
26872 KB |
Output is correct |
45 |
Correct |
23 ms |
26872 KB |
Output is correct |
46 |
Correct |
25 ms |
26876 KB |
Output is correct |
47 |
Correct |
24 ms |
26872 KB |
Output is correct |
48 |
Correct |
25 ms |
26744 KB |
Output is correct |
49 |
Correct |
24 ms |
26872 KB |
Output is correct |
50 |
Correct |
24 ms |
26744 KB |
Output is correct |
51 |
Correct |
23 ms |
26744 KB |
Output is correct |
52 |
Correct |
26 ms |
26744 KB |
Output is correct |
53 |
Correct |
24 ms |
26872 KB |
Output is correct |
54 |
Correct |
24 ms |
26872 KB |
Output is correct |
55 |
Correct |
28 ms |
27384 KB |
Output is correct |
56 |
Correct |
59 ms |
30456 KB |
Output is correct |
57 |
Correct |
90 ms |
33528 KB |
Output is correct |
58 |
Correct |
119 ms |
35568 KB |
Output is correct |
59 |
Correct |
160 ms |
38820 KB |
Output is correct |
60 |
Correct |
198 ms |
41836 KB |
Output is correct |
61 |
Correct |
217 ms |
44144 KB |
Output is correct |
62 |
Correct |
244 ms |
45808 KB |
Output is correct |
63 |
Correct |
294 ms |
49772 KB |
Output is correct |
64 |
Correct |
326 ms |
50948 KB |
Output is correct |
65 |
Correct |
169 ms |
83832 KB |
Output is correct |
66 |
Correct |
164 ms |
83832 KB |
Output is correct |
67 |
Correct |
148 ms |
41848 KB |
Output is correct |
68 |
Correct |
212 ms |
72292 KB |
Output is correct |
69 |
Correct |
249 ms |
69092 KB |
Output is correct |
70 |
Correct |
245 ms |
69220 KB |
Output is correct |
71 |
Correct |
292 ms |
72436 KB |
Output is correct |
72 |
Correct |
278 ms |
72552 KB |
Output is correct |
73 |
Correct |
253 ms |
68860 KB |
Output is correct |
74 |
Correct |
277 ms |
69092 KB |
Output is correct |
75 |
Correct |
258 ms |
67880 KB |
Output is correct |
76 |
Correct |
270 ms |
67816 KB |
Output is correct |
77 |
Correct |
265 ms |
66320 KB |
Output is correct |
78 |
Correct |
272 ms |
63588 KB |
Output is correct |
79 |
Correct |
212 ms |
67028 KB |
Output is correct |