#include <bits/stdc++.h>
#pragma GCC optimize("03")
#pragma GCC target("avx2")
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define F first
#define S second
#define ld long double
#define pb push_back
#define all(v) v.begin(),v.end()
#define in insert
#define sz(s) (int)s.size()
#define int ll
#define ppb pop_back
#define mem(a,i) memset(a,i,sizeof(a))
using namespace std;
const int MAX=2e6+100;
const ll inf=2e18;
const int mod=1e9+7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,m;
vector<int> g[MAX];
int a[MAX];
multiset<int> st[MAX];
int slope[MAX];
void mrg(int v,int to){
slope[v]+=slope[to];
if(sz(st[v])<sz(st[to]))swap(st[v],st[to]);
for(int x:st[to])st[v].in(x);
st[to].clear();
}
void dfs(int v){
if(g[v].empty()){
slope[v]=-1;
st[v].in(a[v]);
st[v].in(a[v]);
return;
}
for(auto to:g[v]){
dfs(to);
mrg(v,to);
}
while(sz(st[v])+slope[v]>1){
st[v].erase(--st[v].end());
}
int A=*(--st[v].end());
st[v].erase(--st[v].end());
int B=*(--st[v].end());
st[v].erase(--st[v].end());
A+=a[v],B+=a[v];
st[v].in(A);st[v].in(B);
}
void solve(){
cin>>n>>m;
int ans=0;
for(int i=2;i<=n+m;i++){
int p;
cin>>p>>a[i];
g[p].pb(i);
ans+=a[i];
}
dfs(1);
int prev=0;
// cout<<ans<<"\n";
// slope[1]++;
for(int i:st[1]){
// cout<<i<<" ";
ans+=slope[1]*(i-prev);
prev=i;
// cout<<i-prev<<" "<<slope[1]<<"\n";
slope[1]++;
}
cout<<ans;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--)solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
141148 KB |
Output is correct |
2 |
Correct |
52 ms |
141140 KB |
Output is correct |
3 |
Correct |
55 ms |
141396 KB |
Output is correct |
4 |
Correct |
53 ms |
141144 KB |
Output is correct |
5 |
Correct |
65 ms |
141312 KB |
Output is correct |
6 |
Correct |
59 ms |
141136 KB |
Output is correct |
7 |
Correct |
57 ms |
141136 KB |
Output is correct |
8 |
Correct |
48 ms |
141236 KB |
Output is correct |
9 |
Correct |
47 ms |
141144 KB |
Output is correct |
10 |
Correct |
49 ms |
141144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
141144 KB |
Output is correct |
2 |
Correct |
55 ms |
141140 KB |
Output is correct |
3 |
Correct |
60 ms |
141228 KB |
Output is correct |
4 |
Correct |
51 ms |
141136 KB |
Output is correct |
5 |
Correct |
50 ms |
141148 KB |
Output is correct |
6 |
Correct |
52 ms |
141360 KB |
Output is correct |
7 |
Correct |
49 ms |
141148 KB |
Output is correct |
8 |
Correct |
50 ms |
141136 KB |
Output is correct |
9 |
Correct |
65 ms |
141172 KB |
Output is correct |
10 |
Correct |
48 ms |
141396 KB |
Output is correct |
11 |
Correct |
50 ms |
141140 KB |
Output is correct |
12 |
Correct |
50 ms |
141136 KB |
Output is correct |
13 |
Correct |
52 ms |
141140 KB |
Output is correct |
14 |
Correct |
48 ms |
141396 KB |
Output is correct |
15 |
Correct |
58 ms |
141140 KB |
Output is correct |
16 |
Correct |
49 ms |
141136 KB |
Output is correct |
17 |
Correct |
47 ms |
141216 KB |
Output is correct |
18 |
Correct |
58 ms |
141140 KB |
Output is correct |
19 |
Correct |
49 ms |
141136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
141148 KB |
Output is correct |
2 |
Correct |
52 ms |
141140 KB |
Output is correct |
3 |
Correct |
55 ms |
141396 KB |
Output is correct |
4 |
Correct |
53 ms |
141144 KB |
Output is correct |
5 |
Correct |
65 ms |
141312 KB |
Output is correct |
6 |
Correct |
59 ms |
141136 KB |
Output is correct |
7 |
Correct |
57 ms |
141136 KB |
Output is correct |
8 |
Correct |
48 ms |
141236 KB |
Output is correct |
9 |
Correct |
47 ms |
141144 KB |
Output is correct |
10 |
Correct |
49 ms |
141144 KB |
Output is correct |
11 |
Correct |
51 ms |
141144 KB |
Output is correct |
12 |
Correct |
55 ms |
141140 KB |
Output is correct |
13 |
Correct |
60 ms |
141228 KB |
Output is correct |
14 |
Correct |
51 ms |
141136 KB |
Output is correct |
15 |
Correct |
50 ms |
141148 KB |
Output is correct |
16 |
Correct |
52 ms |
141360 KB |
Output is correct |
17 |
Correct |
49 ms |
141148 KB |
Output is correct |
18 |
Correct |
50 ms |
141136 KB |
Output is correct |
19 |
Correct |
65 ms |
141172 KB |
Output is correct |
20 |
Correct |
48 ms |
141396 KB |
Output is correct |
21 |
Correct |
50 ms |
141140 KB |
Output is correct |
22 |
Correct |
50 ms |
141136 KB |
Output is correct |
23 |
Correct |
52 ms |
141140 KB |
Output is correct |
24 |
Correct |
48 ms |
141396 KB |
Output is correct |
25 |
Correct |
58 ms |
141140 KB |
Output is correct |
26 |
Correct |
49 ms |
141136 KB |
Output is correct |
27 |
Correct |
47 ms |
141216 KB |
Output is correct |
28 |
Correct |
58 ms |
141140 KB |
Output is correct |
29 |
Correct |
49 ms |
141136 KB |
Output is correct |
30 |
Correct |
52 ms |
141160 KB |
Output is correct |
31 |
Correct |
50 ms |
141284 KB |
Output is correct |
32 |
Correct |
51 ms |
141392 KB |
Output is correct |
33 |
Correct |
52 ms |
141392 KB |
Output is correct |
34 |
Correct |
49 ms |
141392 KB |
Output is correct |
35 |
Correct |
53 ms |
141396 KB |
Output is correct |
36 |
Correct |
53 ms |
141392 KB |
Output is correct |
37 |
Correct |
54 ms |
141464 KB |
Output is correct |
38 |
Correct |
50 ms |
141652 KB |
Output is correct |
39 |
Correct |
48 ms |
141416 KB |
Output is correct |
40 |
Correct |
48 ms |
141908 KB |
Output is correct |
41 |
Correct |
58 ms |
141836 KB |
Output is correct |
42 |
Correct |
47 ms |
141396 KB |
Output is correct |
43 |
Correct |
52 ms |
141700 KB |
Output is correct |
44 |
Correct |
57 ms |
141688 KB |
Output is correct |
45 |
Correct |
52 ms |
141652 KB |
Output is correct |
46 |
Correct |
51 ms |
141612 KB |
Output is correct |
47 |
Correct |
51 ms |
141584 KB |
Output is correct |
48 |
Correct |
53 ms |
141668 KB |
Output is correct |
49 |
Correct |
54 ms |
141648 KB |
Output is correct |
50 |
Correct |
51 ms |
141652 KB |
Output is correct |
51 |
Correct |
49 ms |
141648 KB |
Output is correct |
52 |
Correct |
53 ms |
141684 KB |
Output is correct |
53 |
Correct |
50 ms |
141692 KB |
Output is correct |
54 |
Correct |
53 ms |
141904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
141148 KB |
Output is correct |
2 |
Correct |
52 ms |
141140 KB |
Output is correct |
3 |
Correct |
55 ms |
141396 KB |
Output is correct |
4 |
Correct |
53 ms |
141144 KB |
Output is correct |
5 |
Correct |
65 ms |
141312 KB |
Output is correct |
6 |
Correct |
59 ms |
141136 KB |
Output is correct |
7 |
Correct |
57 ms |
141136 KB |
Output is correct |
8 |
Correct |
48 ms |
141236 KB |
Output is correct |
9 |
Correct |
47 ms |
141144 KB |
Output is correct |
10 |
Correct |
49 ms |
141144 KB |
Output is correct |
11 |
Correct |
51 ms |
141144 KB |
Output is correct |
12 |
Correct |
55 ms |
141140 KB |
Output is correct |
13 |
Correct |
60 ms |
141228 KB |
Output is correct |
14 |
Correct |
51 ms |
141136 KB |
Output is correct |
15 |
Correct |
50 ms |
141148 KB |
Output is correct |
16 |
Correct |
52 ms |
141360 KB |
Output is correct |
17 |
Correct |
49 ms |
141148 KB |
Output is correct |
18 |
Correct |
50 ms |
141136 KB |
Output is correct |
19 |
Correct |
65 ms |
141172 KB |
Output is correct |
20 |
Correct |
48 ms |
141396 KB |
Output is correct |
21 |
Correct |
50 ms |
141140 KB |
Output is correct |
22 |
Correct |
50 ms |
141136 KB |
Output is correct |
23 |
Correct |
52 ms |
141140 KB |
Output is correct |
24 |
Correct |
48 ms |
141396 KB |
Output is correct |
25 |
Correct |
58 ms |
141140 KB |
Output is correct |
26 |
Correct |
49 ms |
141136 KB |
Output is correct |
27 |
Correct |
47 ms |
141216 KB |
Output is correct |
28 |
Correct |
58 ms |
141140 KB |
Output is correct |
29 |
Correct |
49 ms |
141136 KB |
Output is correct |
30 |
Correct |
52 ms |
141160 KB |
Output is correct |
31 |
Correct |
50 ms |
141284 KB |
Output is correct |
32 |
Correct |
51 ms |
141392 KB |
Output is correct |
33 |
Correct |
52 ms |
141392 KB |
Output is correct |
34 |
Correct |
49 ms |
141392 KB |
Output is correct |
35 |
Correct |
53 ms |
141396 KB |
Output is correct |
36 |
Correct |
53 ms |
141392 KB |
Output is correct |
37 |
Correct |
54 ms |
141464 KB |
Output is correct |
38 |
Correct |
50 ms |
141652 KB |
Output is correct |
39 |
Correct |
48 ms |
141416 KB |
Output is correct |
40 |
Correct |
48 ms |
141908 KB |
Output is correct |
41 |
Correct |
58 ms |
141836 KB |
Output is correct |
42 |
Correct |
47 ms |
141396 KB |
Output is correct |
43 |
Correct |
52 ms |
141700 KB |
Output is correct |
44 |
Correct |
57 ms |
141688 KB |
Output is correct |
45 |
Correct |
52 ms |
141652 KB |
Output is correct |
46 |
Correct |
51 ms |
141612 KB |
Output is correct |
47 |
Correct |
51 ms |
141584 KB |
Output is correct |
48 |
Correct |
53 ms |
141668 KB |
Output is correct |
49 |
Correct |
54 ms |
141648 KB |
Output is correct |
50 |
Correct |
51 ms |
141652 KB |
Output is correct |
51 |
Correct |
49 ms |
141648 KB |
Output is correct |
52 |
Correct |
53 ms |
141684 KB |
Output is correct |
53 |
Correct |
50 ms |
141692 KB |
Output is correct |
54 |
Correct |
53 ms |
141904 KB |
Output is correct |
55 |
Correct |
66 ms |
142116 KB |
Output is correct |
56 |
Correct |
74 ms |
144584 KB |
Output is correct |
57 |
Correct |
87 ms |
146976 KB |
Output is correct |
58 |
Correct |
109 ms |
148440 KB |
Output is correct |
59 |
Correct |
122 ms |
150864 KB |
Output is correct |
60 |
Correct |
133 ms |
152920 KB |
Output is correct |
61 |
Correct |
168 ms |
154708 KB |
Output is correct |
62 |
Correct |
202 ms |
158284 KB |
Output is correct |
63 |
Correct |
214 ms |
161320 KB |
Output is correct |
64 |
Correct |
213 ms |
161620 KB |
Output is correct |
65 |
Correct |
135 ms |
183820 KB |
Output is correct |
66 |
Correct |
132 ms |
183584 KB |
Output is correct |
67 |
Correct |
156 ms |
160336 KB |
Output is correct |
68 |
Correct |
167 ms |
174160 KB |
Output is correct |
69 |
Correct |
207 ms |
172884 KB |
Output is correct |
70 |
Correct |
198 ms |
172880 KB |
Output is correct |
71 |
Correct |
240 ms |
167768 KB |
Output is correct |
72 |
Correct |
267 ms |
168020 KB |
Output is correct |
73 |
Correct |
254 ms |
167092 KB |
Output is correct |
74 |
Correct |
242 ms |
167248 KB |
Output is correct |
75 |
Correct |
234 ms |
166684 KB |
Output is correct |
76 |
Correct |
217 ms |
166812 KB |
Output is correct |
77 |
Correct |
272 ms |
166604 KB |
Output is correct |
78 |
Correct |
271 ms |
167512 KB |
Output is correct |
79 |
Correct |
285 ms |
179908 KB |
Output is correct |