#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 = -1;
if(cmp()(i,adj[i].top())){
x = adj[i].top();
}else 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 |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
18 ms |
10876 KB |
Output is correct |
6 |
Correct |
32 ms |
15176 KB |
Output is correct |
7 |
Correct |
48 ms |
19480 KB |
Output is correct |
8 |
Correct |
61 ms |
23756 KB |
Output is correct |
9 |
Correct |
62 ms |
23776 KB |
Output is correct |
10 |
Correct |
67 ms |
23760 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
62 ms |
23736 KB |
Output is correct |
13 |
Correct |
61 ms |
23756 KB |
Output is correct |
14 |
Correct |
64 ms |
23812 KB |
Output is correct |
15 |
Correct |
60 ms |
23768 KB |
Output is correct |
16 |
Correct |
60 ms |
23884 KB |
Output is correct |
17 |
Correct |
62 ms |
23756 KB |
Output is correct |
18 |
Correct |
63 ms |
23752 KB |
Output is correct |
19 |
Correct |
66 ms |
23764 KB |
Output is correct |
20 |
Correct |
68 ms |
31024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
118 ms |
19776 KB |
Output is correct |
5 |
Correct |
119 ms |
19764 KB |
Output is correct |
6 |
Correct |
115 ms |
19760 KB |
Output is correct |
7 |
Correct |
123 ms |
19828 KB |
Output is correct |
8 |
Correct |
149 ms |
19752 KB |
Output is correct |
9 |
Correct |
119 ms |
19856 KB |
Output is correct |
10 |
Correct |
116 ms |
19792 KB |
Output is correct |
11 |
Correct |
115 ms |
19776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
7 ms |
7252 KB |
Output is correct |
6 |
Correct |
120 ms |
19848 KB |
Output is correct |
7 |
Correct |
120 ms |
19796 KB |
Output is correct |
8 |
Correct |
122 ms |
19776 KB |
Output is correct |
9 |
Correct |
129 ms |
19768 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
4 ms |
6612 KB |
Output is correct |
12 |
Correct |
6 ms |
7124 KB |
Output is correct |
13 |
Correct |
7 ms |
7252 KB |
Output is correct |
14 |
Correct |
120 ms |
19788 KB |
Output is correct |
15 |
Correct |
121 ms |
19760 KB |
Output is correct |
16 |
Correct |
124 ms |
19840 KB |
Output is correct |
17 |
Correct |
127 ms |
19824 KB |
Output is correct |
18 |
Correct |
120 ms |
19740 KB |
Output is correct |
19 |
Correct |
119 ms |
19760 KB |
Output is correct |
20 |
Correct |
121 ms |
19776 KB |
Output is correct |
21 |
Correct |
119 ms |
19780 KB |
Output is correct |
22 |
Correct |
133 ms |
19860 KB |
Output is correct |
23 |
Correct |
118 ms |
19820 KB |
Output is correct |
24 |
Correct |
122 ms |
19856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
62 ms |
23760 KB |
Output is correct |
3 |
Correct |
62 ms |
23836 KB |
Output is correct |
4 |
Correct |
63 ms |
23784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
18 ms |
10876 KB |
Output is correct |
6 |
Correct |
32 ms |
15176 KB |
Output is correct |
7 |
Correct |
48 ms |
19480 KB |
Output is correct |
8 |
Correct |
61 ms |
23756 KB |
Output is correct |
9 |
Correct |
62 ms |
23776 KB |
Output is correct |
10 |
Correct |
67 ms |
23760 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
62 ms |
23736 KB |
Output is correct |
13 |
Correct |
61 ms |
23756 KB |
Output is correct |
14 |
Correct |
64 ms |
23812 KB |
Output is correct |
15 |
Correct |
60 ms |
23768 KB |
Output is correct |
16 |
Correct |
60 ms |
23884 KB |
Output is correct |
17 |
Correct |
62 ms |
23756 KB |
Output is correct |
18 |
Correct |
63 ms |
23752 KB |
Output is correct |
19 |
Correct |
66 ms |
23764 KB |
Output is correct |
20 |
Correct |
68 ms |
31024 KB |
Output is correct |
21 |
Correct |
3 ms |
6484 KB |
Output is correct |
22 |
Correct |
3 ms |
6484 KB |
Output is correct |
23 |
Correct |
3 ms |
6484 KB |
Output is correct |
24 |
Correct |
118 ms |
19776 KB |
Output is correct |
25 |
Correct |
119 ms |
19764 KB |
Output is correct |
26 |
Correct |
115 ms |
19760 KB |
Output is correct |
27 |
Correct |
123 ms |
19828 KB |
Output is correct |
28 |
Correct |
149 ms |
19752 KB |
Output is correct |
29 |
Correct |
119 ms |
19856 KB |
Output is correct |
30 |
Correct |
116 ms |
19792 KB |
Output is correct |
31 |
Correct |
115 ms |
19776 KB |
Output is correct |
32 |
Correct |
4 ms |
6484 KB |
Output is correct |
33 |
Correct |
3 ms |
6484 KB |
Output is correct |
34 |
Correct |
3 ms |
6484 KB |
Output is correct |
35 |
Correct |
3 ms |
6612 KB |
Output is correct |
36 |
Correct |
7 ms |
7252 KB |
Output is correct |
37 |
Correct |
120 ms |
19848 KB |
Output is correct |
38 |
Correct |
120 ms |
19796 KB |
Output is correct |
39 |
Correct |
122 ms |
19776 KB |
Output is correct |
40 |
Correct |
129 ms |
19768 KB |
Output is correct |
41 |
Correct |
3 ms |
6484 KB |
Output is correct |
42 |
Correct |
4 ms |
6612 KB |
Output is correct |
43 |
Correct |
6 ms |
7124 KB |
Output is correct |
44 |
Correct |
7 ms |
7252 KB |
Output is correct |
45 |
Correct |
120 ms |
19788 KB |
Output is correct |
46 |
Correct |
121 ms |
19760 KB |
Output is correct |
47 |
Correct |
124 ms |
19840 KB |
Output is correct |
48 |
Correct |
127 ms |
19824 KB |
Output is correct |
49 |
Correct |
120 ms |
19740 KB |
Output is correct |
50 |
Correct |
119 ms |
19760 KB |
Output is correct |
51 |
Correct |
121 ms |
19776 KB |
Output is correct |
52 |
Correct |
119 ms |
19780 KB |
Output is correct |
53 |
Correct |
133 ms |
19860 KB |
Output is correct |
54 |
Correct |
118 ms |
19820 KB |
Output is correct |
55 |
Correct |
122 ms |
19856 KB |
Output is correct |
56 |
Correct |
3 ms |
6484 KB |
Output is correct |
57 |
Correct |
62 ms |
23760 KB |
Output is correct |
58 |
Correct |
62 ms |
23836 KB |
Output is correct |
59 |
Correct |
63 ms |
23784 KB |
Output is correct |
60 |
Correct |
3 ms |
6484 KB |
Output is correct |
61 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |