#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000007;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };
Vi v[200001];
int h[200001],pt[200001];
ll c[200001];
map<int,ll,greater<int>> M[200001];
int ind[200001],gn[200001],GN=1;
Vi st;
Vi group[200005];
bool chk[200005],fin[200005];
void merge(int next,int N){
int &x=pt[next],&y=pt[N];
if(M[x].size()>M[y].size())swap(x,y);
for(auto [a,b]:M[x]){
M[y][a]+=b;
}
}
void dfs(int N){
if(v[N].empty()){
M[N][h[N]]=c[N];
return;
}
if(gn[N]){
for(int x:group[gn[N]]){
chk[x]=1;
for(int next:v[x]){
if(!gn[next]){
dfs(next);
merge(next,N);
}
}
}
return;
}
for(int next:v[N]){
dfs(next);
merge(next,N);
}
int nn=pt[N];
M[nn][h[N]]+=c[N];
ll t=c[N];
auto it=M[nn].upper_bound(h[N]);
while(it!=M[nn].end()){
if(it->Y<t){
t-=it->Y;
it=M[nn].erase(it);
}else{
it->Y-=t;
break;
}
}
}
void dfs2(int N){
st.pb(N);
chk[N]=1;
for(int next:v[N]){
if(chk[next]){
if(!fin[next]){
int M=st.size();
fdn(ii,M-1,0,1){
gn[st[ii]]=GN;
group[GN].pb(st[ii]);
if(st[ii]==next)break;
}
GN++;
}
}
else dfs2(next);
}
fin[N]=1;
st.pop_back();
}
int main(){
int n;
scanf("%d",&n);
iota(pt,pt+n+1,0);
ll tot=0;
fup(i,1,n,1){
int x;
scanf("%d%d%lld",&x,&h[i],&c[i]);
tot+=c[i];
if(i!=x){
v[x].pb(i);
ind[i]++;
}
}
fup(i,1,n,1){
if(!chk[i]){
st.clear();
dfs2(i);
}
}
ll ans=0;
MEM0(chk);
fup(i,1,n,1){
if(!chk[i] && (ind[i]==0 || gn[i])){
dfs(i);
if(ind[i]==0){
for(auto [a,b]:M[pt[i]])ans+=b;
}else{
M[pt[i]][1000000007]+=0;
VPll v1,v2;
for(auto it=M[pt[i]].begin();it!=M[pt[i]].end();it++){
v1.pb(*it);
}
reverse(ALL(v1));
int N=v1.size();
fdn(i,N-2,0,1)v1[i].Y+=v1[i+1].Y;
//fup(i,0,N-1,1)printf("%lld %lld\n",v1[i].X,v1[i].Y);
map<ll,ll> MM;
for(int x:group[gn[i]]){
MM[h[x]]+=c[x];
}
for(auto it=MM.begin();it!=MM.end();it++){
v2.pb(*it);
}
ll tmp=0;
bool ok=0;
fup(i,0,(int)v2.size()-1,1){
if(v2[i].X!=i+1){
ok=1;
int t=lower_bound(ALL(v1),mp((ll)i,-1ll))-v1.begin();
tmp=max(tmp,v1[t].Y);
}
int t=lower_bound(ALL(v1),mp(v2[i].X,-1ll))-v1.begin();
tmp=max(tmp,v1[t].Y+v2[i].Y);
}
if(!ok){
int t=lower_bound(ALL(v1),mp((ll)v2.size(),-1ll))-v1.begin();
tmp=max(tmp,v1[t].Y);
}
ans+=tmp;
}
}
}
printf("%lld\n",tot-ans);
}
Compilation message
worst_reporter2.cpp: In function 'void dfs2(int)':
worst_reporter2.cpp:11:30: warning: unnecessary parentheses in declaration of 'ii' [-Wparentheses]
11 | #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
| ^
worst_reporter2.cpp:101:5: note: in expansion of macro 'fdn'
101 | fdn(ii,M-1,0,1){
| ^~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
worst_reporter2.cpp:119:2: note: in expansion of macro 'fup'
119 | fup(i,1,n,1){
| ^~~
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
worst_reporter2.cpp:128:2: note: in expansion of macro 'fup'
128 | fup(i,1,n,1){
| ^~~
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
worst_reporter2.cpp:136:2: note: in expansion of macro 'fup'
136 | fup(i,1,n,1){
| ^~~
worst_reporter2.cpp:11:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
11 | #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
| ^
worst_reporter2.cpp:149:5: note: in expansion of macro 'fdn'
149 | fdn(i,N-2,0,1)v1[i].Y+=v1[i+1].Y;
| ^~~
worst_reporter2.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
worst_reporter2.cpp:160:5: note: in expansion of macro 'fup'
160 | fup(i,0,(int)v2.size()-1,1){
| ^~~
worst_reporter2.cpp:116:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
worst_reporter2.cpp:121:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | scanf("%d%d%lld",&x,&h[i],&c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19276 KB |
Output is correct |
2 |
Correct |
13 ms |
19276 KB |
Output is correct |
3 |
Correct |
14 ms |
19224 KB |
Output is correct |
4 |
Correct |
14 ms |
19276 KB |
Output is correct |
5 |
Correct |
19 ms |
20556 KB |
Output is correct |
6 |
Correct |
18 ms |
20060 KB |
Output is correct |
7 |
Correct |
17 ms |
19924 KB |
Output is correct |
8 |
Correct |
19 ms |
20556 KB |
Output is correct |
9 |
Correct |
17 ms |
20044 KB |
Output is correct |
10 |
Correct |
16 ms |
19936 KB |
Output is correct |
11 |
Correct |
16 ms |
19660 KB |
Output is correct |
12 |
Correct |
17 ms |
20152 KB |
Output is correct |
13 |
Correct |
15 ms |
20172 KB |
Output is correct |
14 |
Correct |
16 ms |
19948 KB |
Output is correct |
15 |
Correct |
17 ms |
19916 KB |
Output is correct |
16 |
Correct |
20 ms |
20952 KB |
Output is correct |
17 |
Correct |
18 ms |
20252 KB |
Output is correct |
18 |
Correct |
17 ms |
19724 KB |
Output is correct |
19 |
Correct |
16 ms |
20168 KB |
Output is correct |
20 |
Correct |
16 ms |
19916 KB |
Output is correct |
21 |
Correct |
16 ms |
19916 KB |
Output is correct |
22 |
Correct |
19 ms |
20044 KB |
Output is correct |
23 |
Correct |
16 ms |
19788 KB |
Output is correct |
24 |
Correct |
20 ms |
20172 KB |
Output is correct |
25 |
Correct |
16 ms |
20020 KB |
Output is correct |
26 |
Correct |
17 ms |
20428 KB |
Output is correct |
27 |
Correct |
17 ms |
20172 KB |
Output is correct |
28 |
Correct |
18 ms |
20172 KB |
Output is correct |
29 |
Correct |
17 ms |
20300 KB |
Output is correct |
30 |
Correct |
17 ms |
20284 KB |
Output is correct |
31 |
Correct |
18 ms |
20300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19276 KB |
Output is correct |
2 |
Correct |
13 ms |
19276 KB |
Output is correct |
3 |
Correct |
14 ms |
19224 KB |
Output is correct |
4 |
Correct |
14 ms |
19276 KB |
Output is correct |
5 |
Correct |
19 ms |
20556 KB |
Output is correct |
6 |
Correct |
18 ms |
20060 KB |
Output is correct |
7 |
Correct |
17 ms |
19924 KB |
Output is correct |
8 |
Correct |
19 ms |
20556 KB |
Output is correct |
9 |
Correct |
17 ms |
20044 KB |
Output is correct |
10 |
Correct |
16 ms |
19936 KB |
Output is correct |
11 |
Correct |
16 ms |
19660 KB |
Output is correct |
12 |
Correct |
17 ms |
20152 KB |
Output is correct |
13 |
Correct |
15 ms |
20172 KB |
Output is correct |
14 |
Correct |
16 ms |
19948 KB |
Output is correct |
15 |
Correct |
17 ms |
19916 KB |
Output is correct |
16 |
Correct |
20 ms |
20952 KB |
Output is correct |
17 |
Correct |
18 ms |
20252 KB |
Output is correct |
18 |
Correct |
17 ms |
19724 KB |
Output is correct |
19 |
Correct |
16 ms |
20168 KB |
Output is correct |
20 |
Correct |
16 ms |
19916 KB |
Output is correct |
21 |
Correct |
16 ms |
19916 KB |
Output is correct |
22 |
Correct |
19 ms |
20044 KB |
Output is correct |
23 |
Correct |
16 ms |
19788 KB |
Output is correct |
24 |
Correct |
20 ms |
20172 KB |
Output is correct |
25 |
Correct |
16 ms |
20020 KB |
Output is correct |
26 |
Correct |
17 ms |
20428 KB |
Output is correct |
27 |
Correct |
17 ms |
20172 KB |
Output is correct |
28 |
Correct |
18 ms |
20172 KB |
Output is correct |
29 |
Correct |
17 ms |
20300 KB |
Output is correct |
30 |
Correct |
17 ms |
20284 KB |
Output is correct |
31 |
Correct |
18 ms |
20300 KB |
Output is correct |
32 |
Correct |
20 ms |
20484 KB |
Output is correct |
33 |
Correct |
415 ms |
81348 KB |
Output is correct |
34 |
Correct |
335 ms |
57028 KB |
Output is correct |
35 |
Correct |
408 ms |
78876 KB |
Output is correct |
36 |
Correct |
333 ms |
57048 KB |
Output is correct |
37 |
Correct |
228 ms |
37996 KB |
Output is correct |
38 |
Correct |
211 ms |
33680 KB |
Output is correct |
39 |
Correct |
179 ms |
50152 KB |
Output is correct |
40 |
Correct |
168 ms |
50208 KB |
Output is correct |
41 |
Correct |
131 ms |
50192 KB |
Output is correct |
42 |
Correct |
171 ms |
40396 KB |
Output is correct |
43 |
Correct |
165 ms |
40380 KB |
Output is correct |
44 |
Correct |
356 ms |
106784 KB |
Output is correct |
45 |
Correct |
251 ms |
71224 KB |
Output is correct |
46 |
Correct |
122 ms |
33608 KB |
Output is correct |
47 |
Correct |
276 ms |
50332 KB |
Output is correct |
48 |
Correct |
146 ms |
43412 KB |
Output is correct |
49 |
Correct |
123 ms |
43332 KB |
Output is correct |
50 |
Correct |
291 ms |
50068 KB |
Output is correct |
51 |
Correct |
128 ms |
37516 KB |
Output is correct |
52 |
Correct |
328 ms |
51000 KB |
Output is correct |
53 |
Correct |
145 ms |
43860 KB |
Output is correct |
54 |
Correct |
202 ms |
62580 KB |
Output is correct |
55 |
Correct |
271 ms |
52764 KB |
Output is correct |
56 |
Correct |
213 ms |
56464 KB |
Output is correct |
57 |
Correct |
200 ms |
58660 KB |
Output is correct |
58 |
Correct |
210 ms |
58976 KB |
Output is correct |
59 |
Correct |
216 ms |
58948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19276 KB |
Output is correct |
2 |
Correct |
13 ms |
19276 KB |
Output is correct |
3 |
Correct |
14 ms |
19224 KB |
Output is correct |
4 |
Correct |
14 ms |
19276 KB |
Output is correct |
5 |
Correct |
19 ms |
20556 KB |
Output is correct |
6 |
Correct |
18 ms |
20060 KB |
Output is correct |
7 |
Correct |
17 ms |
19924 KB |
Output is correct |
8 |
Correct |
19 ms |
20556 KB |
Output is correct |
9 |
Correct |
17 ms |
20044 KB |
Output is correct |
10 |
Correct |
16 ms |
19936 KB |
Output is correct |
11 |
Correct |
16 ms |
19660 KB |
Output is correct |
12 |
Correct |
17 ms |
20152 KB |
Output is correct |
13 |
Correct |
15 ms |
20172 KB |
Output is correct |
14 |
Correct |
16 ms |
19948 KB |
Output is correct |
15 |
Correct |
17 ms |
19916 KB |
Output is correct |
16 |
Correct |
20 ms |
20952 KB |
Output is correct |
17 |
Correct |
18 ms |
20252 KB |
Output is correct |
18 |
Correct |
17 ms |
19724 KB |
Output is correct |
19 |
Correct |
16 ms |
20168 KB |
Output is correct |
20 |
Correct |
16 ms |
19916 KB |
Output is correct |
21 |
Correct |
16 ms |
19916 KB |
Output is correct |
22 |
Correct |
19 ms |
20044 KB |
Output is correct |
23 |
Correct |
16 ms |
19788 KB |
Output is correct |
24 |
Correct |
20 ms |
20172 KB |
Output is correct |
25 |
Correct |
16 ms |
20020 KB |
Output is correct |
26 |
Correct |
17 ms |
20428 KB |
Output is correct |
27 |
Correct |
17 ms |
20172 KB |
Output is correct |
28 |
Correct |
18 ms |
20172 KB |
Output is correct |
29 |
Correct |
17 ms |
20300 KB |
Output is correct |
30 |
Correct |
17 ms |
20284 KB |
Output is correct |
31 |
Correct |
18 ms |
20300 KB |
Output is correct |
32 |
Correct |
20 ms |
20484 KB |
Output is correct |
33 |
Correct |
415 ms |
81348 KB |
Output is correct |
34 |
Correct |
335 ms |
57028 KB |
Output is correct |
35 |
Correct |
408 ms |
78876 KB |
Output is correct |
36 |
Correct |
333 ms |
57048 KB |
Output is correct |
37 |
Correct |
228 ms |
37996 KB |
Output is correct |
38 |
Correct |
211 ms |
33680 KB |
Output is correct |
39 |
Correct |
179 ms |
50152 KB |
Output is correct |
40 |
Correct |
168 ms |
50208 KB |
Output is correct |
41 |
Correct |
131 ms |
50192 KB |
Output is correct |
42 |
Correct |
171 ms |
40396 KB |
Output is correct |
43 |
Correct |
165 ms |
40380 KB |
Output is correct |
44 |
Correct |
356 ms |
106784 KB |
Output is correct |
45 |
Correct |
251 ms |
71224 KB |
Output is correct |
46 |
Correct |
122 ms |
33608 KB |
Output is correct |
47 |
Correct |
276 ms |
50332 KB |
Output is correct |
48 |
Correct |
146 ms |
43412 KB |
Output is correct |
49 |
Correct |
123 ms |
43332 KB |
Output is correct |
50 |
Correct |
291 ms |
50068 KB |
Output is correct |
51 |
Correct |
128 ms |
37516 KB |
Output is correct |
52 |
Correct |
328 ms |
51000 KB |
Output is correct |
53 |
Correct |
145 ms |
43860 KB |
Output is correct |
54 |
Correct |
202 ms |
62580 KB |
Output is correct |
55 |
Correct |
271 ms |
52764 KB |
Output is correct |
56 |
Correct |
213 ms |
56464 KB |
Output is correct |
57 |
Correct |
200 ms |
58660 KB |
Output is correct |
58 |
Correct |
210 ms |
58976 KB |
Output is correct |
59 |
Correct |
216 ms |
58948 KB |
Output is correct |
60 |
Correct |
12 ms |
19276 KB |
Output is correct |
61 |
Correct |
12 ms |
19276 KB |
Output is correct |
62 |
Correct |
12 ms |
19276 KB |
Output is correct |
63 |
Correct |
12 ms |
19224 KB |
Output is correct |
64 |
Correct |
386 ms |
71256 KB |
Output is correct |
65 |
Correct |
326 ms |
52728 KB |
Output is correct |
66 |
Correct |
378 ms |
69012 KB |
Output is correct |
67 |
Correct |
321 ms |
52420 KB |
Output is correct |
68 |
Correct |
252 ms |
38944 KB |
Output is correct |
69 |
Correct |
244 ms |
35700 KB |
Output is correct |
70 |
Correct |
322 ms |
60364 KB |
Output is correct |
71 |
Correct |
205 ms |
44476 KB |
Output is correct |
72 |
Correct |
392 ms |
70464 KB |
Output is correct |
73 |
Correct |
212 ms |
54200 KB |
Output is correct |
74 |
Correct |
408 ms |
63832 KB |
Output is correct |
75 |
Correct |
258 ms |
46784 KB |
Output is correct |
76 |
Correct |
232 ms |
46644 KB |
Output is correct |
77 |
Correct |
297 ms |
64132 KB |
Output is correct |
78 |
Correct |
172 ms |
47172 KB |
Output is correct |
79 |
Correct |
377 ms |
79600 KB |
Output is correct |
80 |
Correct |
289 ms |
58168 KB |
Output is correct |
81 |
Correct |
219 ms |
47548 KB |
Output is correct |
82 |
Correct |
274 ms |
70340 KB |
Output is correct |
83 |
Correct |
106 ms |
37956 KB |
Output is correct |
84 |
Correct |
329 ms |
62240 KB |
Output is correct |
85 |
Correct |
350 ms |
62180 KB |
Output is correct |
86 |
Correct |
318 ms |
62156 KB |
Output is correct |
87 |
Correct |
365 ms |
62300 KB |
Output is correct |
88 |
Correct |
324 ms |
62136 KB |
Output is correct |