//In the name of God
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 5100;
const int mod = 1e9 + 7;
const int inf = 1e9;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
ll pw(ll a, ll b){
ll c = 1;
while(b){
if(b & 1) c = c * a % mod;
a = a * a % mod;
b >>= 1;
}
return c;
}
int n, a[maxn], h[maxn], c[maxn];
vector<int> g[maxn];
vector<int> vec;
ll dp[maxn][maxn];
void solve(int v){
for(int u : g[v]){
solve(u);
for(int i = 0; i <= n; i++){
dp[v][i] += dp[u][i];
}
}
for(int i = 0; i <= n; i++){
dp[v][i] += c[v];
}
dp[v][h[v]] -= c[v];
for(int i = 1; i <= n; i++){
dp[v][i] = min(dp[v][i], dp[v][i - 1]);
}
return;
}
int main(){
fast_io;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i] >> h[i] >> c[i];
vec.pb(h[i]);
if(i != 1) g[a[i]].pb(i);
}
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
for(int i = 1; i <= n; i++){
h[i] = Sz(vec) - 1 - (lower_bound(all(vec), h[i]) - vec.begin());
}
solve(1);
cout << dp[1][n] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
540 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
452 KB |
Output is correct |
5 |
Correct |
198 ms |
200232 KB |
Output is correct |
6 |
Correct |
201 ms |
200156 KB |
Output is correct |
7 |
Correct |
182 ms |
200260 KB |
Output is correct |
8 |
Correct |
187 ms |
200304 KB |
Output is correct |
9 |
Correct |
188 ms |
200388 KB |
Output is correct |
10 |
Correct |
187 ms |
200312 KB |
Output is correct |
11 |
Correct |
184 ms |
200300 KB |
Output is correct |
12 |
Correct |
185 ms |
200768 KB |
Output is correct |
13 |
Correct |
182 ms |
200760 KB |
Output is correct |
14 |
Correct |
182 ms |
200516 KB |
Output is correct |
15 |
Correct |
186 ms |
200492 KB |
Output is correct |
16 |
Correct |
188 ms |
200312 KB |
Output is correct |
17 |
Correct |
186 ms |
200316 KB |
Output is correct |
18 |
Correct |
182 ms |
200304 KB |
Output is correct |
19 |
Correct |
197 ms |
200576 KB |
Output is correct |
20 |
Correct |
184 ms |
200484 KB |
Output is correct |
21 |
Correct |
182 ms |
200456 KB |
Output is correct |
22 |
Correct |
182 ms |
200264 KB |
Output is correct |
23 |
Correct |
181 ms |
200272 KB |
Output is correct |
24 |
Correct |
183 ms |
200428 KB |
Output is correct |
25 |
Correct |
187 ms |
200528 KB |
Output is correct |
26 |
Correct |
205 ms |
200852 KB |
Output is correct |
27 |
Correct |
187 ms |
200400 KB |
Output is correct |
28 |
Correct |
183 ms |
200516 KB |
Output is correct |
29 |
Correct |
191 ms |
200700 KB |
Output is correct |
30 |
Correct |
184 ms |
200432 KB |
Output is correct |
31 |
Correct |
190 ms |
200456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
540 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
452 KB |
Output is correct |
5 |
Correct |
198 ms |
200232 KB |
Output is correct |
6 |
Correct |
201 ms |
200156 KB |
Output is correct |
7 |
Correct |
182 ms |
200260 KB |
Output is correct |
8 |
Correct |
187 ms |
200304 KB |
Output is correct |
9 |
Correct |
188 ms |
200388 KB |
Output is correct |
10 |
Correct |
187 ms |
200312 KB |
Output is correct |
11 |
Correct |
184 ms |
200300 KB |
Output is correct |
12 |
Correct |
185 ms |
200768 KB |
Output is correct |
13 |
Correct |
182 ms |
200760 KB |
Output is correct |
14 |
Correct |
182 ms |
200516 KB |
Output is correct |
15 |
Correct |
186 ms |
200492 KB |
Output is correct |
16 |
Correct |
188 ms |
200312 KB |
Output is correct |
17 |
Correct |
186 ms |
200316 KB |
Output is correct |
18 |
Correct |
182 ms |
200304 KB |
Output is correct |
19 |
Correct |
197 ms |
200576 KB |
Output is correct |
20 |
Correct |
184 ms |
200484 KB |
Output is correct |
21 |
Correct |
182 ms |
200456 KB |
Output is correct |
22 |
Correct |
182 ms |
200264 KB |
Output is correct |
23 |
Correct |
181 ms |
200272 KB |
Output is correct |
24 |
Correct |
183 ms |
200428 KB |
Output is correct |
25 |
Correct |
187 ms |
200528 KB |
Output is correct |
26 |
Correct |
205 ms |
200852 KB |
Output is correct |
27 |
Correct |
187 ms |
200400 KB |
Output is correct |
28 |
Correct |
183 ms |
200516 KB |
Output is correct |
29 |
Correct |
191 ms |
200700 KB |
Output is correct |
30 |
Correct |
184 ms |
200432 KB |
Output is correct |
31 |
Correct |
190 ms |
200456 KB |
Output is correct |
32 |
Correct |
196 ms |
200260 KB |
Output is correct |
33 |
Runtime error |
7 ms |
1920 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
540 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
452 KB |
Output is correct |
5 |
Correct |
198 ms |
200232 KB |
Output is correct |
6 |
Correct |
201 ms |
200156 KB |
Output is correct |
7 |
Correct |
182 ms |
200260 KB |
Output is correct |
8 |
Correct |
187 ms |
200304 KB |
Output is correct |
9 |
Correct |
188 ms |
200388 KB |
Output is correct |
10 |
Correct |
187 ms |
200312 KB |
Output is correct |
11 |
Correct |
184 ms |
200300 KB |
Output is correct |
12 |
Correct |
185 ms |
200768 KB |
Output is correct |
13 |
Correct |
182 ms |
200760 KB |
Output is correct |
14 |
Correct |
182 ms |
200516 KB |
Output is correct |
15 |
Correct |
186 ms |
200492 KB |
Output is correct |
16 |
Correct |
188 ms |
200312 KB |
Output is correct |
17 |
Correct |
186 ms |
200316 KB |
Output is correct |
18 |
Correct |
182 ms |
200304 KB |
Output is correct |
19 |
Correct |
197 ms |
200576 KB |
Output is correct |
20 |
Correct |
184 ms |
200484 KB |
Output is correct |
21 |
Correct |
182 ms |
200456 KB |
Output is correct |
22 |
Correct |
182 ms |
200264 KB |
Output is correct |
23 |
Correct |
181 ms |
200272 KB |
Output is correct |
24 |
Correct |
183 ms |
200428 KB |
Output is correct |
25 |
Correct |
187 ms |
200528 KB |
Output is correct |
26 |
Correct |
205 ms |
200852 KB |
Output is correct |
27 |
Correct |
187 ms |
200400 KB |
Output is correct |
28 |
Correct |
183 ms |
200516 KB |
Output is correct |
29 |
Correct |
191 ms |
200700 KB |
Output is correct |
30 |
Correct |
184 ms |
200432 KB |
Output is correct |
31 |
Correct |
190 ms |
200456 KB |
Output is correct |
32 |
Correct |
196 ms |
200260 KB |
Output is correct |
33 |
Runtime error |
7 ms |
1920 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |