// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 3e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
int n, m;
vector<int> G[N];
int C[N];
priority_queue<ll> pq[N];
void DFS(int u){
if(G[u].empty()){
pq[u].push(C[u]);
pq[u].push(C[u]);
return ;
}
for(auto adj : G[u]){
DFS(adj);
if(pq[adj].size() > pq[u].size())
swap(pq[u], pq[adj]);
while(!pq[adj].empty()){
pq[u].push(pq[adj].top());
pq[adj].pop();
}
}
for(int i = 0; i + 1 < (int) G[u].size(); i++)
pq[u].pop();
ll sn = pq[u].top();
pq[u].pop();
ll s0 = pq[u].top();
pq[u].pop();
pq[u].push(sn + C[u]);
pq[u].push(s0 + C[u]);
//cerr << u << ' ' << pq[u].size() << '\n';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
int p;
ll Sum = 0;
for(int i = 2; i <= n + m; i++){
cin >> p >> C[i];
G[p].pb(i);
Sum += C[i];
}
//cerr << "#########\n";
DFS(1);
vector<ll> V;
while(!pq[1].empty()){
V.pb(pq[1].top());
pq[1].pop();
}
//debug
reverse(all(V));
ll M = 1 - ((int) V.size());
V.insert(V.begin(), 0);
//debug(V[0]);
//debug(M);
ll po = 0;
while(M < 0){
//cerr << M << ' ' << V[po + 1] - V[po] << '\n';
Sum += M * (V[po + 1] - V[po]);
po ++;
M ++;
}
cout << Sum << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 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 |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
13 ms |
16768 KB |
Output is correct |
7 |
Correct |
11 ms |
16768 KB |
Output is correct |
8 |
Correct |
12 ms |
16768 KB |
Output is correct |
9 |
Correct |
11 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
16828 KB |
Output is correct |
2 |
Correct |
12 ms |
16768 KB |
Output is correct |
3 |
Correct |
12 ms |
16768 KB |
Output is correct |
4 |
Correct |
12 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
13 ms |
16768 KB |
Output is correct |
7 |
Correct |
12 ms |
16768 KB |
Output is correct |
8 |
Correct |
12 ms |
16768 KB |
Output is correct |
9 |
Correct |
14 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16768 KB |
Output is correct |
11 |
Correct |
11 ms |
16768 KB |
Output is correct |
12 |
Correct |
12 ms |
16768 KB |
Output is correct |
13 |
Correct |
11 ms |
16768 KB |
Output is correct |
14 |
Correct |
12 ms |
16768 KB |
Output is correct |
15 |
Correct |
12 ms |
16768 KB |
Output is correct |
16 |
Correct |
12 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 |
16768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 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 |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
13 ms |
16768 KB |
Output is correct |
7 |
Correct |
11 ms |
16768 KB |
Output is correct |
8 |
Correct |
12 ms |
16768 KB |
Output is correct |
9 |
Correct |
11 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
11 |
Correct |
13 ms |
16828 KB |
Output is correct |
12 |
Correct |
12 ms |
16768 KB |
Output is correct |
13 |
Correct |
12 ms |
16768 KB |
Output is correct |
14 |
Correct |
12 ms |
16768 KB |
Output is correct |
15 |
Correct |
11 ms |
16768 KB |
Output is correct |
16 |
Correct |
13 ms |
16768 KB |
Output is correct |
17 |
Correct |
12 ms |
16768 KB |
Output is correct |
18 |
Correct |
12 ms |
16768 KB |
Output is correct |
19 |
Correct |
14 ms |
16768 KB |
Output is correct |
20 |
Correct |
11 ms |
16768 KB |
Output is correct |
21 |
Correct |
11 ms |
16768 KB |
Output is correct |
22 |
Correct |
12 ms |
16768 KB |
Output is correct |
23 |
Correct |
11 ms |
16768 KB |
Output is correct |
24 |
Correct |
12 ms |
16768 KB |
Output is correct |
25 |
Correct |
12 ms |
16768 KB |
Output is correct |
26 |
Correct |
12 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 |
16768 KB |
Output is correct |
30 |
Correct |
13 ms |
16768 KB |
Output is correct |
31 |
Correct |
12 ms |
16896 KB |
Output is correct |
32 |
Correct |
12 ms |
16896 KB |
Output is correct |
33 |
Correct |
13 ms |
16896 KB |
Output is correct |
34 |
Correct |
13 ms |
16896 KB |
Output is correct |
35 |
Correct |
14 ms |
17024 KB |
Output is correct |
36 |
Correct |
14 ms |
17024 KB |
Output is correct |
37 |
Correct |
14 ms |
17024 KB |
Output is correct |
38 |
Correct |
14 ms |
17024 KB |
Output is correct |
39 |
Correct |
15 ms |
17024 KB |
Output is correct |
40 |
Correct |
13 ms |
17536 KB |
Output is correct |
41 |
Correct |
13 ms |
17536 KB |
Output is correct |
42 |
Correct |
14 ms |
17024 KB |
Output is correct |
43 |
Correct |
14 ms |
17280 KB |
Output is correct |
44 |
Correct |
15 ms |
17280 KB |
Output is correct |
45 |
Correct |
15 ms |
17280 KB |
Output is correct |
46 |
Correct |
15 ms |
17280 KB |
Output is correct |
47 |
Correct |
15 ms |
17280 KB |
Output is correct |
48 |
Correct |
15 ms |
17152 KB |
Output is correct |
49 |
Correct |
15 ms |
17152 KB |
Output is correct |
50 |
Correct |
14 ms |
17152 KB |
Output is correct |
51 |
Correct |
15 ms |
17152 KB |
Output is correct |
52 |
Correct |
14 ms |
17152 KB |
Output is correct |
53 |
Correct |
15 ms |
17152 KB |
Output is correct |
54 |
Correct |
14 ms |
17152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 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 |
11 ms |
16768 KB |
Output is correct |
5 |
Correct |
11 ms |
16768 KB |
Output is correct |
6 |
Correct |
13 ms |
16768 KB |
Output is correct |
7 |
Correct |
11 ms |
16768 KB |
Output is correct |
8 |
Correct |
12 ms |
16768 KB |
Output is correct |
9 |
Correct |
11 ms |
16768 KB |
Output is correct |
10 |
Correct |
13 ms |
16768 KB |
Output is correct |
11 |
Correct |
13 ms |
16828 KB |
Output is correct |
12 |
Correct |
12 ms |
16768 KB |
Output is correct |
13 |
Correct |
12 ms |
16768 KB |
Output is correct |
14 |
Correct |
12 ms |
16768 KB |
Output is correct |
15 |
Correct |
11 ms |
16768 KB |
Output is correct |
16 |
Correct |
13 ms |
16768 KB |
Output is correct |
17 |
Correct |
12 ms |
16768 KB |
Output is correct |
18 |
Correct |
12 ms |
16768 KB |
Output is correct |
19 |
Correct |
14 ms |
16768 KB |
Output is correct |
20 |
Correct |
11 ms |
16768 KB |
Output is correct |
21 |
Correct |
11 ms |
16768 KB |
Output is correct |
22 |
Correct |
12 ms |
16768 KB |
Output is correct |
23 |
Correct |
11 ms |
16768 KB |
Output is correct |
24 |
Correct |
12 ms |
16768 KB |
Output is correct |
25 |
Correct |
12 ms |
16768 KB |
Output is correct |
26 |
Correct |
12 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 |
16768 KB |
Output is correct |
30 |
Correct |
13 ms |
16768 KB |
Output is correct |
31 |
Correct |
12 ms |
16896 KB |
Output is correct |
32 |
Correct |
12 ms |
16896 KB |
Output is correct |
33 |
Correct |
13 ms |
16896 KB |
Output is correct |
34 |
Correct |
13 ms |
16896 KB |
Output is correct |
35 |
Correct |
14 ms |
17024 KB |
Output is correct |
36 |
Correct |
14 ms |
17024 KB |
Output is correct |
37 |
Correct |
14 ms |
17024 KB |
Output is correct |
38 |
Correct |
14 ms |
17024 KB |
Output is correct |
39 |
Correct |
15 ms |
17024 KB |
Output is correct |
40 |
Correct |
13 ms |
17536 KB |
Output is correct |
41 |
Correct |
13 ms |
17536 KB |
Output is correct |
42 |
Correct |
14 ms |
17024 KB |
Output is correct |
43 |
Correct |
14 ms |
17280 KB |
Output is correct |
44 |
Correct |
15 ms |
17280 KB |
Output is correct |
45 |
Correct |
15 ms |
17280 KB |
Output is correct |
46 |
Correct |
15 ms |
17280 KB |
Output is correct |
47 |
Correct |
15 ms |
17280 KB |
Output is correct |
48 |
Correct |
15 ms |
17152 KB |
Output is correct |
49 |
Correct |
15 ms |
17152 KB |
Output is correct |
50 |
Correct |
14 ms |
17152 KB |
Output is correct |
51 |
Correct |
15 ms |
17152 KB |
Output is correct |
52 |
Correct |
14 ms |
17152 KB |
Output is correct |
53 |
Correct |
15 ms |
17152 KB |
Output is correct |
54 |
Correct |
14 ms |
17152 KB |
Output is correct |
55 |
Correct |
21 ms |
17536 KB |
Output is correct |
56 |
Correct |
49 ms |
19700 KB |
Output is correct |
57 |
Correct |
78 ms |
21484 KB |
Output is correct |
58 |
Correct |
110 ms |
23020 KB |
Output is correct |
59 |
Correct |
137 ms |
24816 KB |
Output is correct |
60 |
Correct |
173 ms |
26620 KB |
Output is correct |
61 |
Correct |
216 ms |
28744 KB |
Output is correct |
62 |
Correct |
225 ms |
33260 KB |
Output is correct |
63 |
Correct |
281 ms |
36416 KB |
Output is correct |
64 |
Correct |
313 ms |
37100 KB |
Output is correct |
65 |
Correct |
198 ms |
65144 KB |
Output is correct |
66 |
Correct |
168 ms |
65144 KB |
Output is correct |
67 |
Correct |
164 ms |
32380 KB |
Output is correct |
68 |
Correct |
223 ms |
52836 KB |
Output is correct |
69 |
Correct |
254 ms |
50020 KB |
Output is correct |
70 |
Correct |
267 ms |
50020 KB |
Output is correct |
71 |
Correct |
279 ms |
50912 KB |
Output is correct |
72 |
Correct |
277 ms |
50908 KB |
Output is correct |
73 |
Correct |
248 ms |
47464 KB |
Output is correct |
74 |
Correct |
246 ms |
47580 KB |
Output is correct |
75 |
Correct |
257 ms |
46300 KB |
Output is correct |
76 |
Correct |
249 ms |
46428 KB |
Output is correct |
77 |
Correct |
262 ms |
45388 KB |
Output is correct |
78 |
Correct |
270 ms |
44120 KB |
Output is correct |
79 |
Correct |
247 ms |
43224 KB |
Output is correct |