//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,m;
vector<int> al[300005];
ll d[300005];
ll w[300005];
priority_queue<ll> pq[300005];
void dfs(int i){
if (i>n){
pq[i].push(0);
pq[i].push(0);
}
else{
vector<ll> tops;
int grad=-1;
for (auto &it:al[i]){
dfs(it);
d[i]+=d[it];
grad++;
tops.push_back(pq[it].top());
if (pq[i].empty()) swap(pq[i],pq[it]);
else{
if (sz(pq[i])<sz(pq[it])) swap(pq[i],pq[it]);
while (!pq[it].empty()){
pq[i].push(pq[it].top());
pq[it].pop();
}
}
}
for (auto &it:tops) d[i]+=pq[i].top()-it;
while (grad){
ll temp=pq[i].top();
pq[i].pop();
d[i]-=grad*(temp-pq[i].top());
grad--;
}
}
ll t1,t2;
t1=pq[i].top(),pq[i].pop();
t2=pq[i].top(),pq[i].pop();
pq[i].push(t1+w[i]);
pq[i].push(t2+w[i]);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
int a,b;
rep(x,2,n+m+1){
cin>>a>>b;
al[a].push_back(x);
w[x]=b;
}
dfs(1);
cout<<d[1]<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
10 ms |
16768 KB |
Output is correct |
7 |
Correct |
13 ms |
16768 KB |
Output is correct |
8 |
Correct |
11 ms |
16768 KB |
Output is correct |
9 |
Correct |
13 ms |
16768 KB |
Output is correct |
10 |
Correct |
9 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16768 KB |
Output is correct |
2 |
Correct |
10 ms |
16768 KB |
Output is correct |
3 |
Correct |
10 ms |
16768 KB |
Output is correct |
4 |
Correct |
13 ms |
16768 KB |
Output is correct |
5 |
Correct |
13 ms |
16768 KB |
Output is correct |
6 |
Correct |
10 ms |
16768 KB |
Output is correct |
7 |
Correct |
10 ms |
16768 KB |
Output is correct |
8 |
Correct |
13 ms |
16768 KB |
Output is correct |
9 |
Correct |
12 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16768 KB |
Output is correct |
11 |
Correct |
12 ms |
16768 KB |
Output is correct |
12 |
Correct |
10 ms |
16768 KB |
Output is correct |
13 |
Correct |
11 ms |
16768 KB |
Output is correct |
14 |
Correct |
11 ms |
16896 KB |
Output is correct |
15 |
Correct |
14 ms |
16768 KB |
Output is correct |
16 |
Correct |
11 ms |
16768 KB |
Output is correct |
17 |
Correct |
11 ms |
16768 KB |
Output is correct |
18 |
Correct |
11 ms |
16768 KB |
Output is correct |
19 |
Correct |
11 ms |
16792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
10 ms |
16768 KB |
Output is correct |
7 |
Correct |
13 ms |
16768 KB |
Output is correct |
8 |
Correct |
11 ms |
16768 KB |
Output is correct |
9 |
Correct |
13 ms |
16768 KB |
Output is correct |
10 |
Correct |
9 ms |
16768 KB |
Output is correct |
11 |
Correct |
10 ms |
16768 KB |
Output is correct |
12 |
Correct |
10 ms |
16768 KB |
Output is correct |
13 |
Correct |
10 ms |
16768 KB |
Output is correct |
14 |
Correct |
13 ms |
16768 KB |
Output is correct |
15 |
Correct |
13 ms |
16768 KB |
Output is correct |
16 |
Correct |
10 ms |
16768 KB |
Output is correct |
17 |
Correct |
10 ms |
16768 KB |
Output is correct |
18 |
Correct |
13 ms |
16768 KB |
Output is correct |
19 |
Correct |
12 ms |
16768 KB |
Output is correct |
20 |
Correct |
11 ms |
16768 KB |
Output is correct |
21 |
Correct |
12 ms |
16768 KB |
Output is correct |
22 |
Correct |
10 ms |
16768 KB |
Output is correct |
23 |
Correct |
11 ms |
16768 KB |
Output is correct |
24 |
Correct |
11 ms |
16896 KB |
Output is correct |
25 |
Correct |
14 ms |
16768 KB |
Output is correct |
26 |
Correct |
11 ms |
16768 KB |
Output is correct |
27 |
Correct |
11 ms |
16768 KB |
Output is correct |
28 |
Correct |
11 ms |
16768 KB |
Output is correct |
29 |
Correct |
11 ms |
16792 KB |
Output is correct |
30 |
Correct |
14 ms |
16768 KB |
Output is correct |
31 |
Correct |
12 ms |
16860 KB |
Output is correct |
32 |
Correct |
12 ms |
16896 KB |
Output is correct |
33 |
Correct |
13 ms |
17024 KB |
Output is correct |
34 |
Correct |
13 ms |
17024 KB |
Output is correct |
35 |
Correct |
16 ms |
17024 KB |
Output is correct |
36 |
Correct |
12 ms |
17024 KB |
Output is correct |
37 |
Correct |
14 ms |
17152 KB |
Output is correct |
38 |
Correct |
12 ms |
17152 KB |
Output is correct |
39 |
Correct |
13 ms |
17152 KB |
Output is correct |
40 |
Correct |
15 ms |
17664 KB |
Output is correct |
41 |
Correct |
13 ms |
17664 KB |
Output is correct |
42 |
Correct |
18 ms |
17152 KB |
Output is correct |
43 |
Correct |
14 ms |
17408 KB |
Output is correct |
44 |
Correct |
14 ms |
17280 KB |
Output is correct |
45 |
Correct |
16 ms |
17280 KB |
Output is correct |
46 |
Correct |
13 ms |
17280 KB |
Output is correct |
47 |
Correct |
13 ms |
17280 KB |
Output is correct |
48 |
Correct |
13 ms |
17152 KB |
Output is correct |
49 |
Correct |
13 ms |
17152 KB |
Output is correct |
50 |
Correct |
13 ms |
17152 KB |
Output is correct |
51 |
Correct |
12 ms |
17152 KB |
Output is correct |
52 |
Correct |
12 ms |
17152 KB |
Output is correct |
53 |
Correct |
13 ms |
17280 KB |
Output is correct |
54 |
Correct |
15 ms |
17280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16768 KB |
Output is correct |
3 |
Correct |
11 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
10 ms |
16768 KB |
Output is correct |
7 |
Correct |
13 ms |
16768 KB |
Output is correct |
8 |
Correct |
11 ms |
16768 KB |
Output is correct |
9 |
Correct |
13 ms |
16768 KB |
Output is correct |
10 |
Correct |
9 ms |
16768 KB |
Output is correct |
11 |
Correct |
10 ms |
16768 KB |
Output is correct |
12 |
Correct |
10 ms |
16768 KB |
Output is correct |
13 |
Correct |
10 ms |
16768 KB |
Output is correct |
14 |
Correct |
13 ms |
16768 KB |
Output is correct |
15 |
Correct |
13 ms |
16768 KB |
Output is correct |
16 |
Correct |
10 ms |
16768 KB |
Output is correct |
17 |
Correct |
10 ms |
16768 KB |
Output is correct |
18 |
Correct |
13 ms |
16768 KB |
Output is correct |
19 |
Correct |
12 ms |
16768 KB |
Output is correct |
20 |
Correct |
11 ms |
16768 KB |
Output is correct |
21 |
Correct |
12 ms |
16768 KB |
Output is correct |
22 |
Correct |
10 ms |
16768 KB |
Output is correct |
23 |
Correct |
11 ms |
16768 KB |
Output is correct |
24 |
Correct |
11 ms |
16896 KB |
Output is correct |
25 |
Correct |
14 ms |
16768 KB |
Output is correct |
26 |
Correct |
11 ms |
16768 KB |
Output is correct |
27 |
Correct |
11 ms |
16768 KB |
Output is correct |
28 |
Correct |
11 ms |
16768 KB |
Output is correct |
29 |
Correct |
11 ms |
16792 KB |
Output is correct |
30 |
Correct |
14 ms |
16768 KB |
Output is correct |
31 |
Correct |
12 ms |
16860 KB |
Output is correct |
32 |
Correct |
12 ms |
16896 KB |
Output is correct |
33 |
Correct |
13 ms |
17024 KB |
Output is correct |
34 |
Correct |
13 ms |
17024 KB |
Output is correct |
35 |
Correct |
16 ms |
17024 KB |
Output is correct |
36 |
Correct |
12 ms |
17024 KB |
Output is correct |
37 |
Correct |
14 ms |
17152 KB |
Output is correct |
38 |
Correct |
12 ms |
17152 KB |
Output is correct |
39 |
Correct |
13 ms |
17152 KB |
Output is correct |
40 |
Correct |
15 ms |
17664 KB |
Output is correct |
41 |
Correct |
13 ms |
17664 KB |
Output is correct |
42 |
Correct |
18 ms |
17152 KB |
Output is correct |
43 |
Correct |
14 ms |
17408 KB |
Output is correct |
44 |
Correct |
14 ms |
17280 KB |
Output is correct |
45 |
Correct |
16 ms |
17280 KB |
Output is correct |
46 |
Correct |
13 ms |
17280 KB |
Output is correct |
47 |
Correct |
13 ms |
17280 KB |
Output is correct |
48 |
Correct |
13 ms |
17152 KB |
Output is correct |
49 |
Correct |
13 ms |
17152 KB |
Output is correct |
50 |
Correct |
13 ms |
17152 KB |
Output is correct |
51 |
Correct |
12 ms |
17152 KB |
Output is correct |
52 |
Correct |
12 ms |
17152 KB |
Output is correct |
53 |
Correct |
13 ms |
17280 KB |
Output is correct |
54 |
Correct |
15 ms |
17280 KB |
Output is correct |
55 |
Correct |
18 ms |
17792 KB |
Output is correct |
56 |
Correct |
48 ms |
20472 KB |
Output is correct |
57 |
Correct |
79 ms |
23164 KB |
Output is correct |
58 |
Correct |
100 ms |
24816 KB |
Output is correct |
59 |
Correct |
135 ms |
27632 KB |
Output is correct |
60 |
Correct |
184 ms |
30276 KB |
Output is correct |
61 |
Correct |
197 ms |
32500 KB |
Output is correct |
62 |
Correct |
224 ms |
34028 KB |
Output is correct |
63 |
Correct |
279 ms |
37228 KB |
Output is correct |
64 |
Correct |
301 ms |
38100 KB |
Output is correct |
65 |
Correct |
160 ms |
73336 KB |
Output is correct |
66 |
Correct |
162 ms |
73336 KB |
Output is correct |
67 |
Correct |
162 ms |
35832 KB |
Output is correct |
68 |
Correct |
200 ms |
54504 KB |
Output is correct |
69 |
Correct |
230 ms |
51304 KB |
Output is correct |
70 |
Correct |
239 ms |
51304 KB |
Output is correct |
71 |
Correct |
228 ms |
45968 KB |
Output is correct |
72 |
Correct |
226 ms |
46432 KB |
Output is correct |
73 |
Correct |
198 ms |
42464 KB |
Output is correct |
74 |
Correct |
198 ms |
42592 KB |
Output is correct |
75 |
Correct |
194 ms |
41588 KB |
Output is correct |
76 |
Correct |
199 ms |
41460 KB |
Output is correct |
77 |
Correct |
205 ms |
41464 KB |
Output is correct |
78 |
Correct |
201 ms |
39496 KB |
Output is correct |
79 |
Correct |
183 ms |
42340 KB |
Output is correct |