#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3uHasSr ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
const int inf=1e18;
signed main(){
_3uHasSr;
int n;
cin>>n;
vec(pii) a(n+1);
vi g(n+1);
vec(vi) rg(n+1);
rep(i,n){
int u,h,c;
cin>>u>>h>>c;
u-=1;
g[i]=u;
rg[u].pb(i);
a[i]={h,c};
}
vec(std::map<int,int>) dp(n+1);
vi center(n+1);
rep(v,n+1){
center[v]=v;
}
auto evaluate=[&](int rt,vi&usd){
vi topo;
{
queue<int> que;
que.push(rt);
while(sz(que)){
int v=que.front();
que.pop();
topo.pb(v);
for(auto u:rg[v]){
que.push(u);
}
}
}
for(auto v:topo){
usd[v]=1;
// print(v);
}
// print(".....");
per(i,sz(topo)){
int v=topo[i];
auto [h,c]=a[v];
dp[center[v]][h]+=c;
// perform dp update
{
auto it=dp[center[v]].find(h);
if(it!=dp[center[v]].begin()){
it=prev(it);
while(1){
auto nit=it;
bool lok=0;
if(it==dp[center[v]].begin()){
lok=1;
}else{
it=prev(it);
}
pii _p=*nit;
if(c>=_p.se){
c-=_p.se;
dp[center[v]].erase(nit);
}else{
_p.se-=c;
dp[center[v]].erase(nit);
dp[center[v]][_p.fi]+=_p.se;
break;
}
if(lok){
break;
}
}
}
}
if(!i){
break;
}
// merge to parent node
int up=g[v];
int u0=center[up],u1=center[v];
if(sz(dp[u0])<sz(dp[u1])){
swap(u0,u1);
center[up]=center[v];
}
for(auto p:dp[u1]){
dp[u0][p.fi]+=p.se;
}
}
int v=center[topo[0]];
vec(pii) evs;
for(auto p:dp[v]){
if(p.fi){
evs.pb(p);
}
}
per(i,sz(evs)-1){
evs[i].se+=evs[i+1].se;
}
return evs;
};
const int extra_node=n;
vi usd(n+1),incyc(n+1);
int res=0;
rep(rt,n){
if(usd[rt]){
continue;
}
// print(rt);
vi way,cyc;
{
int v=rt;
while(1){
way.pb(v);
int u=g[v];
if(usd[u]){
while(way.back()!=u){
cyc.pb(way.back());
way.pop_back();
}
cyc.pb(way.back());
way.pop_back();
reverse(cyc.begin(), cyc.end());
break;
}
usd[v]=1;
v=u;
}
for(auto v:cyc){
incyc[v]=1;
}
}
vi rbts;
{
for(auto v:cyc){
for(auto u:rg[v]){
if(!incyc[u]){
rbts.pb(u);
}
}
}
}
// // unite cycle and add edge from every rabbit to extra
{
rg[extra_node].clear();
dp[extra_node].clear();
g[extra_node]=0;
center[extra_node]=extra_node;
}
for(auto v:rbts){
g[v]=extra_node;
rg[extra_node].pb(v);
}
vec(pii) evs=evaluate(extra_node,usd);
int now=sz(evs)?evs[0].se:0;
std::map<int,int> mp;
for(auto v:cyc){
mp[a[v].fi]+=a[v].se;
}
for(auto p:mp){
int need=p.fi;
auto it=lower_bound(evs.begin(), evs.end(),pii(need,-1));
if(it==evs.end()){
now=max(now,p.se);
}else{
now=max(now,p.se+(*it).se);
}
}
res+=now;
}
rep(v,n){
res-=a[v].se;
}
print(-res);
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
2260 KB |
Output is correct |
6 |
Correct |
4 ms |
1620 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
6 ms |
2260 KB |
Output is correct |
9 |
Correct |
4 ms |
1620 KB |
Output is correct |
10 |
Correct |
4 ms |
1364 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
4 ms |
1108 KB |
Output is correct |
13 |
Correct |
3 ms |
1108 KB |
Output is correct |
14 |
Correct |
4 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
6 ms |
2516 KB |
Output is correct |
17 |
Correct |
4 ms |
1696 KB |
Output is correct |
18 |
Correct |
3 ms |
1108 KB |
Output is correct |
19 |
Correct |
4 ms |
1364 KB |
Output is correct |
20 |
Correct |
3 ms |
1108 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
4 ms |
1876 KB |
Output is correct |
23 |
Correct |
2 ms |
1364 KB |
Output is correct |
24 |
Correct |
4 ms |
1492 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
3 ms |
1108 KB |
Output is correct |
27 |
Correct |
4 ms |
1620 KB |
Output is correct |
28 |
Correct |
4 ms |
1492 KB |
Output is correct |
29 |
Correct |
4 ms |
1492 KB |
Output is correct |
30 |
Correct |
5 ms |
1748 KB |
Output is correct |
31 |
Correct |
4 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
2260 KB |
Output is correct |
6 |
Correct |
4 ms |
1620 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
6 ms |
2260 KB |
Output is correct |
9 |
Correct |
4 ms |
1620 KB |
Output is correct |
10 |
Correct |
4 ms |
1364 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
4 ms |
1108 KB |
Output is correct |
13 |
Correct |
3 ms |
1108 KB |
Output is correct |
14 |
Correct |
4 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
6 ms |
2516 KB |
Output is correct |
17 |
Correct |
4 ms |
1696 KB |
Output is correct |
18 |
Correct |
3 ms |
1108 KB |
Output is correct |
19 |
Correct |
4 ms |
1364 KB |
Output is correct |
20 |
Correct |
3 ms |
1108 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
4 ms |
1876 KB |
Output is correct |
23 |
Correct |
2 ms |
1364 KB |
Output is correct |
24 |
Correct |
4 ms |
1492 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
3 ms |
1108 KB |
Output is correct |
27 |
Correct |
4 ms |
1620 KB |
Output is correct |
28 |
Correct |
4 ms |
1492 KB |
Output is correct |
29 |
Correct |
4 ms |
1492 KB |
Output is correct |
30 |
Correct |
5 ms |
1748 KB |
Output is correct |
31 |
Correct |
4 ms |
1748 KB |
Output is correct |
32 |
Correct |
6 ms |
2260 KB |
Output is correct |
33 |
Correct |
362 ms |
99436 KB |
Output is correct |
34 |
Correct |
298 ms |
65212 KB |
Output is correct |
35 |
Correct |
357 ms |
97960 KB |
Output is correct |
36 |
Correct |
278 ms |
65148 KB |
Output is correct |
37 |
Correct |
177 ms |
40592 KB |
Output is correct |
38 |
Correct |
151 ms |
35284 KB |
Output is correct |
39 |
Correct |
149 ms |
32220 KB |
Output is correct |
40 |
Correct |
132 ms |
32152 KB |
Output is correct |
41 |
Correct |
86 ms |
32136 KB |
Output is correct |
42 |
Correct |
150 ms |
32176 KB |
Output is correct |
43 |
Correct |
142 ms |
32220 KB |
Output is correct |
44 |
Correct |
281 ms |
112224 KB |
Output is correct |
45 |
Correct |
194 ms |
72352 KB |
Output is correct |
46 |
Correct |
88 ms |
34724 KB |
Output is correct |
47 |
Correct |
235 ms |
44764 KB |
Output is correct |
48 |
Correct |
123 ms |
34748 KB |
Output is correct |
49 |
Correct |
89 ms |
34708 KB |
Output is correct |
50 |
Correct |
230 ms |
59292 KB |
Output is correct |
51 |
Correct |
93 ms |
42644 KB |
Output is correct |
52 |
Correct |
234 ms |
45736 KB |
Output is correct |
53 |
Correct |
113 ms |
35552 KB |
Output is correct |
54 |
Correct |
90 ms |
32244 KB |
Output is correct |
55 |
Correct |
194 ms |
51812 KB |
Output is correct |
56 |
Correct |
161 ms |
47684 KB |
Output is correct |
57 |
Correct |
154 ms |
47544 KB |
Output is correct |
58 |
Correct |
213 ms |
54608 KB |
Output is correct |
59 |
Correct |
214 ms |
54576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
6 ms |
2260 KB |
Output is correct |
6 |
Correct |
4 ms |
1620 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
6 ms |
2260 KB |
Output is correct |
9 |
Correct |
4 ms |
1620 KB |
Output is correct |
10 |
Correct |
4 ms |
1364 KB |
Output is correct |
11 |
Correct |
3 ms |
1108 KB |
Output is correct |
12 |
Correct |
4 ms |
1108 KB |
Output is correct |
13 |
Correct |
3 ms |
1108 KB |
Output is correct |
14 |
Correct |
4 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
6 ms |
2516 KB |
Output is correct |
17 |
Correct |
4 ms |
1696 KB |
Output is correct |
18 |
Correct |
3 ms |
1108 KB |
Output is correct |
19 |
Correct |
4 ms |
1364 KB |
Output is correct |
20 |
Correct |
3 ms |
1108 KB |
Output is correct |
21 |
Correct |
3 ms |
1108 KB |
Output is correct |
22 |
Correct |
4 ms |
1876 KB |
Output is correct |
23 |
Correct |
2 ms |
1364 KB |
Output is correct |
24 |
Correct |
4 ms |
1492 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
3 ms |
1108 KB |
Output is correct |
27 |
Correct |
4 ms |
1620 KB |
Output is correct |
28 |
Correct |
4 ms |
1492 KB |
Output is correct |
29 |
Correct |
4 ms |
1492 KB |
Output is correct |
30 |
Correct |
5 ms |
1748 KB |
Output is correct |
31 |
Correct |
4 ms |
1748 KB |
Output is correct |
32 |
Correct |
6 ms |
2260 KB |
Output is correct |
33 |
Correct |
362 ms |
99436 KB |
Output is correct |
34 |
Correct |
298 ms |
65212 KB |
Output is correct |
35 |
Correct |
357 ms |
97960 KB |
Output is correct |
36 |
Correct |
278 ms |
65148 KB |
Output is correct |
37 |
Correct |
177 ms |
40592 KB |
Output is correct |
38 |
Correct |
151 ms |
35284 KB |
Output is correct |
39 |
Correct |
149 ms |
32220 KB |
Output is correct |
40 |
Correct |
132 ms |
32152 KB |
Output is correct |
41 |
Correct |
86 ms |
32136 KB |
Output is correct |
42 |
Correct |
150 ms |
32176 KB |
Output is correct |
43 |
Correct |
142 ms |
32220 KB |
Output is correct |
44 |
Correct |
281 ms |
112224 KB |
Output is correct |
45 |
Correct |
194 ms |
72352 KB |
Output is correct |
46 |
Correct |
88 ms |
34724 KB |
Output is correct |
47 |
Correct |
235 ms |
44764 KB |
Output is correct |
48 |
Correct |
123 ms |
34748 KB |
Output is correct |
49 |
Correct |
89 ms |
34708 KB |
Output is correct |
50 |
Correct |
230 ms |
59292 KB |
Output is correct |
51 |
Correct |
93 ms |
42644 KB |
Output is correct |
52 |
Correct |
234 ms |
45736 KB |
Output is correct |
53 |
Correct |
113 ms |
35552 KB |
Output is correct |
54 |
Correct |
90 ms |
32244 KB |
Output is correct |
55 |
Correct |
194 ms |
51812 KB |
Output is correct |
56 |
Correct |
161 ms |
47684 KB |
Output is correct |
57 |
Correct |
154 ms |
47544 KB |
Output is correct |
58 |
Correct |
213 ms |
54608 KB |
Output is correct |
59 |
Correct |
214 ms |
54576 KB |
Output is correct |
60 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |