#include<bits/stdc++.h>
using namespace std;
long long D[200001],U[200001];
bool comp(int a,int b){
return (D[a]*U[b])<(D[b]*U[a]);
}
struct cmp {
bool operator()(const int &a, const int &b) {
return (D[a]*U[b])>(D[b]*U[a]);
}
};
priority_queue<int,vector<int>,cmp> adj[200001];
vector<int> ans;
void dfs(int u) {
ans.push_back(u);
for(;adj[u].size();adj[u].pop())dfs(adj[u].top());
}
long long scheduling_cost(vector<int> p,vector<int> u,vector<int> d){
int n = p.size();
long long nxt[n],nick_name[n];
U[0] = u[0] , D[0] = d[0];
nxt[0] = -1;nick_name[0] = 0;
for(int i = 1;i<n;i++){
nick_name[i] = i;
nxt[i] = -1;
U[i] = u[i];
D[i] = d[i];
adj[p[i]].push(i);
}
for(int i = n-1;i>=0;i--){
while(adj[i].size()){
int x = adj[i].top();
if(cmp()(adj[i].top(),i)){
break;
}
adj[i].pop();
nxt[nick_name[i]] = x;
nick_name[i] = nick_name[x];
D[i]+=D[x];
U[i]+=U[x];
if(adj[i].size()<adj[x].size())swap(adj[i],adj[x]);
while(adj[x].size()){
adj[i].push(adj[x].top());adj[x].pop();
}
}
}
dfs(0);
sort(ans.begin(),ans.end(),cmp());
long long all = 0 , cur = 0;
for(int i = ans.size()-1;i>=0;i--){
int lol = ans[i];
while(lol!=-1){
cur+=d[lol];
all+=cur*u[lol];
lol = nxt[lol];
}
}
return all;
}/*
int main(){
cout<<scheduling_cost({-1,0,0},{5,2,5},{3,4,1});
}*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
5 ms |
6572 KB |
Output is correct |
3 |
Correct |
4 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
21 ms |
11292 KB |
Output is correct |
6 |
Correct |
34 ms |
15668 KB |
Output is correct |
7 |
Correct |
52 ms |
19908 KB |
Output is correct |
8 |
Correct |
61 ms |
24344 KB |
Output is correct |
9 |
Correct |
61 ms |
24256 KB |
Output is correct |
10 |
Correct |
72 ms |
24236 KB |
Output is correct |
11 |
Correct |
3 ms |
6564 KB |
Output is correct |
12 |
Correct |
63 ms |
24236 KB |
Output is correct |
13 |
Correct |
87 ms |
24216 KB |
Output is correct |
14 |
Correct |
67 ms |
24252 KB |
Output is correct |
15 |
Correct |
59 ms |
24248 KB |
Output is correct |
16 |
Correct |
87 ms |
24384 KB |
Output is correct |
17 |
Correct |
64 ms |
24260 KB |
Output is correct |
18 |
Correct |
83 ms |
24272 KB |
Output is correct |
19 |
Correct |
62 ms |
24204 KB |
Output is correct |
20 |
Correct |
67 ms |
31512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6496 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
141 ms |
20748 KB |
Output is correct |
5 |
Correct |
135 ms |
20712 KB |
Output is correct |
6 |
Correct |
124 ms |
20756 KB |
Output is correct |
7 |
Correct |
163 ms |
20684 KB |
Output is correct |
8 |
Correct |
125 ms |
20736 KB |
Output is correct |
9 |
Correct |
126 ms |
20756 KB |
Output is correct |
10 |
Correct |
157 ms |
20748 KB |
Output is correct |
11 |
Correct |
126 ms |
20700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6564 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6560 KB |
Output is correct |
5 |
Correct |
7 ms |
7344 KB |
Output is correct |
6 |
Correct |
135 ms |
20668 KB |
Output is correct |
7 |
Correct |
133 ms |
20648 KB |
Output is correct |
8 |
Correct |
125 ms |
20716 KB |
Output is correct |
9 |
Correct |
145 ms |
20716 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
5 ms |
6580 KB |
Output is correct |
12 |
Correct |
7 ms |
7208 KB |
Output is correct |
13 |
Correct |
8 ms |
7348 KB |
Output is correct |
14 |
Correct |
124 ms |
20676 KB |
Output is correct |
15 |
Correct |
155 ms |
20664 KB |
Output is correct |
16 |
Correct |
147 ms |
20684 KB |
Output is correct |
17 |
Correct |
122 ms |
20736 KB |
Output is correct |
18 |
Correct |
131 ms |
20672 KB |
Output is correct |
19 |
Correct |
127 ms |
20672 KB |
Output is correct |
20 |
Correct |
130 ms |
20732 KB |
Output is correct |
21 |
Correct |
120 ms |
20744 KB |
Output is correct |
22 |
Correct |
121 ms |
20668 KB |
Output is correct |
23 |
Correct |
125 ms |
20724 KB |
Output is correct |
24 |
Correct |
123 ms |
20760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
61 ms |
24364 KB |
Output is correct |
3 |
Correct |
64 ms |
24180 KB |
Output is correct |
4 |
Correct |
65 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Incorrect |
4 ms |
6464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
5 ms |
6572 KB |
Output is correct |
3 |
Correct |
4 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
21 ms |
11292 KB |
Output is correct |
6 |
Correct |
34 ms |
15668 KB |
Output is correct |
7 |
Correct |
52 ms |
19908 KB |
Output is correct |
8 |
Correct |
61 ms |
24344 KB |
Output is correct |
9 |
Correct |
61 ms |
24256 KB |
Output is correct |
10 |
Correct |
72 ms |
24236 KB |
Output is correct |
11 |
Correct |
3 ms |
6564 KB |
Output is correct |
12 |
Correct |
63 ms |
24236 KB |
Output is correct |
13 |
Correct |
87 ms |
24216 KB |
Output is correct |
14 |
Correct |
67 ms |
24252 KB |
Output is correct |
15 |
Correct |
59 ms |
24248 KB |
Output is correct |
16 |
Correct |
87 ms |
24384 KB |
Output is correct |
17 |
Correct |
64 ms |
24260 KB |
Output is correct |
18 |
Correct |
83 ms |
24272 KB |
Output is correct |
19 |
Correct |
62 ms |
24204 KB |
Output is correct |
20 |
Correct |
67 ms |
31512 KB |
Output is correct |
21 |
Correct |
4 ms |
6484 KB |
Output is correct |
22 |
Correct |
3 ms |
6496 KB |
Output is correct |
23 |
Correct |
3 ms |
6484 KB |
Output is correct |
24 |
Correct |
141 ms |
20748 KB |
Output is correct |
25 |
Correct |
135 ms |
20712 KB |
Output is correct |
26 |
Correct |
124 ms |
20756 KB |
Output is correct |
27 |
Correct |
163 ms |
20684 KB |
Output is correct |
28 |
Correct |
125 ms |
20736 KB |
Output is correct |
29 |
Correct |
126 ms |
20756 KB |
Output is correct |
30 |
Correct |
157 ms |
20748 KB |
Output is correct |
31 |
Correct |
126 ms |
20700 KB |
Output is correct |
32 |
Correct |
3 ms |
6484 KB |
Output is correct |
33 |
Correct |
3 ms |
6564 KB |
Output is correct |
34 |
Correct |
3 ms |
6484 KB |
Output is correct |
35 |
Correct |
4 ms |
6560 KB |
Output is correct |
36 |
Correct |
7 ms |
7344 KB |
Output is correct |
37 |
Correct |
135 ms |
20668 KB |
Output is correct |
38 |
Correct |
133 ms |
20648 KB |
Output is correct |
39 |
Correct |
125 ms |
20716 KB |
Output is correct |
40 |
Correct |
145 ms |
20716 KB |
Output is correct |
41 |
Correct |
3 ms |
6484 KB |
Output is correct |
42 |
Correct |
5 ms |
6580 KB |
Output is correct |
43 |
Correct |
7 ms |
7208 KB |
Output is correct |
44 |
Correct |
8 ms |
7348 KB |
Output is correct |
45 |
Correct |
124 ms |
20676 KB |
Output is correct |
46 |
Correct |
155 ms |
20664 KB |
Output is correct |
47 |
Correct |
147 ms |
20684 KB |
Output is correct |
48 |
Correct |
122 ms |
20736 KB |
Output is correct |
49 |
Correct |
131 ms |
20672 KB |
Output is correct |
50 |
Correct |
127 ms |
20672 KB |
Output is correct |
51 |
Correct |
130 ms |
20732 KB |
Output is correct |
52 |
Correct |
120 ms |
20744 KB |
Output is correct |
53 |
Correct |
121 ms |
20668 KB |
Output is correct |
54 |
Correct |
125 ms |
20724 KB |
Output is correct |
55 |
Correct |
123 ms |
20760 KB |
Output is correct |
56 |
Correct |
3 ms |
6484 KB |
Output is correct |
57 |
Correct |
61 ms |
24364 KB |
Output is correct |
58 |
Correct |
64 ms |
24180 KB |
Output is correct |
59 |
Correct |
65 ms |
24312 KB |
Output is correct |
60 |
Correct |
3 ms |
6484 KB |
Output is correct |
61 |
Incorrect |
4 ms |
6464 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |