#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef long long ll;
const int N = 5005;
vector<int> edges[N];
int used[N], is[N];
ll dp[N][N], c[N];
int a[N], h[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
vector<int> p(n);
for(int i=0;i<n;i++){
cin>>a[i]>>h[i]>>c[i];
a[i]--; p[i] = i;
} sort(p.begin(), p.end(), [&](int i, int j) { return (h[i] < h[j]); });
for(int i=0, last=0;i<n;){
int j = i;
while(j<n && h[p[i]] == h[p[j]]) j++;
while(i < j) h[p[i]] = last, i++;
last++;
}
vector<vector<int>> cc;
for(int i=0;i<n;i++){
if(used[i]) continue;
vector<int> ss;
int u = i;
while(!used[u]){
used[u] = 1;
ss.push_back(u);
u = a[u];
}
vector<int> tmp = {u};
while(!ss.empty() && ss.back() != u){
tmp.push_back(ss.back());
ss.pop_back();
}
if(ss.empty()) continue;
for(auto x : tmp) is[x] = 1;
cc.push_back(tmp);
}
//~ for(auto v : cc){
//~ for(auto x : v) cout<<x<<" ";
//~ cout<<"\n";
//~ }
for(int i=0;i<n;i++){
if(is[i] && is[a[i]]) continue;
edges[a[i]].push_back(i);
}
function<void(int)> dfs = [&](int u){
dp[u][h[u]] += c[u];
for(auto x : edges[u]){
dfs(x);
for(int j=0;j<n;j++){
dp[u][j] += dp[x][j];
}
}
if(!is[u]){
for(int j=n-1;~j;j--){
dp[u][j] = max(dp[u][j], dp[u][j+1]);
}
}
};
//~ for(int i=0;i<n;i++) cout<<h[i]<<" ";
//~ cout<<"\n";
for(int i=0;i<n;i++){
if(!is[i]) continue;
dfs(i);
}
//~ for(int i=0;i<n;i++){
//~ for(int j=0;j<n;j++) cout<<dp[i][j]<<" ";
//~ cout<<"\n";
//~ }
ll res = accumulate(c, c+n, 0ll);
//~ cout<<res<<"\n";
for(auto v : cc){
ll tot = 0;
for(int j=0;j<n;j++){
ll rr = 0;
for(auto x : v){
rr += dp[x][j];
} tot = max(tot, rr);
} res -= tot;
}
cout<<res<<"\n";
}
/*
6
1 6 5
1 3 6
1 8 4
3 4 9
2 2 5
2 5 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
184 ms |
196416 KB |
Output is correct |
6 |
Correct |
179 ms |
196432 KB |
Output is correct |
7 |
Correct |
189 ms |
196436 KB |
Output is correct |
8 |
Correct |
180 ms |
196428 KB |
Output is correct |
9 |
Correct |
180 ms |
196420 KB |
Output is correct |
10 |
Correct |
179 ms |
196396 KB |
Output is correct |
11 |
Correct |
183 ms |
196420 KB |
Output is correct |
12 |
Correct |
164 ms |
197020 KB |
Output is correct |
13 |
Correct |
164 ms |
197028 KB |
Output is correct |
14 |
Correct |
165 ms |
196820 KB |
Output is correct |
15 |
Correct |
163 ms |
196804 KB |
Output is correct |
16 |
Correct |
176 ms |
196440 KB |
Output is correct |
17 |
Correct |
185 ms |
196404 KB |
Output is correct |
18 |
Correct |
182 ms |
196580 KB |
Output is correct |
19 |
Correct |
180 ms |
196636 KB |
Output is correct |
20 |
Correct |
178 ms |
196708 KB |
Output is correct |
21 |
Correct |
178 ms |
196660 KB |
Output is correct |
22 |
Correct |
196 ms |
196384 KB |
Output is correct |
23 |
Correct |
202 ms |
196424 KB |
Output is correct |
24 |
Correct |
179 ms |
196744 KB |
Output is correct |
25 |
Correct |
187 ms |
196816 KB |
Output is correct |
26 |
Correct |
165 ms |
196988 KB |
Output is correct |
27 |
Correct |
181 ms |
196596 KB |
Output is correct |
28 |
Correct |
182 ms |
196772 KB |
Output is correct |
29 |
Correct |
172 ms |
196920 KB |
Output is correct |
30 |
Correct |
166 ms |
196636 KB |
Output is correct |
31 |
Correct |
172 ms |
196804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
184 ms |
196416 KB |
Output is correct |
6 |
Correct |
179 ms |
196432 KB |
Output is correct |
7 |
Correct |
189 ms |
196436 KB |
Output is correct |
8 |
Correct |
180 ms |
196428 KB |
Output is correct |
9 |
Correct |
180 ms |
196420 KB |
Output is correct |
10 |
Correct |
179 ms |
196396 KB |
Output is correct |
11 |
Correct |
183 ms |
196420 KB |
Output is correct |
12 |
Correct |
164 ms |
197020 KB |
Output is correct |
13 |
Correct |
164 ms |
197028 KB |
Output is correct |
14 |
Correct |
165 ms |
196820 KB |
Output is correct |
15 |
Correct |
163 ms |
196804 KB |
Output is correct |
16 |
Correct |
176 ms |
196440 KB |
Output is correct |
17 |
Correct |
185 ms |
196404 KB |
Output is correct |
18 |
Correct |
182 ms |
196580 KB |
Output is correct |
19 |
Correct |
180 ms |
196636 KB |
Output is correct |
20 |
Correct |
178 ms |
196708 KB |
Output is correct |
21 |
Correct |
178 ms |
196660 KB |
Output is correct |
22 |
Correct |
196 ms |
196384 KB |
Output is correct |
23 |
Correct |
202 ms |
196424 KB |
Output is correct |
24 |
Correct |
179 ms |
196744 KB |
Output is correct |
25 |
Correct |
187 ms |
196816 KB |
Output is correct |
26 |
Correct |
165 ms |
196988 KB |
Output is correct |
27 |
Correct |
181 ms |
196596 KB |
Output is correct |
28 |
Correct |
182 ms |
196772 KB |
Output is correct |
29 |
Correct |
172 ms |
196920 KB |
Output is correct |
30 |
Correct |
166 ms |
196636 KB |
Output is correct |
31 |
Correct |
172 ms |
196804 KB |
Output is correct |
32 |
Correct |
188 ms |
196496 KB |
Output is correct |
33 |
Runtime error |
81 ms |
9560 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
184 ms |
196416 KB |
Output is correct |
6 |
Correct |
179 ms |
196432 KB |
Output is correct |
7 |
Correct |
189 ms |
196436 KB |
Output is correct |
8 |
Correct |
180 ms |
196428 KB |
Output is correct |
9 |
Correct |
180 ms |
196420 KB |
Output is correct |
10 |
Correct |
179 ms |
196396 KB |
Output is correct |
11 |
Correct |
183 ms |
196420 KB |
Output is correct |
12 |
Correct |
164 ms |
197020 KB |
Output is correct |
13 |
Correct |
164 ms |
197028 KB |
Output is correct |
14 |
Correct |
165 ms |
196820 KB |
Output is correct |
15 |
Correct |
163 ms |
196804 KB |
Output is correct |
16 |
Correct |
176 ms |
196440 KB |
Output is correct |
17 |
Correct |
185 ms |
196404 KB |
Output is correct |
18 |
Correct |
182 ms |
196580 KB |
Output is correct |
19 |
Correct |
180 ms |
196636 KB |
Output is correct |
20 |
Correct |
178 ms |
196708 KB |
Output is correct |
21 |
Correct |
178 ms |
196660 KB |
Output is correct |
22 |
Correct |
196 ms |
196384 KB |
Output is correct |
23 |
Correct |
202 ms |
196424 KB |
Output is correct |
24 |
Correct |
179 ms |
196744 KB |
Output is correct |
25 |
Correct |
187 ms |
196816 KB |
Output is correct |
26 |
Correct |
165 ms |
196988 KB |
Output is correct |
27 |
Correct |
181 ms |
196596 KB |
Output is correct |
28 |
Correct |
182 ms |
196772 KB |
Output is correct |
29 |
Correct |
172 ms |
196920 KB |
Output is correct |
30 |
Correct |
166 ms |
196636 KB |
Output is correct |
31 |
Correct |
172 ms |
196804 KB |
Output is correct |
32 |
Correct |
188 ms |
196496 KB |
Output is correct |
33 |
Runtime error |
81 ms |
9560 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |