#include<bits/stdc++.h>
using namespace std;
long long D[200001],U[200001];
int nxt[200001],nick_name[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();
iota(nick_name,nick_name+n,0);
memset(nxt,-1,sizeof nick_name);
for(int i = n-1;i>=0;i--){
U[i] = u[i] , D[i] = d[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();
}
}
if(i)adj[p[i]].push(i);
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
8 ms |
7292 KB |
Output is correct |
4 |
Correct |
3 ms |
7364 KB |
Output is correct |
5 |
Correct |
18 ms |
11064 KB |
Output is correct |
6 |
Correct |
31 ms |
14788 KB |
Output is correct |
7 |
Correct |
55 ms |
18440 KB |
Output is correct |
8 |
Correct |
59 ms |
22096 KB |
Output is correct |
9 |
Correct |
59 ms |
22096 KB |
Output is correct |
10 |
Correct |
59 ms |
22204 KB |
Output is correct |
11 |
Correct |
3 ms |
7256 KB |
Output is correct |
12 |
Correct |
59 ms |
22208 KB |
Output is correct |
13 |
Correct |
58 ms |
22096 KB |
Output is correct |
14 |
Correct |
59 ms |
22220 KB |
Output is correct |
15 |
Correct |
71 ms |
22208 KB |
Output is correct |
16 |
Correct |
59 ms |
22356 KB |
Output is correct |
17 |
Correct |
59 ms |
22220 KB |
Output is correct |
18 |
Correct |
65 ms |
22284 KB |
Output is correct |
19 |
Correct |
56 ms |
22140 KB |
Output is correct |
20 |
Correct |
66 ms |
29452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
119 ms |
18240 KB |
Output is correct |
5 |
Correct |
120 ms |
18236 KB |
Output is correct |
6 |
Correct |
121 ms |
18212 KB |
Output is correct |
7 |
Correct |
120 ms |
18216 KB |
Output is correct |
8 |
Correct |
124 ms |
18228 KB |
Output is correct |
9 |
Correct |
118 ms |
18204 KB |
Output is correct |
10 |
Correct |
130 ms |
18276 KB |
Output is correct |
11 |
Correct |
128 ms |
18236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
8 ms |
7892 KB |
Output is correct |
6 |
Correct |
120 ms |
18288 KB |
Output is correct |
7 |
Correct |
122 ms |
18220 KB |
Output is correct |
8 |
Correct |
121 ms |
18248 KB |
Output is correct |
9 |
Correct |
119 ms |
18188 KB |
Output is correct |
10 |
Correct |
3 ms |
7252 KB |
Output is correct |
11 |
Correct |
4 ms |
7320 KB |
Output is correct |
12 |
Correct |
6 ms |
7764 KB |
Output is correct |
13 |
Correct |
7 ms |
7892 KB |
Output is correct |
14 |
Correct |
143 ms |
18204 KB |
Output is correct |
15 |
Correct |
124 ms |
18260 KB |
Output is correct |
16 |
Correct |
120 ms |
18296 KB |
Output is correct |
17 |
Correct |
120 ms |
18184 KB |
Output is correct |
18 |
Correct |
124 ms |
18260 KB |
Output is correct |
19 |
Correct |
125 ms |
18168 KB |
Output is correct |
20 |
Correct |
126 ms |
18228 KB |
Output is correct |
21 |
Correct |
121 ms |
18200 KB |
Output is correct |
22 |
Correct |
123 ms |
18204 KB |
Output is correct |
23 |
Correct |
165 ms |
18248 KB |
Output is correct |
24 |
Correct |
124 ms |
18188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
58 ms |
22228 KB |
Output is correct |
3 |
Correct |
59 ms |
22164 KB |
Output is correct |
4 |
Correct |
58 ms |
22204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7336 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7336 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7336 KB |
Output is correct |
8 |
Correct |
3 ms |
7252 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
3 ms |
7332 KB |
Output is correct |
11 |
Correct |
3 ms |
7380 KB |
Output is correct |
12 |
Correct |
5 ms |
7344 KB |
Output is correct |
13 |
Correct |
3 ms |
7252 KB |
Output is correct |
14 |
Correct |
3 ms |
7340 KB |
Output is correct |
15 |
Correct |
3 ms |
7340 KB |
Output is correct |
16 |
Correct |
3 ms |
7336 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
8 ms |
7292 KB |
Output is correct |
4 |
Correct |
3 ms |
7364 KB |
Output is correct |
5 |
Correct |
18 ms |
11064 KB |
Output is correct |
6 |
Correct |
31 ms |
14788 KB |
Output is correct |
7 |
Correct |
55 ms |
18440 KB |
Output is correct |
8 |
Correct |
59 ms |
22096 KB |
Output is correct |
9 |
Correct |
59 ms |
22096 KB |
Output is correct |
10 |
Correct |
59 ms |
22204 KB |
Output is correct |
11 |
Correct |
3 ms |
7256 KB |
Output is correct |
12 |
Correct |
59 ms |
22208 KB |
Output is correct |
13 |
Correct |
58 ms |
22096 KB |
Output is correct |
14 |
Correct |
59 ms |
22220 KB |
Output is correct |
15 |
Correct |
71 ms |
22208 KB |
Output is correct |
16 |
Correct |
59 ms |
22356 KB |
Output is correct |
17 |
Correct |
59 ms |
22220 KB |
Output is correct |
18 |
Correct |
65 ms |
22284 KB |
Output is correct |
19 |
Correct |
56 ms |
22140 KB |
Output is correct |
20 |
Correct |
66 ms |
29452 KB |
Output is correct |
21 |
Correct |
3 ms |
7252 KB |
Output is correct |
22 |
Correct |
3 ms |
7252 KB |
Output is correct |
23 |
Correct |
3 ms |
7252 KB |
Output is correct |
24 |
Correct |
119 ms |
18240 KB |
Output is correct |
25 |
Correct |
120 ms |
18236 KB |
Output is correct |
26 |
Correct |
121 ms |
18212 KB |
Output is correct |
27 |
Correct |
120 ms |
18216 KB |
Output is correct |
28 |
Correct |
124 ms |
18228 KB |
Output is correct |
29 |
Correct |
118 ms |
18204 KB |
Output is correct |
30 |
Correct |
130 ms |
18276 KB |
Output is correct |
31 |
Correct |
128 ms |
18236 KB |
Output is correct |
32 |
Correct |
3 ms |
7252 KB |
Output is correct |
33 |
Correct |
3 ms |
7252 KB |
Output is correct |
34 |
Correct |
3 ms |
7252 KB |
Output is correct |
35 |
Correct |
3 ms |
7380 KB |
Output is correct |
36 |
Correct |
8 ms |
7892 KB |
Output is correct |
37 |
Correct |
120 ms |
18288 KB |
Output is correct |
38 |
Correct |
122 ms |
18220 KB |
Output is correct |
39 |
Correct |
121 ms |
18248 KB |
Output is correct |
40 |
Correct |
119 ms |
18188 KB |
Output is correct |
41 |
Correct |
3 ms |
7252 KB |
Output is correct |
42 |
Correct |
4 ms |
7320 KB |
Output is correct |
43 |
Correct |
6 ms |
7764 KB |
Output is correct |
44 |
Correct |
7 ms |
7892 KB |
Output is correct |
45 |
Correct |
143 ms |
18204 KB |
Output is correct |
46 |
Correct |
124 ms |
18260 KB |
Output is correct |
47 |
Correct |
120 ms |
18296 KB |
Output is correct |
48 |
Correct |
120 ms |
18184 KB |
Output is correct |
49 |
Correct |
124 ms |
18260 KB |
Output is correct |
50 |
Correct |
125 ms |
18168 KB |
Output is correct |
51 |
Correct |
126 ms |
18228 KB |
Output is correct |
52 |
Correct |
121 ms |
18200 KB |
Output is correct |
53 |
Correct |
123 ms |
18204 KB |
Output is correct |
54 |
Correct |
165 ms |
18248 KB |
Output is correct |
55 |
Correct |
124 ms |
18188 KB |
Output is correct |
56 |
Correct |
3 ms |
7252 KB |
Output is correct |
57 |
Correct |
58 ms |
22228 KB |
Output is correct |
58 |
Correct |
59 ms |
22164 KB |
Output is correct |
59 |
Correct |
58 ms |
22204 KB |
Output is correct |
60 |
Correct |
3 ms |
7252 KB |
Output is correct |
61 |
Correct |
4 ms |
7252 KB |
Output is correct |
62 |
Correct |
3 ms |
7336 KB |
Output is correct |
63 |
Correct |
3 ms |
7380 KB |
Output is correct |
64 |
Correct |
3 ms |
7336 KB |
Output is correct |
65 |
Correct |
3 ms |
7252 KB |
Output is correct |
66 |
Correct |
3 ms |
7336 KB |
Output is correct |
67 |
Correct |
3 ms |
7252 KB |
Output is correct |
68 |
Correct |
3 ms |
7380 KB |
Output is correct |
69 |
Correct |
3 ms |
7332 KB |
Output is correct |
70 |
Correct |
3 ms |
7380 KB |
Output is correct |
71 |
Correct |
5 ms |
7344 KB |
Output is correct |
72 |
Correct |
3 ms |
7252 KB |
Output is correct |
73 |
Correct |
3 ms |
7340 KB |
Output is correct |
74 |
Correct |
3 ms |
7340 KB |
Output is correct |
75 |
Correct |
3 ms |
7336 KB |
Output is correct |
76 |
Correct |
4 ms |
7380 KB |
Output is correct |
77 |
Correct |
3 ms |
7252 KB |
Output is correct |
78 |
Correct |
4 ms |
7252 KB |
Output is correct |
79 |
Correct |
3 ms |
7252 KB |
Output is correct |
80 |
Correct |
3 ms |
7252 KB |
Output is correct |
81 |
Correct |
3 ms |
7352 KB |
Output is correct |
82 |
Correct |
3 ms |
7340 KB |
Output is correct |
83 |
Correct |
4 ms |
7340 KB |
Output is correct |
84 |
Correct |
3 ms |
7252 KB |
Output is correct |
85 |
Correct |
3 ms |
7344 KB |
Output is correct |
86 |
Correct |
5 ms |
7252 KB |
Output is correct |
87 |
Correct |
4 ms |
7252 KB |
Output is correct |
88 |
Correct |
5 ms |
7764 KB |
Output is correct |
89 |
Correct |
9 ms |
7988 KB |
Output is correct |
90 |
Correct |
8 ms |
8128 KB |
Output is correct |
91 |
Correct |
25 ms |
11144 KB |
Output is correct |
92 |
Correct |
57 ms |
14524 KB |
Output is correct |
93 |
Correct |
103 ms |
20676 KB |
Output is correct |
94 |
Correct |
109 ms |
20720 KB |
Output is correct |
95 |
Correct |
113 ms |
20692 KB |
Output is correct |
96 |
Correct |
106 ms |
20720 KB |
Output is correct |
97 |
Correct |
98 ms |
20108 KB |
Output is correct |
98 |
Correct |
94 ms |
21096 KB |
Output is correct |
99 |
Correct |
99 ms |
20112 KB |
Output is correct |
100 |
Correct |
156 ms |
20696 KB |
Output is correct |
101 |
Correct |
95 ms |
21328 KB |
Output is correct |
102 |
Correct |
104 ms |
20940 KB |
Output is correct |
103 |
Correct |
93 ms |
20404 KB |
Output is correct |
104 |
Correct |
112 ms |
20816 KB |
Output is correct |
105 |
Correct |
85 ms |
21116 KB |
Output is correct |
106 |
Correct |
95 ms |
18920 KB |
Output is correct |