#include <bits/stdc++.h>
using namespace std;
#define int long long
int h[200005];
int c[200005];
vector<int> adjl[200005];
int in_cycle[200005];
///pos, increase
set<pair<int,int> > func(int node, bool fin = true){
set<pair<int,int> > res1;
for (auto x : adjl[node]){
if (in_cycle[x]) continue;
auto res = func(x);
if (res.size()>res1.size()) swap(res,res1);
for (auto y : res){
auto it = res1.lower_bound({y.first,-1});
if (it!=res1.end() && (*it).first==y.first){
int t = (*it).second;
res1.erase(it);
res1.insert({y.first,y.second+t});
}
else{
res1.insert({y.first,y.second});
}
}
}
if (!fin) return res1;
pair<int,int> to_insert = {h[node]+1,c[node]};
int cursum = 0;
auto it = res1.lower_bound({h[node]+1,-1});
if (it!=res1.end() && (*it).first==to_insert.first){
int t = (*it).second;
res1.erase(it);
res1.insert({to_insert.first,to_insert.second+t});
}
else{
res1.insert(to_insert);
}
it = res1.lower_bound(to_insert);
bool done = false;
while (it!=res1.begin()){
it--;
cursum += (*it).second;
int curpos = (*it).first;
it = res1.erase(it);
if (cursum >= c[node]){
int rem = cursum-c[node];
if (rem!=0){
res1.insert({curpos,rem});
}
done = true;
break;
}
}
if (!done){
if (cursum!=0){
if ((!res1.empty()) && (*res1.begin()).first==0){
cursum += (*res1.begin()).second;
res1.erase(res1.begin());
res1.insert({0,cursum});
}
else
res1.insert({0,cursum});
}
}
else{
if ((!res1.empty()) && (*res1.begin()).first==0){
int t = (*res1.begin()).second;
res1.erase(res1.begin());
res1.insert({0,c[node]+t});
}
else
res1.insert({0,c[node]});
}
/*printf("node %lld, ret ",node);
for (auto x : res1){
printf("(%lld,%lld) ",x);
}
printf("\n");*/
return res1;
}
int p[200005];
int vis[200005];
main(){
int n;
scanf("%lld",&n);
for (int x = 1; x<=n; x++){
int t;
scanf("%lld%lld%lld",&t,&h[x],&c[x]);
p[x] = t;
adjl[t].push_back(x);
}
vector<pair<int,vector<int> > > roots;
for (int x = 1; x<=n; x++){
if (vis[x]) continue;
int c1 = x, c2 = x;
c1 = p[c1]; c2 = p[p[c2]];
bool found = false;
while (c1!=c2){
if (vis[c1]) {
found = true;
break;
}
vis[c1] = true;
c1 = p[c1]; c2 = p[p[c2]];
}
if (found||vis[c1]) continue;
vis[c1] = true;
in_cycle[c1] = true;
vector<int> curcycle;
c2 = p[c2];
while (c2!=c1){
curcycle.push_back(c2);
vis[c2] = true;
in_cycle[c2] = true;
c2 = p[c2];
}
for (auto x : curcycle){
for (auto y : adjl[x]){
if (!in_cycle[y]){
adjl[c1].push_back(y);
}
}
}
curcycle.push_back(c1);
/* printf("cycle found: ");
for (auto x : curcycle) printf("%lld ",x);
printf("\n");*/
roots.push_back({c1,curcycle});
}
int ans = 0;
for (auto x : roots){
int root = x.first;
int curans = 0;
vector<pair<int,int> >stuff;
for (auto y : x.second){
stuff.push_back({h[y],c[y]});
curans += c[y];
}
auto res = func(root,false);
int cur = 0;
stuff.push_back({0,0});
sort(stuff.begin(),stuff.end());
vector<pair<int,int> > newstuff;
for (int x = 0; x<stuff.size(); x++){
if (x!=0 && stuff[x].first==newstuff.back().first){
newstuff.back().second+=stuff[x].second;
}
else newstuff.push_back(stuff[x]);
}
swap(newstuff,stuff);
auto it = res.begin();
int best = 999999999999999999LL;
for (auto val : stuff){
while (it!=res.end() && (*it).first<=val.first){
cur += (*it).second;
it++;
}
best = min(best,cur+curans-val.second);
// printf("try values %lld, %lld %lld\n",cur,val);
}
ans += best;
}
printf("%lld",ans);
}
Compilation message
worst_reporter2.cpp:88:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
88 | main(){
| ^~~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:149:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for (int x = 0; x<stuff.size(); x++){
| ~^~~~~~~~~~~~~
worst_reporter2.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
worst_reporter2.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | scanf("%lld%lld%lld",&t,&h[x],&c[x]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
9 ms |
5452 KB |
Output is correct |
6 |
Correct |
7 ms |
5284 KB |
Output is correct |
7 |
Correct |
7 ms |
5176 KB |
Output is correct |
8 |
Correct |
10 ms |
5452 KB |
Output is correct |
9 |
Correct |
8 ms |
5288 KB |
Output is correct |
10 |
Correct |
7 ms |
5180 KB |
Output is correct |
11 |
Correct |
6 ms |
5196 KB |
Output is correct |
12 |
Correct |
8 ms |
6348 KB |
Output is correct |
13 |
Correct |
7 ms |
6348 KB |
Output is correct |
14 |
Correct |
8 ms |
5836 KB |
Output is correct |
15 |
Correct |
8 ms |
5836 KB |
Output is correct |
16 |
Correct |
10 ms |
5492 KB |
Output is correct |
17 |
Correct |
8 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5152 KB |
Output is correct |
19 |
Correct |
8 ms |
5836 KB |
Output is correct |
20 |
Correct |
7 ms |
5708 KB |
Output is correct |
21 |
Correct |
6 ms |
5708 KB |
Output is correct |
22 |
Correct |
6 ms |
5452 KB |
Output is correct |
23 |
Correct |
6 ms |
5196 KB |
Output is correct |
24 |
Correct |
8 ms |
5836 KB |
Output is correct |
25 |
Correct |
7 ms |
5704 KB |
Output is correct |
26 |
Correct |
6 ms |
6348 KB |
Output is correct |
27 |
Correct |
8 ms |
5820 KB |
Output is correct |
28 |
Correct |
8 ms |
6080 KB |
Output is correct |
29 |
Correct |
8 ms |
6348 KB |
Output is correct |
30 |
Correct |
9 ms |
6212 KB |
Output is correct |
31 |
Correct |
8 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
9 ms |
5452 KB |
Output is correct |
6 |
Correct |
7 ms |
5284 KB |
Output is correct |
7 |
Correct |
7 ms |
5176 KB |
Output is correct |
8 |
Correct |
10 ms |
5452 KB |
Output is correct |
9 |
Correct |
8 ms |
5288 KB |
Output is correct |
10 |
Correct |
7 ms |
5180 KB |
Output is correct |
11 |
Correct |
6 ms |
5196 KB |
Output is correct |
12 |
Correct |
8 ms |
6348 KB |
Output is correct |
13 |
Correct |
7 ms |
6348 KB |
Output is correct |
14 |
Correct |
8 ms |
5836 KB |
Output is correct |
15 |
Correct |
8 ms |
5836 KB |
Output is correct |
16 |
Correct |
10 ms |
5492 KB |
Output is correct |
17 |
Correct |
8 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5152 KB |
Output is correct |
19 |
Correct |
8 ms |
5836 KB |
Output is correct |
20 |
Correct |
7 ms |
5708 KB |
Output is correct |
21 |
Correct |
6 ms |
5708 KB |
Output is correct |
22 |
Correct |
6 ms |
5452 KB |
Output is correct |
23 |
Correct |
6 ms |
5196 KB |
Output is correct |
24 |
Correct |
8 ms |
5836 KB |
Output is correct |
25 |
Correct |
7 ms |
5704 KB |
Output is correct |
26 |
Correct |
6 ms |
6348 KB |
Output is correct |
27 |
Correct |
8 ms |
5820 KB |
Output is correct |
28 |
Correct |
8 ms |
6080 KB |
Output is correct |
29 |
Correct |
8 ms |
6348 KB |
Output is correct |
30 |
Correct |
9 ms |
6212 KB |
Output is correct |
31 |
Correct |
8 ms |
6220 KB |
Output is correct |
32 |
Correct |
9 ms |
5520 KB |
Output is correct |
33 |
Correct |
390 ms |
27408 KB |
Output is correct |
34 |
Correct |
277 ms |
15444 KB |
Output is correct |
35 |
Correct |
373 ms |
25400 KB |
Output is correct |
36 |
Correct |
270 ms |
15348 KB |
Output is correct |
37 |
Correct |
181 ms |
14884 KB |
Output is correct |
38 |
Correct |
156 ms |
14916 KB |
Output is correct |
39 |
Correct |
236 ms |
58328 KB |
Output is correct |
40 |
Correct |
210 ms |
58180 KB |
Output is correct |
41 |
Correct |
154 ms |
58164 KB |
Output is correct |
42 |
Correct |
211 ms |
38056 KB |
Output is correct |
43 |
Correct |
216 ms |
37980 KB |
Output is correct |
44 |
Correct |
376 ms |
25956 KB |
Output is correct |
45 |
Correct |
246 ms |
14120 KB |
Output is correct |
46 |
Correct |
109 ms |
13692 KB |
Output is correct |
47 |
Correct |
267 ms |
40884 KB |
Output is correct |
48 |
Correct |
180 ms |
34072 KB |
Output is correct |
49 |
Correct |
135 ms |
34104 KB |
Output is correct |
50 |
Correct |
250 ms |
23800 KB |
Output is correct |
51 |
Correct |
120 ms |
11312 KB |
Output is correct |
52 |
Correct |
285 ms |
41992 KB |
Output is correct |
53 |
Correct |
175 ms |
34880 KB |
Output is correct |
54 |
Correct |
143 ms |
58180 KB |
Output is correct |
55 |
Correct |
234 ms |
44160 KB |
Output is correct |
56 |
Correct |
224 ms |
56056 KB |
Output is correct |
57 |
Correct |
231 ms |
61656 KB |
Output is correct |
58 |
Correct |
277 ms |
56628 KB |
Output is correct |
59 |
Correct |
294 ms |
56620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
9 ms |
5452 KB |
Output is correct |
6 |
Correct |
7 ms |
5284 KB |
Output is correct |
7 |
Correct |
7 ms |
5176 KB |
Output is correct |
8 |
Correct |
10 ms |
5452 KB |
Output is correct |
9 |
Correct |
8 ms |
5288 KB |
Output is correct |
10 |
Correct |
7 ms |
5180 KB |
Output is correct |
11 |
Correct |
6 ms |
5196 KB |
Output is correct |
12 |
Correct |
8 ms |
6348 KB |
Output is correct |
13 |
Correct |
7 ms |
6348 KB |
Output is correct |
14 |
Correct |
8 ms |
5836 KB |
Output is correct |
15 |
Correct |
8 ms |
5836 KB |
Output is correct |
16 |
Correct |
10 ms |
5492 KB |
Output is correct |
17 |
Correct |
8 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5152 KB |
Output is correct |
19 |
Correct |
8 ms |
5836 KB |
Output is correct |
20 |
Correct |
7 ms |
5708 KB |
Output is correct |
21 |
Correct |
6 ms |
5708 KB |
Output is correct |
22 |
Correct |
6 ms |
5452 KB |
Output is correct |
23 |
Correct |
6 ms |
5196 KB |
Output is correct |
24 |
Correct |
8 ms |
5836 KB |
Output is correct |
25 |
Correct |
7 ms |
5704 KB |
Output is correct |
26 |
Correct |
6 ms |
6348 KB |
Output is correct |
27 |
Correct |
8 ms |
5820 KB |
Output is correct |
28 |
Correct |
8 ms |
6080 KB |
Output is correct |
29 |
Correct |
8 ms |
6348 KB |
Output is correct |
30 |
Correct |
9 ms |
6212 KB |
Output is correct |
31 |
Correct |
8 ms |
6220 KB |
Output is correct |
32 |
Correct |
9 ms |
5520 KB |
Output is correct |
33 |
Correct |
390 ms |
27408 KB |
Output is correct |
34 |
Correct |
277 ms |
15444 KB |
Output is correct |
35 |
Correct |
373 ms |
25400 KB |
Output is correct |
36 |
Correct |
270 ms |
15348 KB |
Output is correct |
37 |
Correct |
181 ms |
14884 KB |
Output is correct |
38 |
Correct |
156 ms |
14916 KB |
Output is correct |
39 |
Correct |
236 ms |
58328 KB |
Output is correct |
40 |
Correct |
210 ms |
58180 KB |
Output is correct |
41 |
Correct |
154 ms |
58164 KB |
Output is correct |
42 |
Correct |
211 ms |
38056 KB |
Output is correct |
43 |
Correct |
216 ms |
37980 KB |
Output is correct |
44 |
Correct |
376 ms |
25956 KB |
Output is correct |
45 |
Correct |
246 ms |
14120 KB |
Output is correct |
46 |
Correct |
109 ms |
13692 KB |
Output is correct |
47 |
Correct |
267 ms |
40884 KB |
Output is correct |
48 |
Correct |
180 ms |
34072 KB |
Output is correct |
49 |
Correct |
135 ms |
34104 KB |
Output is correct |
50 |
Correct |
250 ms |
23800 KB |
Output is correct |
51 |
Correct |
120 ms |
11312 KB |
Output is correct |
52 |
Correct |
285 ms |
41992 KB |
Output is correct |
53 |
Correct |
175 ms |
34880 KB |
Output is correct |
54 |
Correct |
143 ms |
58180 KB |
Output is correct |
55 |
Correct |
234 ms |
44160 KB |
Output is correct |
56 |
Correct |
224 ms |
56056 KB |
Output is correct |
57 |
Correct |
231 ms |
61656 KB |
Output is correct |
58 |
Correct |
277 ms |
56628 KB |
Output is correct |
59 |
Correct |
294 ms |
56620 KB |
Output is correct |
60 |
Correct |
3 ms |
4940 KB |
Output is correct |
61 |
Correct |
3 ms |
4940 KB |
Output is correct |
62 |
Correct |
3 ms |
4940 KB |
Output is correct |
63 |
Correct |
4 ms |
4944 KB |
Output is correct |
64 |
Correct |
353 ms |
29932 KB |
Output is correct |
65 |
Correct |
297 ms |
22452 KB |
Output is correct |
66 |
Correct |
341 ms |
30364 KB |
Output is correct |
67 |
Correct |
289 ms |
22492 KB |
Output is correct |
68 |
Correct |
204 ms |
22052 KB |
Output is correct |
69 |
Correct |
175 ms |
22400 KB |
Output is correct |
70 |
Correct |
179 ms |
35140 KB |
Output is correct |
71 |
Correct |
192 ms |
29656 KB |
Output is correct |
72 |
Correct |
177 ms |
36536 KB |
Output is correct |
73 |
Correct |
184 ms |
33432 KB |
Output is correct |
74 |
Correct |
259 ms |
34232 KB |
Output is correct |
75 |
Correct |
195 ms |
26496 KB |
Output is correct |
76 |
Correct |
174 ms |
26500 KB |
Output is correct |
77 |
Correct |
208 ms |
35176 KB |
Output is correct |
78 |
Correct |
169 ms |
27232 KB |
Output is correct |
79 |
Correct |
305 ms |
35328 KB |
Output is correct |
80 |
Correct |
241 ms |
27556 KB |
Output is correct |
81 |
Correct |
206 ms |
27600 KB |
Output is correct |
82 |
Correct |
178 ms |
36800 KB |
Output is correct |
83 |
Correct |
152 ms |
36764 KB |
Output is correct |
84 |
Correct |
294 ms |
44696 KB |
Output is correct |
85 |
Correct |
292 ms |
44712 KB |
Output is correct |
86 |
Correct |
278 ms |
43444 KB |
Output is correct |
87 |
Correct |
293 ms |
44772 KB |
Output is correct |
88 |
Correct |
288 ms |
44784 KB |
Output is correct |