#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
vector<int> adj[200000];
map<int,long long> D[200000];
long long O[200000];
int P[200000], H[200000], C[200000], R[200000], cnt[200000];
void dfs(int c)
{
long long v=C[c], m;
for(auto n: adj[c]) {
dfs(n);
if(D[R[c]].size()<D[R[n]].size()) swap(R[c],R[n]);
for(auto[a,b]: D[R[n]]) D[R[c]][a]+=b;
O[c]+=O[n];
}
O[c]+=C[c];
D[R[c]][H[c]]+=C[c];
if(cnt[c]) {
D[R[c]][H[c]+1]-=C[c];
return;
}
while(v) {
auto it=D[R[c]].upper_bound(H[c]);
if(it==D[R[c]].end()) break;
m=min(v,it->second);
it->second-=m;
v-=m;
if(it->second==0) D[R[c]].erase(it);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int N;
long long ans=0;
queue<int> Q;
cin>>N;
for(int i=0;i<N;i++) {
cin>>P[i]>>H[i]>>C[i];
H[R[i]=i]=1000000000-H[i];
cnt[--P[i]]++;
}
for(int i=0;i<N;i++) if(cnt[i]==0) Q.push(i);
while(!Q.empty()) {
int c=Q.front();
Q.pop();
if(--cnt[P[c]]==0) Q.push(P[c]);
}
for(int i=0;i<N;i++) if(cnt[i]==0) adj[P[i]].push_back(i);
for(int i=0;i<N;i++) if(cnt[i]) dfs(i);
for(int i=0;i<N;i++) if(cnt[i]) {
long long mn;
cnt[i]--;
for(int p=P[i];p!=i;p=P[p]) {
cnt[p]--;
if(D[R[p]].size()>D[R[i]].size()) swap(R[p],R[i]);
for(auto[a,b]: D[R[p]]) D[R[i]][a]+=b;
O[i]+=O[p];
}
mn=O[i];
for(auto[a,b]: D[R[i]]) mn=min(mn,O[i]-=b);
ans+=mn;
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
17 ms |
15664 KB |
Output is correct |
6 |
Correct |
14 ms |
15108 KB |
Output is correct |
7 |
Correct |
13 ms |
14924 KB |
Output is correct |
8 |
Correct |
16 ms |
15660 KB |
Output is correct |
9 |
Correct |
13 ms |
15180 KB |
Output is correct |
10 |
Correct |
12 ms |
14880 KB |
Output is correct |
11 |
Correct |
12 ms |
14796 KB |
Output is correct |
12 |
Correct |
13 ms |
15492 KB |
Output is correct |
13 |
Correct |
12 ms |
15440 KB |
Output is correct |
14 |
Correct |
13 ms |
15112 KB |
Output is correct |
15 |
Correct |
12 ms |
15084 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15356 KB |
Output is correct |
18 |
Correct |
12 ms |
14760 KB |
Output is correct |
19 |
Correct |
14 ms |
15360 KB |
Output is correct |
20 |
Correct |
12 ms |
15180 KB |
Output is correct |
21 |
Correct |
12 ms |
15128 KB |
Output is correct |
22 |
Correct |
13 ms |
15144 KB |
Output is correct |
23 |
Correct |
12 ms |
14796 KB |
Output is correct |
24 |
Correct |
14 ms |
15408 KB |
Output is correct |
25 |
Correct |
12 ms |
15180 KB |
Output is correct |
26 |
Correct |
12 ms |
15436 KB |
Output is correct |
27 |
Correct |
13 ms |
15356 KB |
Output is correct |
28 |
Correct |
13 ms |
15392 KB |
Output is correct |
29 |
Correct |
13 ms |
15564 KB |
Output is correct |
30 |
Correct |
14 ms |
15560 KB |
Output is correct |
31 |
Correct |
14 ms |
15564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
17 ms |
15664 KB |
Output is correct |
6 |
Correct |
14 ms |
15108 KB |
Output is correct |
7 |
Correct |
13 ms |
14924 KB |
Output is correct |
8 |
Correct |
16 ms |
15660 KB |
Output is correct |
9 |
Correct |
13 ms |
15180 KB |
Output is correct |
10 |
Correct |
12 ms |
14880 KB |
Output is correct |
11 |
Correct |
12 ms |
14796 KB |
Output is correct |
12 |
Correct |
13 ms |
15492 KB |
Output is correct |
13 |
Correct |
12 ms |
15440 KB |
Output is correct |
14 |
Correct |
13 ms |
15112 KB |
Output is correct |
15 |
Correct |
12 ms |
15084 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15356 KB |
Output is correct |
18 |
Correct |
12 ms |
14760 KB |
Output is correct |
19 |
Correct |
14 ms |
15360 KB |
Output is correct |
20 |
Correct |
12 ms |
15180 KB |
Output is correct |
21 |
Correct |
12 ms |
15128 KB |
Output is correct |
22 |
Correct |
13 ms |
15144 KB |
Output is correct |
23 |
Correct |
12 ms |
14796 KB |
Output is correct |
24 |
Correct |
14 ms |
15408 KB |
Output is correct |
25 |
Correct |
12 ms |
15180 KB |
Output is correct |
26 |
Correct |
12 ms |
15436 KB |
Output is correct |
27 |
Correct |
13 ms |
15356 KB |
Output is correct |
28 |
Correct |
13 ms |
15392 KB |
Output is correct |
29 |
Correct |
13 ms |
15564 KB |
Output is correct |
30 |
Correct |
14 ms |
15560 KB |
Output is correct |
31 |
Correct |
14 ms |
15564 KB |
Output is correct |
32 |
Correct |
15 ms |
15564 KB |
Output is correct |
33 |
Correct |
402 ms |
77540 KB |
Output is correct |
34 |
Correct |
304 ms |
57680 KB |
Output is correct |
35 |
Correct |
389 ms |
79564 KB |
Output is correct |
36 |
Correct |
299 ms |
57668 KB |
Output is correct |
37 |
Correct |
214 ms |
38796 KB |
Output is correct |
38 |
Correct |
194 ms |
34244 KB |
Output is correct |
39 |
Correct |
180 ms |
62596 KB |
Output is correct |
40 |
Correct |
165 ms |
62532 KB |
Output is correct |
41 |
Correct |
149 ms |
62572 KB |
Output is correct |
42 |
Correct |
188 ms |
46988 KB |
Output is correct |
43 |
Correct |
164 ms |
46900 KB |
Output is correct |
44 |
Correct |
347 ms |
107060 KB |
Output is correct |
45 |
Correct |
241 ms |
71620 KB |
Output is correct |
46 |
Correct |
113 ms |
33872 KB |
Output is correct |
47 |
Correct |
281 ms |
56496 KB |
Output is correct |
48 |
Correct |
144 ms |
49688 KB |
Output is correct |
49 |
Correct |
119 ms |
49536 KB |
Output is correct |
50 |
Correct |
293 ms |
49148 KB |
Output is correct |
51 |
Correct |
116 ms |
36668 KB |
Output is correct |
52 |
Correct |
307 ms |
57160 KB |
Output is correct |
53 |
Correct |
136 ms |
50108 KB |
Output is correct |
54 |
Correct |
135 ms |
62780 KB |
Output is correct |
55 |
Correct |
241 ms |
58144 KB |
Output is correct |
56 |
Correct |
225 ms |
64960 KB |
Output is correct |
57 |
Correct |
185 ms |
68456 KB |
Output is correct |
58 |
Correct |
211 ms |
65152 KB |
Output is correct |
59 |
Correct |
244 ms |
64968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
17 ms |
15664 KB |
Output is correct |
6 |
Correct |
14 ms |
15108 KB |
Output is correct |
7 |
Correct |
13 ms |
14924 KB |
Output is correct |
8 |
Correct |
16 ms |
15660 KB |
Output is correct |
9 |
Correct |
13 ms |
15180 KB |
Output is correct |
10 |
Correct |
12 ms |
14880 KB |
Output is correct |
11 |
Correct |
12 ms |
14796 KB |
Output is correct |
12 |
Correct |
13 ms |
15492 KB |
Output is correct |
13 |
Correct |
12 ms |
15440 KB |
Output is correct |
14 |
Correct |
13 ms |
15112 KB |
Output is correct |
15 |
Correct |
12 ms |
15084 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15356 KB |
Output is correct |
18 |
Correct |
12 ms |
14760 KB |
Output is correct |
19 |
Correct |
14 ms |
15360 KB |
Output is correct |
20 |
Correct |
12 ms |
15180 KB |
Output is correct |
21 |
Correct |
12 ms |
15128 KB |
Output is correct |
22 |
Correct |
13 ms |
15144 KB |
Output is correct |
23 |
Correct |
12 ms |
14796 KB |
Output is correct |
24 |
Correct |
14 ms |
15408 KB |
Output is correct |
25 |
Correct |
12 ms |
15180 KB |
Output is correct |
26 |
Correct |
12 ms |
15436 KB |
Output is correct |
27 |
Correct |
13 ms |
15356 KB |
Output is correct |
28 |
Correct |
13 ms |
15392 KB |
Output is correct |
29 |
Correct |
13 ms |
15564 KB |
Output is correct |
30 |
Correct |
14 ms |
15560 KB |
Output is correct |
31 |
Correct |
14 ms |
15564 KB |
Output is correct |
32 |
Correct |
15 ms |
15564 KB |
Output is correct |
33 |
Correct |
402 ms |
77540 KB |
Output is correct |
34 |
Correct |
304 ms |
57680 KB |
Output is correct |
35 |
Correct |
389 ms |
79564 KB |
Output is correct |
36 |
Correct |
299 ms |
57668 KB |
Output is correct |
37 |
Correct |
214 ms |
38796 KB |
Output is correct |
38 |
Correct |
194 ms |
34244 KB |
Output is correct |
39 |
Correct |
180 ms |
62596 KB |
Output is correct |
40 |
Correct |
165 ms |
62532 KB |
Output is correct |
41 |
Correct |
149 ms |
62572 KB |
Output is correct |
42 |
Correct |
188 ms |
46988 KB |
Output is correct |
43 |
Correct |
164 ms |
46900 KB |
Output is correct |
44 |
Correct |
347 ms |
107060 KB |
Output is correct |
45 |
Correct |
241 ms |
71620 KB |
Output is correct |
46 |
Correct |
113 ms |
33872 KB |
Output is correct |
47 |
Correct |
281 ms |
56496 KB |
Output is correct |
48 |
Correct |
144 ms |
49688 KB |
Output is correct |
49 |
Correct |
119 ms |
49536 KB |
Output is correct |
50 |
Correct |
293 ms |
49148 KB |
Output is correct |
51 |
Correct |
116 ms |
36668 KB |
Output is correct |
52 |
Correct |
307 ms |
57160 KB |
Output is correct |
53 |
Correct |
136 ms |
50108 KB |
Output is correct |
54 |
Correct |
135 ms |
62780 KB |
Output is correct |
55 |
Correct |
241 ms |
58144 KB |
Output is correct |
56 |
Correct |
225 ms |
64960 KB |
Output is correct |
57 |
Correct |
185 ms |
68456 KB |
Output is correct |
58 |
Correct |
211 ms |
65152 KB |
Output is correct |
59 |
Correct |
244 ms |
64968 KB |
Output is correct |
60 |
Correct |
8 ms |
14416 KB |
Output is correct |
61 |
Correct |
8 ms |
14412 KB |
Output is correct |
62 |
Correct |
8 ms |
14412 KB |
Output is correct |
63 |
Correct |
9 ms |
14412 KB |
Output is correct |
64 |
Correct |
369 ms |
67524 KB |
Output is correct |
65 |
Correct |
294 ms |
50916 KB |
Output is correct |
66 |
Correct |
364 ms |
67172 KB |
Output is correct |
67 |
Correct |
284 ms |
50796 KB |
Output is correct |
68 |
Correct |
223 ms |
37060 KB |
Output is correct |
69 |
Correct |
203 ms |
33700 KB |
Output is correct |
70 |
Correct |
427 ms |
75076 KB |
Output is correct |
71 |
Correct |
226 ms |
50372 KB |
Output is correct |
72 |
Correct |
451 ms |
75292 KB |
Output is correct |
73 |
Correct |
226 ms |
50280 KB |
Output is correct |
74 |
Correct |
429 ms |
65724 KB |
Output is correct |
75 |
Correct |
256 ms |
47048 KB |
Output is correct |
76 |
Correct |
222 ms |
45692 KB |
Output is correct |
77 |
Correct |
378 ms |
66712 KB |
Output is correct |
78 |
Correct |
183 ms |
44396 KB |
Output is correct |
79 |
Correct |
427 ms |
82248 KB |
Output is correct |
80 |
Correct |
288 ms |
55352 KB |
Output is correct |
81 |
Correct |
210 ms |
44612 KB |
Output is correct |
82 |
Correct |
300 ms |
75268 KB |
Output is correct |
83 |
Correct |
120 ms |
49924 KB |
Output is correct |
84 |
Correct |
358 ms |
74616 KB |
Output is correct |
85 |
Correct |
362 ms |
74560 KB |
Output is correct |
86 |
Correct |
313 ms |
62276 KB |
Output is correct |
87 |
Correct |
351 ms |
74600 KB |
Output is correct |
88 |
Correct |
351 ms |
74632 KB |
Output is correct |