#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define pp pair<ll,int>
#define ub(x,i) upper_bound(all(x),i)-x.begin()
using namespace std;
const int mxn=2e5+5;
vector<int> g[mxn];
int a[mxn],h[mxn],c[mxn];
map<int,ll>dp[mxn];
map<int,ll>res;
map<int,ll>cc;
int vis[mxn]{0};
bool iscyc[mxn]{0};
vector<int>cyc;
void dfs1(int u){
vis[u]=1;
if(vis[a[u]]==0)dfs1(a[u]);
else if(vis[a[u]]==1){
int x=a[u];
while(x!=u){
iscyc[x]=1;cyc.pb(x);x=a[x];
}cyc.pb(u);iscyc[u]=1;
}vis[u]=2;
}
void dfs2(int u,int p){
for(auto v:g[u]){
if(v==p||iscyc[v])continue;
dfs2(v,u);
if(dp[v].size()>dp[u].size())swap(dp[u],dp[v]);
for(auto it:dp[v])dp[u][it.f]+=it.s;
}dp[u][h[u]]+=c[u];
auto it = dp[u].upper_bound(h[u]);
int cur=c[u];
for(;it!=dp[u].end();){
if(cur>=it->s){cur-=it->s;auto ij=it;it++;dp[u].erase(ij);}
else {it->s-=cur;break;}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;ll rs=0;
for(int i=1;i<=n;i++){
cin>>a[i]>>h[i]>>c[i];h[i]=-h[i];
g[a[i]].pb(i);rs+=c[i];
}ll rs2=0;
for(int i=1;i<=n;i++){
if(vis[i])continue;
dfs1(i);
for(auto it:cyc){
for(auto v:g[it]){
if(!iscyc[v])dfs2(v,it);
for(auto ij:dp[v])res[ij.f]+=ij.s;
}cc[h[it]]+=c[it];
}
vector<pair<int,ll>>vec;
for(auto it:res)vec.pb(it);
sort(all(vec));for(int i=1;i<vec.size();i++)vec[i].s+=vec[i-1].s;
ll rs3=0;
for(auto it:cyc){
if(vec.empty()){
rs3=max(cc[h[it]],rs3);continue;
}
if(h[it]<vec[0].f){
rs3=max(rs3,max(cc[h[it]],vec.back().s));
continue;
}
auto ij = --lower_bound(all(vec),make_pair(h[it],(ll)1e16));
rs3=max(rs3,ij->s+max(cc[h[it]],vec.back().s-ij->s));
}
rs2+=rs3;
cyc.clear();
cc.clear();
res.clear();
}cout<<rs-rs2;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:67:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | sort(all(vec));for(int i=1;i<vec.size();i++)vec[i].s+=vec[i-1].s;
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
17488 KB |
Output is correct |
2 |
Correct |
3 ms |
17488 KB |
Output is correct |
3 |
Correct |
4 ms |
17488 KB |
Output is correct |
4 |
Correct |
4 ms |
17488 KB |
Output is correct |
5 |
Correct |
8 ms |
19024 KB |
Output is correct |
6 |
Correct |
6 ms |
18256 KB |
Output is correct |
7 |
Correct |
5 ms |
17912 KB |
Output is correct |
8 |
Correct |
8 ms |
19276 KB |
Output is correct |
9 |
Correct |
6 ms |
18256 KB |
Output is correct |
10 |
Correct |
5 ms |
18116 KB |
Output is correct |
11 |
Correct |
5 ms |
17932 KB |
Output is correct |
12 |
Correct |
6 ms |
18684 KB |
Output is correct |
13 |
Correct |
5 ms |
18512 KB |
Output is correct |
14 |
Correct |
6 ms |
18256 KB |
Output is correct |
15 |
Correct |
5 ms |
18220 KB |
Output is correct |
16 |
Correct |
8 ms |
19280 KB |
Output is correct |
17 |
Correct |
7 ms |
18540 KB |
Output is correct |
18 |
Correct |
5 ms |
17744 KB |
Output is correct |
19 |
Correct |
7 ms |
18768 KB |
Output is correct |
20 |
Correct |
5 ms |
18176 KB |
Output is correct |
21 |
Correct |
5 ms |
18256 KB |
Output is correct |
22 |
Correct |
6 ms |
18512 KB |
Output is correct |
23 |
Correct |
5 ms |
18000 KB |
Output is correct |
24 |
Correct |
6 ms |
18820 KB |
Output is correct |
25 |
Correct |
5 ms |
18256 KB |
Output is correct |
26 |
Correct |
5 ms |
18512 KB |
Output is correct |
27 |
Correct |
7 ms |
18768 KB |
Output is correct |
28 |
Correct |
7 ms |
18768 KB |
Output is correct |
29 |
Correct |
7 ms |
19192 KB |
Output is correct |
30 |
Correct |
7 ms |
19024 KB |
Output is correct |
31 |
Correct |
7 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
17488 KB |
Output is correct |
2 |
Correct |
3 ms |
17488 KB |
Output is correct |
3 |
Correct |
4 ms |
17488 KB |
Output is correct |
4 |
Correct |
4 ms |
17488 KB |
Output is correct |
5 |
Correct |
8 ms |
19024 KB |
Output is correct |
6 |
Correct |
6 ms |
18256 KB |
Output is correct |
7 |
Correct |
5 ms |
17912 KB |
Output is correct |
8 |
Correct |
8 ms |
19276 KB |
Output is correct |
9 |
Correct |
6 ms |
18256 KB |
Output is correct |
10 |
Correct |
5 ms |
18116 KB |
Output is correct |
11 |
Correct |
5 ms |
17932 KB |
Output is correct |
12 |
Correct |
6 ms |
18684 KB |
Output is correct |
13 |
Correct |
5 ms |
18512 KB |
Output is correct |
14 |
Correct |
6 ms |
18256 KB |
Output is correct |
15 |
Correct |
5 ms |
18220 KB |
Output is correct |
16 |
Correct |
8 ms |
19280 KB |
Output is correct |
17 |
Correct |
7 ms |
18540 KB |
Output is correct |
18 |
Correct |
5 ms |
17744 KB |
Output is correct |
19 |
Correct |
7 ms |
18768 KB |
Output is correct |
20 |
Correct |
5 ms |
18176 KB |
Output is correct |
21 |
Correct |
5 ms |
18256 KB |
Output is correct |
22 |
Correct |
6 ms |
18512 KB |
Output is correct |
23 |
Correct |
5 ms |
18000 KB |
Output is correct |
24 |
Correct |
6 ms |
18820 KB |
Output is correct |
25 |
Correct |
5 ms |
18256 KB |
Output is correct |
26 |
Correct |
5 ms |
18512 KB |
Output is correct |
27 |
Correct |
7 ms |
18768 KB |
Output is correct |
28 |
Correct |
7 ms |
18768 KB |
Output is correct |
29 |
Correct |
7 ms |
19192 KB |
Output is correct |
30 |
Correct |
7 ms |
19024 KB |
Output is correct |
31 |
Correct |
7 ms |
19036 KB |
Output is correct |
32 |
Correct |
8 ms |
19024 KB |
Output is correct |
33 |
Correct |
299 ms |
85924 KB |
Output is correct |
34 |
Correct |
163 ms |
50388 KB |
Output is correct |
35 |
Correct |
270 ms |
84160 KB |
Output is correct |
36 |
Correct |
162 ms |
50500 KB |
Output is correct |
37 |
Correct |
92 ms |
31304 KB |
Output is correct |
38 |
Correct |
74 ms |
26952 KB |
Output is correct |
39 |
Correct |
115 ms |
55368 KB |
Output is correct |
40 |
Correct |
117 ms |
55300 KB |
Output is correct |
41 |
Correct |
87 ms |
55052 KB |
Output is correct |
42 |
Correct |
100 ms |
39680 KB |
Output is correct |
43 |
Correct |
94 ms |
39500 KB |
Output is correct |
44 |
Correct |
299 ms |
112464 KB |
Output is correct |
45 |
Correct |
179 ms |
64716 KB |
Output is correct |
46 |
Correct |
59 ms |
26952 KB |
Output is correct |
47 |
Correct |
202 ms |
59804 KB |
Output is correct |
48 |
Correct |
89 ms |
42764 KB |
Output is correct |
49 |
Correct |
75 ms |
42568 KB |
Output is correct |
50 |
Correct |
188 ms |
49728 KB |
Output is correct |
51 |
Correct |
67 ms |
30916 KB |
Output is correct |
52 |
Correct |
195 ms |
60480 KB |
Output is correct |
53 |
Correct |
90 ms |
43200 KB |
Output is correct |
54 |
Correct |
92 ms |
55248 KB |
Output is correct |
55 |
Correct |
166 ms |
67524 KB |
Output is correct |
56 |
Correct |
151 ms |
74940 KB |
Output is correct |
57 |
Correct |
164 ms |
79036 KB |
Output is correct |
58 |
Correct |
152 ms |
70840 KB |
Output is correct |
59 |
Correct |
153 ms |
70816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
17488 KB |
Output is correct |
2 |
Correct |
3 ms |
17488 KB |
Output is correct |
3 |
Correct |
4 ms |
17488 KB |
Output is correct |
4 |
Correct |
4 ms |
17488 KB |
Output is correct |
5 |
Correct |
8 ms |
19024 KB |
Output is correct |
6 |
Correct |
6 ms |
18256 KB |
Output is correct |
7 |
Correct |
5 ms |
17912 KB |
Output is correct |
8 |
Correct |
8 ms |
19276 KB |
Output is correct |
9 |
Correct |
6 ms |
18256 KB |
Output is correct |
10 |
Correct |
5 ms |
18116 KB |
Output is correct |
11 |
Correct |
5 ms |
17932 KB |
Output is correct |
12 |
Correct |
6 ms |
18684 KB |
Output is correct |
13 |
Correct |
5 ms |
18512 KB |
Output is correct |
14 |
Correct |
6 ms |
18256 KB |
Output is correct |
15 |
Correct |
5 ms |
18220 KB |
Output is correct |
16 |
Correct |
8 ms |
19280 KB |
Output is correct |
17 |
Correct |
7 ms |
18540 KB |
Output is correct |
18 |
Correct |
5 ms |
17744 KB |
Output is correct |
19 |
Correct |
7 ms |
18768 KB |
Output is correct |
20 |
Correct |
5 ms |
18176 KB |
Output is correct |
21 |
Correct |
5 ms |
18256 KB |
Output is correct |
22 |
Correct |
6 ms |
18512 KB |
Output is correct |
23 |
Correct |
5 ms |
18000 KB |
Output is correct |
24 |
Correct |
6 ms |
18820 KB |
Output is correct |
25 |
Correct |
5 ms |
18256 KB |
Output is correct |
26 |
Correct |
5 ms |
18512 KB |
Output is correct |
27 |
Correct |
7 ms |
18768 KB |
Output is correct |
28 |
Correct |
7 ms |
18768 KB |
Output is correct |
29 |
Correct |
7 ms |
19192 KB |
Output is correct |
30 |
Correct |
7 ms |
19024 KB |
Output is correct |
31 |
Correct |
7 ms |
19036 KB |
Output is correct |
32 |
Correct |
8 ms |
19024 KB |
Output is correct |
33 |
Correct |
299 ms |
85924 KB |
Output is correct |
34 |
Correct |
163 ms |
50388 KB |
Output is correct |
35 |
Correct |
270 ms |
84160 KB |
Output is correct |
36 |
Correct |
162 ms |
50500 KB |
Output is correct |
37 |
Correct |
92 ms |
31304 KB |
Output is correct |
38 |
Correct |
74 ms |
26952 KB |
Output is correct |
39 |
Correct |
115 ms |
55368 KB |
Output is correct |
40 |
Correct |
117 ms |
55300 KB |
Output is correct |
41 |
Correct |
87 ms |
55052 KB |
Output is correct |
42 |
Correct |
100 ms |
39680 KB |
Output is correct |
43 |
Correct |
94 ms |
39500 KB |
Output is correct |
44 |
Correct |
299 ms |
112464 KB |
Output is correct |
45 |
Correct |
179 ms |
64716 KB |
Output is correct |
46 |
Correct |
59 ms |
26952 KB |
Output is correct |
47 |
Correct |
202 ms |
59804 KB |
Output is correct |
48 |
Correct |
89 ms |
42764 KB |
Output is correct |
49 |
Correct |
75 ms |
42568 KB |
Output is correct |
50 |
Correct |
188 ms |
49728 KB |
Output is correct |
51 |
Correct |
67 ms |
30916 KB |
Output is correct |
52 |
Correct |
195 ms |
60480 KB |
Output is correct |
53 |
Correct |
90 ms |
43200 KB |
Output is correct |
54 |
Correct |
92 ms |
55248 KB |
Output is correct |
55 |
Correct |
166 ms |
67524 KB |
Output is correct |
56 |
Correct |
151 ms |
74940 KB |
Output is correct |
57 |
Correct |
164 ms |
79036 KB |
Output is correct |
58 |
Correct |
152 ms |
70840 KB |
Output is correct |
59 |
Correct |
153 ms |
70816 KB |
Output is correct |
60 |
Correct |
3 ms |
17488 KB |
Output is correct |
61 |
Correct |
3 ms |
17488 KB |
Output is correct |
62 |
Correct |
3 ms |
17488 KB |
Output is correct |
63 |
Correct |
4 ms |
17720 KB |
Output is correct |
64 |
Correct |
259 ms |
75748 KB |
Output is correct |
65 |
Correct |
185 ms |
48712 KB |
Output is correct |
66 |
Correct |
227 ms |
67264 KB |
Output is correct |
67 |
Correct |
146 ms |
48712 KB |
Output is correct |
68 |
Correct |
91 ms |
34968 KB |
Output is correct |
69 |
Correct |
79 ms |
31616 KB |
Output is correct |
70 |
Correct |
186 ms |
49052 KB |
Output is correct |
71 |
Correct |
104 ms |
37768 KB |
Output is correct |
72 |
Correct |
207 ms |
58044 KB |
Output is correct |
73 |
Correct |
118 ms |
46016 KB |
Output is correct |
74 |
Correct |
237 ms |
56132 KB |
Output is correct |
75 |
Correct |
152 ms |
40740 KB |
Output is correct |
76 |
Correct |
123 ms |
40544 KB |
Output is correct |
77 |
Correct |
231 ms |
56768 KB |
Output is correct |
78 |
Correct |
147 ms |
41028 KB |
Output is correct |
79 |
Correct |
256 ms |
74828 KB |
Output is correct |
80 |
Correct |
151 ms |
52044 KB |
Output is correct |
81 |
Correct |
113 ms |
41580 KB |
Output is correct |
82 |
Correct |
158 ms |
58300 KB |
Output is correct |
83 |
Correct |
59 ms |
29256 KB |
Output is correct |
84 |
Correct |
201 ms |
60476 KB |
Output is correct |
85 |
Correct |
207 ms |
60476 KB |
Output is correct |
86 |
Correct |
191 ms |
57152 KB |
Output is correct |
87 |
Correct |
203 ms |
61244 KB |
Output is correct |
88 |
Correct |
244 ms |
63036 KB |
Output is correct |