#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll INF=1e17;
map<ll,ll> nrm;
ll nr;
vector<ll> muchii[400005];
vector<ll> edges[400005];
ll to[400005];
ll n,par[400005],v[400005],cost[400005];
set<pll> setik[400005];
ll ans,retard;
ll aib[400005];
ll lsb(ll x)
{
return x&(-x);
}
void update(ll poz,ll val)
{
for(ll i=poz;i<=nr+1;i+=lsb(i))
aib[i]+=val;
}
ll suma(ll poz)
{
ll rez=0;
for(ll i=poz;i>=1;i-=lsb(i))
rez+=aib[i];
return rez;
}
bool use[400005];
void add(ll nod,ll poz,ll val)
{
auto it=setik[nod].lower_bound({poz,0});
if((*it).first==poz)
{
ll x=(*it).second+val;
setik[nod].erase(it);
setik[nod].insert({poz,x});
}
else
setik[nod].insert({poz,val});
}
void dfs(ll nod)
{
use[nod]=1;
if(muchii[nod].empty())
{
add(nod,v[nod]+1,cost[nod]);
return;
}
ll maxim=0;
ll who=0;
for(int i:muchii[nod])
{
dfs(i);
if(setik[i].size()>maxim)
{
maxim=setik[i].size();
who=i;
}
}
swap(setik[nod],setik[who]);
for(int i:muchii[nod])
if(i!=who)
{
for(auto j:setik[i])
add(nod,j.first,j.second);
setik[i].clear();
}
add(nod,1,cost[nod]);
ll dif=cost[nod];
while(!setik[nod].empty()&&dif>0)
{
auto it=prev(setik[nod].upper_bound({v[nod],INF}));
ll poz=(*it).first;
ll x=(*it).second;
ll val=min(dif,x);
x-=val;
dif-=val;
setik[nod].erase(it);
if(x!=0)
setik[nod].insert({poz,x});
}
add(nod,v[nod]+1,cost[nod]);
}
void initdfs(int nod,int cod)
{
use[nod]=1;
for(int i:edges[nod])
if(!use[i])
{
muchii[cod].push_back(i);
initdfs(i,i);
}
}
bool byv(int a,int b)
{
return v[a]<v[b];
}
void solve(ll start)
{
retard++;
vector<ll> nodes;
while(!use[start])
{
use[start]=1;
nodes.push_back(start);
start=to[start];
}
vector<int> ciclu;
ciclu.push_back(start);
while(!nodes.empty())
{
if(nodes.back()==start)
break;
ciclu.push_back(nodes.back());
nodes.pop_back();
}
for(int i:nodes)
use[i]=0;
for(int i:ciclu)
initdfs(i,retard);
dfs(retard);
for(auto i:setik[retard])
update(i.first,i.second);
ll s=0;
sort(ciclu.begin(),ciclu.end(),byv);
for(int i:ciclu)
s+=cost[i];
ll minim=suma(1)+s;
for(int i=0;i<ciclu.size();i++)
{
ll improve=0;
int j=i;
ll ind=v[ciclu[i]];
while(j<ciclu.size()&&v[ciclu[j]]==ind)
{
improve+=cost[ciclu[j]];
j++;
}
j--;
i=j;
ll val=suma(ind)+s-improve;
minim=min(minim,val);
}
for(auto i:setik[retard])
update(i.first,-i.second);
ans+=minim;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
vector<ll> vals;
for(int i=1;i<=n;i++)
{
cin>>to[i]>>v[i]>>cost[i];
vals.push_back(v[i]);
edges[i].push_back(to[i]);
edges[to[i]].push_back(i);
}
sort(vals.begin(),vals.end());
vals.erase(unique(vals.begin(),vals.end()),vals.end());
for(int i=0;i<vals.size();i++)
nrm[vals[i]]=i+1;
nr=vals.size();
for(int i=1;i<=n;i++)
v[i]=nrm[v[i]];
retard=n;
for(int i=1;i<=n;i++)
if(!use[i])
solve(i);
cout<<ans;
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'void dfs(ll)':
worst_reporter2.cpp:58:27: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
58 | if(setik[i].size()>maxim)
| ~~~~~~~~~~~~~~~^~~~~~
worst_reporter2.cpp: In function 'void solve(ll)':
worst_reporter2.cpp:133:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | for(int i=0;i<ciclu.size();i++)
| ~^~~~~~~~~~~~~
worst_reporter2.cpp:138:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | while(j<ciclu.size()&&v[ciclu[j]]==ind)
| ~^~~~~~~~~~~~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:167:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
167 | for(int i=0;i<vals.size();i++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
47708 KB |
Output is correct |
2 |
Correct |
10 ms |
47708 KB |
Output is correct |
3 |
Correct |
11 ms |
47708 KB |
Output is correct |
4 |
Correct |
10 ms |
47708 KB |
Output is correct |
5 |
Correct |
16 ms |
48476 KB |
Output is correct |
6 |
Correct |
14 ms |
47960 KB |
Output is correct |
7 |
Correct |
13 ms |
48000 KB |
Output is correct |
8 |
Correct |
18 ms |
48476 KB |
Output is correct |
9 |
Correct |
14 ms |
47964 KB |
Output is correct |
10 |
Correct |
15 ms |
47980 KB |
Output is correct |
11 |
Correct |
12 ms |
47960 KB |
Output is correct |
12 |
Correct |
15 ms |
48988 KB |
Output is correct |
13 |
Correct |
13 ms |
48732 KB |
Output is correct |
14 |
Correct |
17 ms |
48732 KB |
Output is correct |
15 |
Correct |
14 ms |
48220 KB |
Output is correct |
16 |
Correct |
17 ms |
48476 KB |
Output is correct |
17 |
Correct |
14 ms |
47964 KB |
Output is correct |
18 |
Correct |
11 ms |
47964 KB |
Output is correct |
19 |
Correct |
18 ms |
48728 KB |
Output is correct |
20 |
Correct |
13 ms |
48220 KB |
Output is correct |
21 |
Correct |
13 ms |
48220 KB |
Output is correct |
22 |
Correct |
14 ms |
48476 KB |
Output is correct |
23 |
Correct |
14 ms |
48220 KB |
Output is correct |
24 |
Correct |
15 ms |
48728 KB |
Output is correct |
25 |
Correct |
13 ms |
48560 KB |
Output is correct |
26 |
Correct |
13 ms |
48988 KB |
Output is correct |
27 |
Correct |
14 ms |
48732 KB |
Output is correct |
28 |
Correct |
15 ms |
48828 KB |
Output is correct |
29 |
Correct |
14 ms |
48976 KB |
Output is correct |
30 |
Correct |
15 ms |
48984 KB |
Output is correct |
31 |
Correct |
15 ms |
49212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
47708 KB |
Output is correct |
2 |
Correct |
10 ms |
47708 KB |
Output is correct |
3 |
Correct |
11 ms |
47708 KB |
Output is correct |
4 |
Correct |
10 ms |
47708 KB |
Output is correct |
5 |
Correct |
16 ms |
48476 KB |
Output is correct |
6 |
Correct |
14 ms |
47960 KB |
Output is correct |
7 |
Correct |
13 ms |
48000 KB |
Output is correct |
8 |
Correct |
18 ms |
48476 KB |
Output is correct |
9 |
Correct |
14 ms |
47964 KB |
Output is correct |
10 |
Correct |
15 ms |
47980 KB |
Output is correct |
11 |
Correct |
12 ms |
47960 KB |
Output is correct |
12 |
Correct |
15 ms |
48988 KB |
Output is correct |
13 |
Correct |
13 ms |
48732 KB |
Output is correct |
14 |
Correct |
17 ms |
48732 KB |
Output is correct |
15 |
Correct |
14 ms |
48220 KB |
Output is correct |
16 |
Correct |
17 ms |
48476 KB |
Output is correct |
17 |
Correct |
14 ms |
47964 KB |
Output is correct |
18 |
Correct |
11 ms |
47964 KB |
Output is correct |
19 |
Correct |
18 ms |
48728 KB |
Output is correct |
20 |
Correct |
13 ms |
48220 KB |
Output is correct |
21 |
Correct |
13 ms |
48220 KB |
Output is correct |
22 |
Correct |
14 ms |
48476 KB |
Output is correct |
23 |
Correct |
14 ms |
48220 KB |
Output is correct |
24 |
Correct |
15 ms |
48728 KB |
Output is correct |
25 |
Correct |
13 ms |
48560 KB |
Output is correct |
26 |
Correct |
13 ms |
48988 KB |
Output is correct |
27 |
Correct |
14 ms |
48732 KB |
Output is correct |
28 |
Correct |
15 ms |
48828 KB |
Output is correct |
29 |
Correct |
14 ms |
48976 KB |
Output is correct |
30 |
Correct |
15 ms |
48984 KB |
Output is correct |
31 |
Correct |
15 ms |
49212 KB |
Output is correct |
32 |
Correct |
16 ms |
48476 KB |
Output is correct |
33 |
Correct |
459 ms |
89420 KB |
Output is correct |
34 |
Correct |
238 ms |
63168 KB |
Output is correct |
35 |
Correct |
448 ms |
88204 KB |
Output is correct |
36 |
Correct |
269 ms |
63208 KB |
Output is correct |
37 |
Correct |
204 ms |
62660 KB |
Output is correct |
38 |
Correct |
142 ms |
62796 KB |
Output is correct |
39 |
Correct |
304 ms |
103100 KB |
Output is correct |
40 |
Correct |
198 ms |
89024 KB |
Output is correct |
41 |
Correct |
132 ms |
89024 KB |
Output is correct |
42 |
Correct |
301 ms |
90816 KB |
Output is correct |
43 |
Correct |
189 ms |
76736 KB |
Output is correct |
44 |
Correct |
440 ms |
88940 KB |
Output is correct |
45 |
Correct |
226 ms |
62964 KB |
Output is correct |
46 |
Correct |
97 ms |
62660 KB |
Output is correct |
47 |
Correct |
333 ms |
95932 KB |
Output is correct |
48 |
Correct |
175 ms |
74940 KB |
Output is correct |
49 |
Correct |
123 ms |
74944 KB |
Output is correct |
50 |
Correct |
279 ms |
87548 KB |
Output is correct |
51 |
Correct |
136 ms |
73360 KB |
Output is correct |
52 |
Correct |
368 ms |
96308 KB |
Output is correct |
53 |
Correct |
169 ms |
81344 KB |
Output is correct |
54 |
Correct |
162 ms |
101596 KB |
Output is correct |
55 |
Correct |
272 ms |
98552 KB |
Output is correct |
56 |
Correct |
250 ms |
105904 KB |
Output is correct |
57 |
Correct |
251 ms |
109600 KB |
Output is correct |
58 |
Correct |
285 ms |
109280 KB |
Output is correct |
59 |
Correct |
296 ms |
109248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
47708 KB |
Output is correct |
2 |
Correct |
10 ms |
47708 KB |
Output is correct |
3 |
Correct |
11 ms |
47708 KB |
Output is correct |
4 |
Correct |
10 ms |
47708 KB |
Output is correct |
5 |
Correct |
16 ms |
48476 KB |
Output is correct |
6 |
Correct |
14 ms |
47960 KB |
Output is correct |
7 |
Correct |
13 ms |
48000 KB |
Output is correct |
8 |
Correct |
18 ms |
48476 KB |
Output is correct |
9 |
Correct |
14 ms |
47964 KB |
Output is correct |
10 |
Correct |
15 ms |
47980 KB |
Output is correct |
11 |
Correct |
12 ms |
47960 KB |
Output is correct |
12 |
Correct |
15 ms |
48988 KB |
Output is correct |
13 |
Correct |
13 ms |
48732 KB |
Output is correct |
14 |
Correct |
17 ms |
48732 KB |
Output is correct |
15 |
Correct |
14 ms |
48220 KB |
Output is correct |
16 |
Correct |
17 ms |
48476 KB |
Output is correct |
17 |
Correct |
14 ms |
47964 KB |
Output is correct |
18 |
Correct |
11 ms |
47964 KB |
Output is correct |
19 |
Correct |
18 ms |
48728 KB |
Output is correct |
20 |
Correct |
13 ms |
48220 KB |
Output is correct |
21 |
Correct |
13 ms |
48220 KB |
Output is correct |
22 |
Correct |
14 ms |
48476 KB |
Output is correct |
23 |
Correct |
14 ms |
48220 KB |
Output is correct |
24 |
Correct |
15 ms |
48728 KB |
Output is correct |
25 |
Correct |
13 ms |
48560 KB |
Output is correct |
26 |
Correct |
13 ms |
48988 KB |
Output is correct |
27 |
Correct |
14 ms |
48732 KB |
Output is correct |
28 |
Correct |
15 ms |
48828 KB |
Output is correct |
29 |
Correct |
14 ms |
48976 KB |
Output is correct |
30 |
Correct |
15 ms |
48984 KB |
Output is correct |
31 |
Correct |
15 ms |
49212 KB |
Output is correct |
32 |
Correct |
16 ms |
48476 KB |
Output is correct |
33 |
Correct |
459 ms |
89420 KB |
Output is correct |
34 |
Correct |
238 ms |
63168 KB |
Output is correct |
35 |
Correct |
448 ms |
88204 KB |
Output is correct |
36 |
Correct |
269 ms |
63208 KB |
Output is correct |
37 |
Correct |
204 ms |
62660 KB |
Output is correct |
38 |
Correct |
142 ms |
62796 KB |
Output is correct |
39 |
Correct |
304 ms |
103100 KB |
Output is correct |
40 |
Correct |
198 ms |
89024 KB |
Output is correct |
41 |
Correct |
132 ms |
89024 KB |
Output is correct |
42 |
Correct |
301 ms |
90816 KB |
Output is correct |
43 |
Correct |
189 ms |
76736 KB |
Output is correct |
44 |
Correct |
440 ms |
88940 KB |
Output is correct |
45 |
Correct |
226 ms |
62964 KB |
Output is correct |
46 |
Correct |
97 ms |
62660 KB |
Output is correct |
47 |
Correct |
333 ms |
95932 KB |
Output is correct |
48 |
Correct |
175 ms |
74940 KB |
Output is correct |
49 |
Correct |
123 ms |
74944 KB |
Output is correct |
50 |
Correct |
279 ms |
87548 KB |
Output is correct |
51 |
Correct |
136 ms |
73360 KB |
Output is correct |
52 |
Correct |
368 ms |
96308 KB |
Output is correct |
53 |
Correct |
169 ms |
81344 KB |
Output is correct |
54 |
Correct |
162 ms |
101596 KB |
Output is correct |
55 |
Correct |
272 ms |
98552 KB |
Output is correct |
56 |
Correct |
250 ms |
105904 KB |
Output is correct |
57 |
Correct |
251 ms |
109600 KB |
Output is correct |
58 |
Correct |
285 ms |
109280 KB |
Output is correct |
59 |
Correct |
296 ms |
109248 KB |
Output is correct |
60 |
Correct |
10 ms |
47704 KB |
Output is correct |
61 |
Correct |
10 ms |
47708 KB |
Output is correct |
62 |
Correct |
10 ms |
47708 KB |
Output is correct |
63 |
Correct |
9 ms |
47708 KB |
Output is correct |
64 |
Correct |
455 ms |
90936 KB |
Output is correct |
65 |
Correct |
307 ms |
68552 KB |
Output is correct |
66 |
Correct |
442 ms |
90608 KB |
Output is correct |
67 |
Correct |
260 ms |
69564 KB |
Output is correct |
68 |
Correct |
199 ms |
68208 KB |
Output is correct |
69 |
Correct |
207 ms |
68188 KB |
Output is correct |
70 |
Correct |
250 ms |
78472 KB |
Output is correct |
71 |
Correct |
139 ms |
64704 KB |
Output is correct |
72 |
Correct |
274 ms |
78916 KB |
Output is correct |
73 |
Correct |
134 ms |
66320 KB |
Output is correct |
74 |
Correct |
346 ms |
87712 KB |
Output is correct |
75 |
Correct |
173 ms |
73664 KB |
Output is correct |
76 |
Correct |
162 ms |
73576 KB |
Output is correct |
77 |
Correct |
298 ms |
86112 KB |
Output is correct |
78 |
Correct |
147 ms |
72120 KB |
Output is correct |
79 |
Correct |
377 ms |
86976 KB |
Output is correct |
80 |
Correct |
196 ms |
67504 KB |
Output is correct |
81 |
Correct |
182 ms |
66588 KB |
Output is correct |
82 |
Correct |
253 ms |
79124 KB |
Output is correct |
83 |
Correct |
228 ms |
89948 KB |
Output is correct |
84 |
Correct |
344 ms |
92460 KB |
Output is correct |
85 |
Correct |
323 ms |
92864 KB |
Output is correct |
86 |
Correct |
376 ms |
89048 KB |
Output is correct |
87 |
Correct |
356 ms |
92824 KB |
Output is correct |
88 |
Correct |
321 ms |
92824 KB |
Output is correct |