//叫んで 藻掻もがいて 瞼まぶたを腫らしても
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll , ll> pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 3e5 + 10;
const ll mod = 1e9+7;
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int n , m;
vector < pii > adj[maxn];
priority_queue < ll > slope[maxn];
ll ans = 0;
void merge(int u , int v){
if(slope[u].size() > slope[v].size())
swap(slope[u] , slope[v]);
while(slope[u].size())
slope[v].push(slope[u].top()) , slope[u].pop();
}
void dfs(int v, int w = 0){
if(adj[v].empty())
slope[v].push(0), slope[v].push(0);
for(auto uw : adj[v]){
int u = uw.first , w = uw.second;
dfs(u , w);
merge(u , v);
}
for(int i = 0 ; i < int(adj[v].size()) - 1 ; i ++)
slope[v].pop();
ll a = slope[v].top();slope[v].pop();
ll b = slope[v].top();slope[v].pop();
slope[v].push(a + w);
slope[v].push(b + w);
}
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n >> m;
n += m;
for(int i = 2 ; i <= n ; i ++){
int v , w;
cin >> v >> w;
adj[v].pb({i , w});
ans += w;
}
dfs(1);
slope[1].pop();
while(slope[1].size())
ans-=slope[1].top() , slope[1].pop();
cout << ans;
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
11 ms |
16736 KB |
Output is correct |
4 |
Correct |
11 ms |
16716 KB |
Output is correct |
5 |
Correct |
11 ms |
16760 KB |
Output is correct |
6 |
Correct |
11 ms |
16760 KB |
Output is correct |
7 |
Correct |
11 ms |
16652 KB |
Output is correct |
8 |
Correct |
11 ms |
16716 KB |
Output is correct |
9 |
Correct |
11 ms |
16716 KB |
Output is correct |
10 |
Correct |
11 ms |
16716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
11 ms |
16716 KB |
Output is correct |
4 |
Correct |
10 ms |
16716 KB |
Output is correct |
5 |
Correct |
11 ms |
16716 KB |
Output is correct |
6 |
Correct |
11 ms |
16716 KB |
Output is correct |
7 |
Correct |
11 ms |
16716 KB |
Output is correct |
8 |
Correct |
11 ms |
16716 KB |
Output is correct |
9 |
Correct |
12 ms |
16732 KB |
Output is correct |
10 |
Correct |
12 ms |
16764 KB |
Output is correct |
11 |
Correct |
11 ms |
16656 KB |
Output is correct |
12 |
Correct |
11 ms |
16760 KB |
Output is correct |
13 |
Correct |
11 ms |
16716 KB |
Output is correct |
14 |
Correct |
11 ms |
16716 KB |
Output is correct |
15 |
Correct |
11 ms |
16716 KB |
Output is correct |
16 |
Correct |
11 ms |
16716 KB |
Output is correct |
17 |
Correct |
11 ms |
16716 KB |
Output is correct |
18 |
Correct |
11 ms |
16716 KB |
Output is correct |
19 |
Correct |
11 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
11 ms |
16736 KB |
Output is correct |
4 |
Correct |
11 ms |
16716 KB |
Output is correct |
5 |
Correct |
11 ms |
16760 KB |
Output is correct |
6 |
Correct |
11 ms |
16760 KB |
Output is correct |
7 |
Correct |
11 ms |
16652 KB |
Output is correct |
8 |
Correct |
11 ms |
16716 KB |
Output is correct |
9 |
Correct |
11 ms |
16716 KB |
Output is correct |
10 |
Correct |
11 ms |
16716 KB |
Output is correct |
11 |
Correct |
11 ms |
16716 KB |
Output is correct |
12 |
Correct |
11 ms |
16716 KB |
Output is correct |
13 |
Correct |
11 ms |
16716 KB |
Output is correct |
14 |
Correct |
10 ms |
16716 KB |
Output is correct |
15 |
Correct |
11 ms |
16716 KB |
Output is correct |
16 |
Correct |
11 ms |
16716 KB |
Output is correct |
17 |
Correct |
11 ms |
16716 KB |
Output is correct |
18 |
Correct |
11 ms |
16716 KB |
Output is correct |
19 |
Correct |
12 ms |
16732 KB |
Output is correct |
20 |
Correct |
12 ms |
16764 KB |
Output is correct |
21 |
Correct |
11 ms |
16656 KB |
Output is correct |
22 |
Correct |
11 ms |
16760 KB |
Output is correct |
23 |
Correct |
11 ms |
16716 KB |
Output is correct |
24 |
Correct |
11 ms |
16716 KB |
Output is correct |
25 |
Correct |
11 ms |
16716 KB |
Output is correct |
26 |
Correct |
11 ms |
16716 KB |
Output is correct |
27 |
Correct |
11 ms |
16716 KB |
Output is correct |
28 |
Correct |
11 ms |
16716 KB |
Output is correct |
29 |
Correct |
11 ms |
16768 KB |
Output is correct |
30 |
Correct |
11 ms |
16716 KB |
Output is correct |
31 |
Correct |
12 ms |
16716 KB |
Output is correct |
32 |
Correct |
12 ms |
16768 KB |
Output is correct |
33 |
Correct |
13 ms |
16844 KB |
Output is correct |
34 |
Correct |
13 ms |
16896 KB |
Output is correct |
35 |
Correct |
13 ms |
16972 KB |
Output is correct |
36 |
Correct |
13 ms |
17020 KB |
Output is correct |
37 |
Correct |
14 ms |
17028 KB |
Output is correct |
38 |
Correct |
14 ms |
17036 KB |
Output is correct |
39 |
Correct |
14 ms |
17032 KB |
Output is correct |
40 |
Correct |
12 ms |
17612 KB |
Output is correct |
41 |
Correct |
13 ms |
17668 KB |
Output is correct |
42 |
Correct |
13 ms |
16972 KB |
Output is correct |
43 |
Correct |
14 ms |
17360 KB |
Output is correct |
44 |
Correct |
14 ms |
17284 KB |
Output is correct |
45 |
Correct |
14 ms |
17312 KB |
Output is correct |
46 |
Correct |
14 ms |
17248 KB |
Output is correct |
47 |
Correct |
14 ms |
17156 KB |
Output is correct |
48 |
Correct |
14 ms |
17028 KB |
Output is correct |
49 |
Correct |
15 ms |
17092 KB |
Output is correct |
50 |
Correct |
13 ms |
17076 KB |
Output is correct |
51 |
Correct |
13 ms |
17000 KB |
Output is correct |
52 |
Correct |
14 ms |
17100 KB |
Output is correct |
53 |
Correct |
14 ms |
17100 KB |
Output is correct |
54 |
Correct |
14 ms |
17100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16716 KB |
Output is correct |
2 |
Correct |
11 ms |
16716 KB |
Output is correct |
3 |
Correct |
11 ms |
16736 KB |
Output is correct |
4 |
Correct |
11 ms |
16716 KB |
Output is correct |
5 |
Correct |
11 ms |
16760 KB |
Output is correct |
6 |
Correct |
11 ms |
16760 KB |
Output is correct |
7 |
Correct |
11 ms |
16652 KB |
Output is correct |
8 |
Correct |
11 ms |
16716 KB |
Output is correct |
9 |
Correct |
11 ms |
16716 KB |
Output is correct |
10 |
Correct |
11 ms |
16716 KB |
Output is correct |
11 |
Correct |
11 ms |
16716 KB |
Output is correct |
12 |
Correct |
11 ms |
16716 KB |
Output is correct |
13 |
Correct |
11 ms |
16716 KB |
Output is correct |
14 |
Correct |
10 ms |
16716 KB |
Output is correct |
15 |
Correct |
11 ms |
16716 KB |
Output is correct |
16 |
Correct |
11 ms |
16716 KB |
Output is correct |
17 |
Correct |
11 ms |
16716 KB |
Output is correct |
18 |
Correct |
11 ms |
16716 KB |
Output is correct |
19 |
Correct |
12 ms |
16732 KB |
Output is correct |
20 |
Correct |
12 ms |
16764 KB |
Output is correct |
21 |
Correct |
11 ms |
16656 KB |
Output is correct |
22 |
Correct |
11 ms |
16760 KB |
Output is correct |
23 |
Correct |
11 ms |
16716 KB |
Output is correct |
24 |
Correct |
11 ms |
16716 KB |
Output is correct |
25 |
Correct |
11 ms |
16716 KB |
Output is correct |
26 |
Correct |
11 ms |
16716 KB |
Output is correct |
27 |
Correct |
11 ms |
16716 KB |
Output is correct |
28 |
Correct |
11 ms |
16716 KB |
Output is correct |
29 |
Correct |
11 ms |
16768 KB |
Output is correct |
30 |
Correct |
11 ms |
16716 KB |
Output is correct |
31 |
Correct |
12 ms |
16716 KB |
Output is correct |
32 |
Correct |
12 ms |
16768 KB |
Output is correct |
33 |
Correct |
13 ms |
16844 KB |
Output is correct |
34 |
Correct |
13 ms |
16896 KB |
Output is correct |
35 |
Correct |
13 ms |
16972 KB |
Output is correct |
36 |
Correct |
13 ms |
17020 KB |
Output is correct |
37 |
Correct |
14 ms |
17028 KB |
Output is correct |
38 |
Correct |
14 ms |
17036 KB |
Output is correct |
39 |
Correct |
14 ms |
17032 KB |
Output is correct |
40 |
Correct |
12 ms |
17612 KB |
Output is correct |
41 |
Correct |
13 ms |
17668 KB |
Output is correct |
42 |
Correct |
13 ms |
16972 KB |
Output is correct |
43 |
Correct |
14 ms |
17360 KB |
Output is correct |
44 |
Correct |
14 ms |
17284 KB |
Output is correct |
45 |
Correct |
14 ms |
17312 KB |
Output is correct |
46 |
Correct |
14 ms |
17248 KB |
Output is correct |
47 |
Correct |
14 ms |
17156 KB |
Output is correct |
48 |
Correct |
14 ms |
17028 KB |
Output is correct |
49 |
Correct |
15 ms |
17092 KB |
Output is correct |
50 |
Correct |
13 ms |
17076 KB |
Output is correct |
51 |
Correct |
13 ms |
17000 KB |
Output is correct |
52 |
Correct |
14 ms |
17100 KB |
Output is correct |
53 |
Correct |
14 ms |
17100 KB |
Output is correct |
54 |
Correct |
14 ms |
17100 KB |
Output is correct |
55 |
Correct |
20 ms |
17664 KB |
Output is correct |
56 |
Correct |
48 ms |
20132 KB |
Output is correct |
57 |
Correct |
77 ms |
22512 KB |
Output is correct |
58 |
Correct |
102 ms |
24200 KB |
Output is correct |
59 |
Correct |
134 ms |
26596 KB |
Output is correct |
60 |
Correct |
167 ms |
29116 KB |
Output is correct |
61 |
Correct |
191 ms |
31224 KB |
Output is correct |
62 |
Correct |
211 ms |
32488 KB |
Output is correct |
63 |
Correct |
254 ms |
35496 KB |
Output is correct |
64 |
Correct |
282 ms |
36396 KB |
Output is correct |
65 |
Correct |
144 ms |
73216 KB |
Output is correct |
66 |
Correct |
143 ms |
73252 KB |
Output is correct |
67 |
Correct |
136 ms |
31096 KB |
Output is correct |
68 |
Correct |
216 ms |
55736 KB |
Output is correct |
69 |
Correct |
250 ms |
52764 KB |
Output is correct |
70 |
Correct |
248 ms |
52788 KB |
Output is correct |
71 |
Correct |
258 ms |
48656 KB |
Output is correct |
72 |
Correct |
256 ms |
48948 KB |
Output is correct |
73 |
Correct |
239 ms |
44412 KB |
Output is correct |
74 |
Correct |
236 ms |
44588 KB |
Output is correct |
75 |
Correct |
240 ms |
42860 KB |
Output is correct |
76 |
Correct |
235 ms |
42792 KB |
Output is correct |
77 |
Correct |
239 ms |
42408 KB |
Output is correct |
78 |
Correct |
266 ms |
40304 KB |
Output is correct |
79 |
Correct |
221 ms |
41836 KB |
Output is correct |