#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=2e5+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 |
5 ms |
14572 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
5 ms |
14580 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
5 ms |
14576 KB |
Output is correct |
10 |
Correct |
5 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14576 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
5 ms |
14568 KB |
Output is correct |
6 |
Correct |
5 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14428 KB |
Output is correct |
8 |
Correct |
5 ms |
14584 KB |
Output is correct |
9 |
Correct |
5 ms |
14428 KB |
Output is correct |
10 |
Correct |
5 ms |
14376 KB |
Output is correct |
11 |
Correct |
6 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14428 KB |
Output is correct |
13 |
Correct |
5 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14424 KB |
Output is correct |
15 |
Correct |
6 ms |
14428 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
5 ms |
14428 KB |
Output is correct |
18 |
Correct |
6 ms |
14428 KB |
Output is correct |
19 |
Correct |
8 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14572 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
5 ms |
14580 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
5 ms |
14576 KB |
Output is correct |
10 |
Correct |
5 ms |
14428 KB |
Output is correct |
11 |
Correct |
8 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14576 KB |
Output is correct |
14 |
Correct |
5 ms |
14428 KB |
Output is correct |
15 |
Correct |
5 ms |
14568 KB |
Output is correct |
16 |
Correct |
5 ms |
14428 KB |
Output is correct |
17 |
Correct |
5 ms |
14428 KB |
Output is correct |
18 |
Correct |
5 ms |
14584 KB |
Output is correct |
19 |
Correct |
5 ms |
14428 KB |
Output is correct |
20 |
Correct |
5 ms |
14376 KB |
Output is correct |
21 |
Correct |
6 ms |
14428 KB |
Output is correct |
22 |
Correct |
6 ms |
14428 KB |
Output is correct |
23 |
Correct |
5 ms |
14428 KB |
Output is correct |
24 |
Correct |
6 ms |
14424 KB |
Output is correct |
25 |
Correct |
6 ms |
14428 KB |
Output is correct |
26 |
Correct |
6 ms |
14428 KB |
Output is correct |
27 |
Correct |
5 ms |
14428 KB |
Output is correct |
28 |
Correct |
6 ms |
14428 KB |
Output is correct |
29 |
Correct |
8 ms |
14428 KB |
Output is correct |
30 |
Correct |
6 ms |
14684 KB |
Output is correct |
31 |
Correct |
6 ms |
14428 KB |
Output is correct |
32 |
Correct |
5 ms |
14684 KB |
Output is correct |
33 |
Correct |
6 ms |
14684 KB |
Output is correct |
34 |
Correct |
7 ms |
14636 KB |
Output is correct |
35 |
Correct |
8 ms |
14664 KB |
Output is correct |
36 |
Correct |
7 ms |
14844 KB |
Output is correct |
37 |
Correct |
8 ms |
14680 KB |
Output is correct |
38 |
Correct |
8 ms |
14684 KB |
Output is correct |
39 |
Correct |
8 ms |
14936 KB |
Output is correct |
40 |
Correct |
7 ms |
15196 KB |
Output is correct |
41 |
Correct |
7 ms |
15192 KB |
Output is correct |
42 |
Correct |
6 ms |
14684 KB |
Output is correct |
43 |
Correct |
7 ms |
14960 KB |
Output is correct |
44 |
Correct |
7 ms |
14940 KB |
Output is correct |
45 |
Correct |
8 ms |
14848 KB |
Output is correct |
46 |
Correct |
8 ms |
14940 KB |
Output is correct |
47 |
Correct |
8 ms |
15192 KB |
Output is correct |
48 |
Correct |
8 ms |
14936 KB |
Output is correct |
49 |
Correct |
7 ms |
14940 KB |
Output is correct |
50 |
Correct |
7 ms |
14816 KB |
Output is correct |
51 |
Correct |
9 ms |
14940 KB |
Output is correct |
52 |
Correct |
7 ms |
14940 KB |
Output is correct |
53 |
Correct |
8 ms |
14940 KB |
Output is correct |
54 |
Correct |
7 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14572 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
7 ms |
14424 KB |
Output is correct |
6 |
Correct |
5 ms |
14580 KB |
Output is correct |
7 |
Correct |
6 ms |
14424 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
5 ms |
14576 KB |
Output is correct |
10 |
Correct |
5 ms |
14428 KB |
Output is correct |
11 |
Correct |
8 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14576 KB |
Output is correct |
14 |
Correct |
5 ms |
14428 KB |
Output is correct |
15 |
Correct |
5 ms |
14568 KB |
Output is correct |
16 |
Correct |
5 ms |
14428 KB |
Output is correct |
17 |
Correct |
5 ms |
14428 KB |
Output is correct |
18 |
Correct |
5 ms |
14584 KB |
Output is correct |
19 |
Correct |
5 ms |
14428 KB |
Output is correct |
20 |
Correct |
5 ms |
14376 KB |
Output is correct |
21 |
Correct |
6 ms |
14428 KB |
Output is correct |
22 |
Correct |
6 ms |
14428 KB |
Output is correct |
23 |
Correct |
5 ms |
14428 KB |
Output is correct |
24 |
Correct |
6 ms |
14424 KB |
Output is correct |
25 |
Correct |
6 ms |
14428 KB |
Output is correct |
26 |
Correct |
6 ms |
14428 KB |
Output is correct |
27 |
Correct |
5 ms |
14428 KB |
Output is correct |
28 |
Correct |
6 ms |
14428 KB |
Output is correct |
29 |
Correct |
8 ms |
14428 KB |
Output is correct |
30 |
Correct |
6 ms |
14684 KB |
Output is correct |
31 |
Correct |
6 ms |
14428 KB |
Output is correct |
32 |
Correct |
5 ms |
14684 KB |
Output is correct |
33 |
Correct |
6 ms |
14684 KB |
Output is correct |
34 |
Correct |
7 ms |
14636 KB |
Output is correct |
35 |
Correct |
8 ms |
14664 KB |
Output is correct |
36 |
Correct |
7 ms |
14844 KB |
Output is correct |
37 |
Correct |
8 ms |
14680 KB |
Output is correct |
38 |
Correct |
8 ms |
14684 KB |
Output is correct |
39 |
Correct |
8 ms |
14936 KB |
Output is correct |
40 |
Correct |
7 ms |
15196 KB |
Output is correct |
41 |
Correct |
7 ms |
15192 KB |
Output is correct |
42 |
Correct |
6 ms |
14684 KB |
Output is correct |
43 |
Correct |
7 ms |
14960 KB |
Output is correct |
44 |
Correct |
7 ms |
14940 KB |
Output is correct |
45 |
Correct |
8 ms |
14848 KB |
Output is correct |
46 |
Correct |
8 ms |
14940 KB |
Output is correct |
47 |
Correct |
8 ms |
15192 KB |
Output is correct |
48 |
Correct |
8 ms |
14936 KB |
Output is correct |
49 |
Correct |
7 ms |
14940 KB |
Output is correct |
50 |
Correct |
7 ms |
14816 KB |
Output is correct |
51 |
Correct |
9 ms |
14940 KB |
Output is correct |
52 |
Correct |
7 ms |
14940 KB |
Output is correct |
53 |
Correct |
8 ms |
14940 KB |
Output is correct |
54 |
Correct |
7 ms |
15196 KB |
Output is correct |
55 |
Correct |
11 ms |
15704 KB |
Output is correct |
56 |
Correct |
27 ms |
18032 KB |
Output is correct |
57 |
Correct |
43 ms |
20316 KB |
Output is correct |
58 |
Correct |
56 ms |
22352 KB |
Output is correct |
59 |
Correct |
106 ms |
24912 KB |
Output is correct |
60 |
Correct |
98 ms |
27316 KB |
Output is correct |
61 |
Runtime error |
50 ms |
44116 KB |
Execution killed with signal 11 |
62 |
Halted |
0 ms |
0 KB |
- |