// 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 = 2e5 + 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11264 KB |
Output is correct |
2 |
Correct |
8 ms |
11264 KB |
Output is correct |
3 |
Correct |
8 ms |
11392 KB |
Output is correct |
4 |
Correct |
8 ms |
11264 KB |
Output is correct |
5 |
Correct |
8 ms |
11264 KB |
Output is correct |
6 |
Correct |
9 ms |
11264 KB |
Output is correct |
7 |
Correct |
8 ms |
11264 KB |
Output is correct |
8 |
Correct |
8 ms |
11264 KB |
Output is correct |
9 |
Correct |
8 ms |
11264 KB |
Output is correct |
10 |
Correct |
8 ms |
11264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11264 KB |
Output is correct |
2 |
Correct |
8 ms |
11264 KB |
Output is correct |
3 |
Correct |
8 ms |
11264 KB |
Output is correct |
4 |
Correct |
8 ms |
11264 KB |
Output is correct |
5 |
Correct |
8 ms |
11264 KB |
Output is correct |
6 |
Correct |
8 ms |
11264 KB |
Output is correct |
7 |
Correct |
8 ms |
11264 KB |
Output is correct |
8 |
Correct |
9 ms |
11264 KB |
Output is correct |
9 |
Correct |
8 ms |
11264 KB |
Output is correct |
10 |
Correct |
9 ms |
11264 KB |
Output is correct |
11 |
Correct |
8 ms |
11264 KB |
Output is correct |
12 |
Correct |
9 ms |
11264 KB |
Output is correct |
13 |
Correct |
8 ms |
11264 KB |
Output is correct |
14 |
Correct |
8 ms |
11264 KB |
Output is correct |
15 |
Correct |
8 ms |
11264 KB |
Output is correct |
16 |
Correct |
8 ms |
11264 KB |
Output is correct |
17 |
Correct |
8 ms |
11264 KB |
Output is correct |
18 |
Correct |
8 ms |
11264 KB |
Output is correct |
19 |
Correct |
8 ms |
11264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11264 KB |
Output is correct |
2 |
Correct |
8 ms |
11264 KB |
Output is correct |
3 |
Correct |
8 ms |
11392 KB |
Output is correct |
4 |
Correct |
8 ms |
11264 KB |
Output is correct |
5 |
Correct |
8 ms |
11264 KB |
Output is correct |
6 |
Correct |
9 ms |
11264 KB |
Output is correct |
7 |
Correct |
8 ms |
11264 KB |
Output is correct |
8 |
Correct |
8 ms |
11264 KB |
Output is correct |
9 |
Correct |
8 ms |
11264 KB |
Output is correct |
10 |
Correct |
8 ms |
11264 KB |
Output is correct |
11 |
Correct |
8 ms |
11264 KB |
Output is correct |
12 |
Correct |
8 ms |
11264 KB |
Output is correct |
13 |
Correct |
8 ms |
11264 KB |
Output is correct |
14 |
Correct |
8 ms |
11264 KB |
Output is correct |
15 |
Correct |
8 ms |
11264 KB |
Output is correct |
16 |
Correct |
8 ms |
11264 KB |
Output is correct |
17 |
Correct |
8 ms |
11264 KB |
Output is correct |
18 |
Correct |
9 ms |
11264 KB |
Output is correct |
19 |
Correct |
8 ms |
11264 KB |
Output is correct |
20 |
Correct |
9 ms |
11264 KB |
Output is correct |
21 |
Correct |
8 ms |
11264 KB |
Output is correct |
22 |
Correct |
9 ms |
11264 KB |
Output is correct |
23 |
Correct |
8 ms |
11264 KB |
Output is correct |
24 |
Correct |
8 ms |
11264 KB |
Output is correct |
25 |
Correct |
8 ms |
11264 KB |
Output is correct |
26 |
Correct |
8 ms |
11264 KB |
Output is correct |
27 |
Correct |
8 ms |
11264 KB |
Output is correct |
28 |
Correct |
8 ms |
11264 KB |
Output is correct |
29 |
Correct |
8 ms |
11264 KB |
Output is correct |
30 |
Correct |
8 ms |
11264 KB |
Output is correct |
31 |
Correct |
8 ms |
11392 KB |
Output is correct |
32 |
Correct |
10 ms |
11392 KB |
Output is correct |
33 |
Correct |
9 ms |
11520 KB |
Output is correct |
34 |
Correct |
9 ms |
11520 KB |
Output is correct |
35 |
Correct |
10 ms |
11648 KB |
Output is correct |
36 |
Correct |
10 ms |
11648 KB |
Output is correct |
37 |
Correct |
11 ms |
11648 KB |
Output is correct |
38 |
Correct |
11 ms |
11648 KB |
Output is correct |
39 |
Correct |
12 ms |
11648 KB |
Output is correct |
40 |
Correct |
10 ms |
12032 KB |
Output is correct |
41 |
Correct |
10 ms |
12032 KB |
Output is correct |
42 |
Correct |
10 ms |
11520 KB |
Output is correct |
43 |
Correct |
12 ms |
11904 KB |
Output is correct |
44 |
Correct |
11 ms |
11776 KB |
Output is correct |
45 |
Correct |
12 ms |
11776 KB |
Output is correct |
46 |
Correct |
11 ms |
11776 KB |
Output is correct |
47 |
Correct |
11 ms |
11776 KB |
Output is correct |
48 |
Correct |
11 ms |
11776 KB |
Output is correct |
49 |
Correct |
11 ms |
11776 KB |
Output is correct |
50 |
Correct |
11 ms |
11648 KB |
Output is correct |
51 |
Correct |
10 ms |
11648 KB |
Output is correct |
52 |
Correct |
11 ms |
11648 KB |
Output is correct |
53 |
Correct |
11 ms |
11776 KB |
Output is correct |
54 |
Correct |
11 ms |
11776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11264 KB |
Output is correct |
2 |
Correct |
8 ms |
11264 KB |
Output is correct |
3 |
Correct |
8 ms |
11392 KB |
Output is correct |
4 |
Correct |
8 ms |
11264 KB |
Output is correct |
5 |
Correct |
8 ms |
11264 KB |
Output is correct |
6 |
Correct |
9 ms |
11264 KB |
Output is correct |
7 |
Correct |
8 ms |
11264 KB |
Output is correct |
8 |
Correct |
8 ms |
11264 KB |
Output is correct |
9 |
Correct |
8 ms |
11264 KB |
Output is correct |
10 |
Correct |
8 ms |
11264 KB |
Output is correct |
11 |
Correct |
8 ms |
11264 KB |
Output is correct |
12 |
Correct |
8 ms |
11264 KB |
Output is correct |
13 |
Correct |
8 ms |
11264 KB |
Output is correct |
14 |
Correct |
8 ms |
11264 KB |
Output is correct |
15 |
Correct |
8 ms |
11264 KB |
Output is correct |
16 |
Correct |
8 ms |
11264 KB |
Output is correct |
17 |
Correct |
8 ms |
11264 KB |
Output is correct |
18 |
Correct |
9 ms |
11264 KB |
Output is correct |
19 |
Correct |
8 ms |
11264 KB |
Output is correct |
20 |
Correct |
9 ms |
11264 KB |
Output is correct |
21 |
Correct |
8 ms |
11264 KB |
Output is correct |
22 |
Correct |
9 ms |
11264 KB |
Output is correct |
23 |
Correct |
8 ms |
11264 KB |
Output is correct |
24 |
Correct |
8 ms |
11264 KB |
Output is correct |
25 |
Correct |
8 ms |
11264 KB |
Output is correct |
26 |
Correct |
8 ms |
11264 KB |
Output is correct |
27 |
Correct |
8 ms |
11264 KB |
Output is correct |
28 |
Correct |
8 ms |
11264 KB |
Output is correct |
29 |
Correct |
8 ms |
11264 KB |
Output is correct |
30 |
Correct |
8 ms |
11264 KB |
Output is correct |
31 |
Correct |
8 ms |
11392 KB |
Output is correct |
32 |
Correct |
10 ms |
11392 KB |
Output is correct |
33 |
Correct |
9 ms |
11520 KB |
Output is correct |
34 |
Correct |
9 ms |
11520 KB |
Output is correct |
35 |
Correct |
10 ms |
11648 KB |
Output is correct |
36 |
Correct |
10 ms |
11648 KB |
Output is correct |
37 |
Correct |
11 ms |
11648 KB |
Output is correct |
38 |
Correct |
11 ms |
11648 KB |
Output is correct |
39 |
Correct |
12 ms |
11648 KB |
Output is correct |
40 |
Correct |
10 ms |
12032 KB |
Output is correct |
41 |
Correct |
10 ms |
12032 KB |
Output is correct |
42 |
Correct |
10 ms |
11520 KB |
Output is correct |
43 |
Correct |
12 ms |
11904 KB |
Output is correct |
44 |
Correct |
11 ms |
11776 KB |
Output is correct |
45 |
Correct |
12 ms |
11776 KB |
Output is correct |
46 |
Correct |
11 ms |
11776 KB |
Output is correct |
47 |
Correct |
11 ms |
11776 KB |
Output is correct |
48 |
Correct |
11 ms |
11776 KB |
Output is correct |
49 |
Correct |
11 ms |
11776 KB |
Output is correct |
50 |
Correct |
11 ms |
11648 KB |
Output is correct |
51 |
Correct |
10 ms |
11648 KB |
Output is correct |
52 |
Correct |
11 ms |
11648 KB |
Output is correct |
53 |
Correct |
11 ms |
11776 KB |
Output is correct |
54 |
Correct |
11 ms |
11776 KB |
Output is correct |
55 |
Correct |
18 ms |
12288 KB |
Output is correct |
56 |
Correct |
45 ms |
14964 KB |
Output is correct |
57 |
Correct |
77 ms |
17272 KB |
Output is correct |
58 |
Correct |
106 ms |
19184 KB |
Output is correct |
59 |
Correct |
137 ms |
21616 KB |
Output is correct |
60 |
Correct |
178 ms |
24048 KB |
Output is correct |
61 |
Runtime error |
105 ms |
35452 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
62 |
Halted |
0 ms |
0 KB |
- |