// O(N(logN)^2)
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
void dfs(int v,map<int,ll> &mp,vector<vi> &g,vi &H,vi &C){
for(auto u:g[v]){
map<int,ll> mp_;
dfs(u,mp_,g,H,C);
if(mp.size()<mp_.size()) mp.swap(mp_);
for(auto p:mp_) mp[p.first]+=p.second;
}
mp[H[v]]+=C[v];
ll t=C[v];
while(t){
auto p=mp.upper_bound(H[v]);
if(p==mp.end()) break;
ll sa=min(p->second,t);
p->second-=sa;
t-=sa;
if(p->second==0) mp.erase(p);
}
}
ll Solve(int N,vi A,vi H,vi C){
vector<vi> g(N);
ll res=0;
for(int i=1;i<N;i++) g[A[i]].push_back(i);
for(int i=0;i<N;i++) H[i]*=-1;
for(auto i:C) res+=i;
map<int,ll> mp;
dfs(0,mp,g,H,C);
for(auto p:mp) res-=p.second;
return res;
}
int N;
vi A,H,C;
int main(){
scanf("%d",&N);
A=H=C=vi(N);
vi deg(N),ind(N,-1);
vector<vi> g(N);
queue<int> q;
ll res=0;
for(int i=0;i<N;i++){
scanf("%d%d%d",&A[i],&H[i],&C[i]);
A[i]--;
deg[A[i]]++;
g[A[i]].push_back(i);
}
for(int i=0;i<N;i++) if(!deg[i]) q.push(i);
while(!q.empty()){
int v=q.front(),u=A[v];
q.pop();
deg[u]--;
if(!deg[u]) q.push(u);
}
for(int i=0;i<N;i++) if(ind[i]<0&°[i]){
int v=i,I=1;
vector<pair<int,int>> b;
do{
b.push_back({H[v],C[v]});
ind[v]=0;
for(auto u:g[v]) if(!deg[u]){
ind[u]=I++;
q.push(u);
}
v=A[v];
}while(v!=i);
sort(b.rbegin(),b.rend());
int N_=(int)b.size();
vi A_(N_),H_,C_;
for(int i=1;i<N_;i++) A_[i]=i-1;
for(auto p:b){
H_.push_back(p.first);
C_.push_back(p.second);
}
while(!q.empty()){
int u=q.front();
q.pop();
A_.push_back(ind[A[u]]+N_-1);
H_.push_back(H[u]);
C_.push_back(C[u]);
for(auto w:g[u]){
ind[w]=I++;
q.push(w);
}
}
N_=A_.size();
res+=Solve(N_,A_,H_,C_);
}
printf("%lld\n",res);
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:48:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
48 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
worst_reporter2.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
55 | scanf("%d%d%d",&A[i],&H[i],&C[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
7 ms |
1260 KB |
Output is correct |
6 |
Correct |
5 ms |
1004 KB |
Output is correct |
7 |
Correct |
5 ms |
1004 KB |
Output is correct |
8 |
Correct |
7 ms |
1260 KB |
Output is correct |
9 |
Correct |
5 ms |
1004 KB |
Output is correct |
10 |
Correct |
5 ms |
1024 KB |
Output is correct |
11 |
Correct |
4 ms |
1004 KB |
Output is correct |
12 |
Correct |
6 ms |
2156 KB |
Output is correct |
13 |
Correct |
5 ms |
2156 KB |
Output is correct |
14 |
Correct |
8 ms |
1644 KB |
Output is correct |
15 |
Correct |
5 ms |
1644 KB |
Output is correct |
16 |
Correct |
8 ms |
1260 KB |
Output is correct |
17 |
Correct |
7 ms |
1004 KB |
Output is correct |
18 |
Correct |
4 ms |
1004 KB |
Output is correct |
19 |
Correct |
6 ms |
1644 KB |
Output is correct |
20 |
Correct |
5 ms |
1516 KB |
Output is correct |
21 |
Correct |
4 ms |
1516 KB |
Output is correct |
22 |
Correct |
5 ms |
1152 KB |
Output is correct |
23 |
Correct |
4 ms |
876 KB |
Output is correct |
24 |
Correct |
6 ms |
1644 KB |
Output is correct |
25 |
Correct |
4 ms |
1516 KB |
Output is correct |
26 |
Correct |
5 ms |
2156 KB |
Output is correct |
27 |
Correct |
5 ms |
1644 KB |
Output is correct |
28 |
Correct |
6 ms |
1900 KB |
Output is correct |
29 |
Correct |
6 ms |
2156 KB |
Output is correct |
30 |
Correct |
6 ms |
2156 KB |
Output is correct |
31 |
Correct |
6 ms |
2176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1260 KB |
Output is correct |
2 |
Correct |
344 ms |
37464 KB |
Output is correct |
3 |
Correct |
255 ms |
25252 KB |
Output is correct |
4 |
Correct |
365 ms |
35188 KB |
Output is correct |
5 |
Correct |
243 ms |
25188 KB |
Output is correct |
6 |
Correct |
181 ms |
24908 KB |
Output is correct |
7 |
Correct |
193 ms |
24960 KB |
Output is correct |
8 |
Correct |
214 ms |
74824 KB |
Output is correct |
9 |
Correct |
199 ms |
74696 KB |
Output is correct |
10 |
Correct |
169 ms |
74696 KB |
Output is correct |
11 |
Correct |
206 ms |
52936 KB |
Output is correct |
12 |
Correct |
204 ms |
52936 KB |
Output is correct |
13 |
Correct |
333 ms |
37008 KB |
Output is correct |
14 |
Correct |
226 ms |
25168 KB |
Output is correct |
15 |
Correct |
141 ms |
24784 KB |
Output is correct |
16 |
Correct |
283 ms |
53480 KB |
Output is correct |
17 |
Correct |
158 ms |
46544 KB |
Output is correct |
18 |
Correct |
135 ms |
46544 KB |
Output is correct |
19 |
Correct |
291 ms |
32476 KB |
Output is correct |
20 |
Correct |
118 ms |
21344 KB |
Output is correct |
21 |
Correct |
319 ms |
54500 KB |
Output is correct |
22 |
Correct |
154 ms |
47904 KB |
Output is correct |
23 |
Correct |
187 ms |
74696 KB |
Output is correct |
24 |
Correct |
279 ms |
55956 KB |
Output is correct |
25 |
Correct |
245 ms |
70412 KB |
Output is correct |
26 |
Correct |
228 ms |
76944 KB |
Output is correct |
27 |
Correct |
266 ms |
71504 KB |
Output is correct |
28 |
Correct |
266 ms |
71600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
7 ms |
1260 KB |
Output is correct |
6 |
Correct |
5 ms |
1004 KB |
Output is correct |
7 |
Correct |
5 ms |
1004 KB |
Output is correct |
8 |
Correct |
7 ms |
1260 KB |
Output is correct |
9 |
Correct |
5 ms |
1004 KB |
Output is correct |
10 |
Correct |
5 ms |
1024 KB |
Output is correct |
11 |
Correct |
4 ms |
1004 KB |
Output is correct |
12 |
Correct |
6 ms |
2156 KB |
Output is correct |
13 |
Correct |
5 ms |
2156 KB |
Output is correct |
14 |
Correct |
8 ms |
1644 KB |
Output is correct |
15 |
Correct |
5 ms |
1644 KB |
Output is correct |
16 |
Correct |
8 ms |
1260 KB |
Output is correct |
17 |
Correct |
7 ms |
1004 KB |
Output is correct |
18 |
Correct |
4 ms |
1004 KB |
Output is correct |
19 |
Correct |
6 ms |
1644 KB |
Output is correct |
20 |
Correct |
5 ms |
1516 KB |
Output is correct |
21 |
Correct |
4 ms |
1516 KB |
Output is correct |
22 |
Correct |
5 ms |
1152 KB |
Output is correct |
23 |
Correct |
4 ms |
876 KB |
Output is correct |
24 |
Correct |
6 ms |
1644 KB |
Output is correct |
25 |
Correct |
4 ms |
1516 KB |
Output is correct |
26 |
Correct |
5 ms |
2156 KB |
Output is correct |
27 |
Correct |
5 ms |
1644 KB |
Output is correct |
28 |
Correct |
6 ms |
1900 KB |
Output is correct |
29 |
Correct |
6 ms |
2156 KB |
Output is correct |
30 |
Correct |
6 ms |
2156 KB |
Output is correct |
31 |
Correct |
6 ms |
2176 KB |
Output is correct |
32 |
Correct |
7 ms |
1260 KB |
Output is correct |
33 |
Correct |
344 ms |
37464 KB |
Output is correct |
34 |
Correct |
255 ms |
25252 KB |
Output is correct |
35 |
Correct |
365 ms |
35188 KB |
Output is correct |
36 |
Correct |
243 ms |
25188 KB |
Output is correct |
37 |
Correct |
181 ms |
24908 KB |
Output is correct |
38 |
Correct |
193 ms |
24960 KB |
Output is correct |
39 |
Correct |
214 ms |
74824 KB |
Output is correct |
40 |
Correct |
199 ms |
74696 KB |
Output is correct |
41 |
Correct |
169 ms |
74696 KB |
Output is correct |
42 |
Correct |
206 ms |
52936 KB |
Output is correct |
43 |
Correct |
204 ms |
52936 KB |
Output is correct |
44 |
Correct |
333 ms |
37008 KB |
Output is correct |
45 |
Correct |
226 ms |
25168 KB |
Output is correct |
46 |
Correct |
141 ms |
24784 KB |
Output is correct |
47 |
Correct |
283 ms |
53480 KB |
Output is correct |
48 |
Correct |
158 ms |
46544 KB |
Output is correct |
49 |
Correct |
135 ms |
46544 KB |
Output is correct |
50 |
Correct |
291 ms |
32476 KB |
Output is correct |
51 |
Correct |
118 ms |
21344 KB |
Output is correct |
52 |
Correct |
319 ms |
54500 KB |
Output is correct |
53 |
Correct |
154 ms |
47904 KB |
Output is correct |
54 |
Correct |
187 ms |
74696 KB |
Output is correct |
55 |
Correct |
279 ms |
55956 KB |
Output is correct |
56 |
Correct |
245 ms |
70412 KB |
Output is correct |
57 |
Correct |
228 ms |
76944 KB |
Output is correct |
58 |
Correct |
266 ms |
71504 KB |
Output is correct |
59 |
Correct |
266 ms |
71600 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
1 ms |
364 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
375 ms |
34796 KB |
Output is correct |
65 |
Correct |
282 ms |
23648 KB |
Output is correct |
66 |
Correct |
345 ms |
32816 KB |
Output is correct |
67 |
Correct |
288 ms |
27688 KB |
Output is correct |
68 |
Correct |
200 ms |
25456 KB |
Output is correct |
69 |
Correct |
201 ms |
17212 KB |
Output is correct |
70 |
Correct |
244 ms |
57216 KB |
Output is correct |
71 |
Correct |
222 ms |
46472 KB |
Output is correct |
72 |
Correct |
245 ms |
76424 KB |
Output is correct |
73 |
Correct |
247 ms |
76244 KB |
Output is correct |
74 |
Correct |
371 ms |
55404 KB |
Output is correct |
75 |
Correct |
224 ms |
48232 KB |
Output is correct |
76 |
Correct |
214 ms |
48272 KB |
Output is correct |
77 |
Correct |
321 ms |
56048 KB |
Output is correct |
78 |
Correct |
188 ms |
48864 KB |
Output is correct |
79 |
Correct |
316 ms |
56724 KB |
Output is correct |
80 |
Correct |
280 ms |
51156 KB |
Output is correct |
81 |
Correct |
216 ms |
50580 KB |
Output is correct |
82 |
Correct |
263 ms |
76252 KB |
Output is correct |
83 |
Correct |
163 ms |
15212 KB |
Output is correct |
84 |
Correct |
289 ms |
67640 KB |
Output is correct |
85 |
Correct |
330 ms |
67700 KB |
Output is correct |
86 |
Correct |
347 ms |
67728 KB |
Output is correct |
87 |
Correct |
373 ms |
67632 KB |
Output is correct |
88 |
Correct |
308 ms |
67852 KB |
Output is correct |