/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 5010;
const ll inf = 1e18;
int n, a[maxn], h[maxn];
ll c[maxn];
vector < int > adj[maxn];
void input()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> a[i] >> h[i] >> c[i];
if (a[i] != i)
adj[a[i]].push_back(i);
}
}
unordered_map < int, int > tag;
void compress()
{
vector < int > values;
for (int i = 1; i <= n; i ++)
values.push_back(h[i]);
sort(values.begin(), values.end());
for (int i = 0; i < n; i ++)
tag[values[i]] = i + 1;
}
ll dp[maxn][maxn], temp[maxn];
int used[maxn];
void dfs(int v)
{
used[v] = 1;
for (int u : adj[v])
{
dfs(u);
for (int j = n; j >= 1; j --)
{
dp[v][j] += dp[u][j];
}
}
for (int j = 1; j <= n; j ++)
{
if (j != tag[h[v]])
dp[v][j] += c[v];
}
for (int j = n - 1; j > 0; j --)
{
dp[v][j] = min(dp[v][j], dp[v][j + 1]);
}
/**cout << "state " << v << " " << h[v] << endl;
for (int j = 1; j <= n; j ++)
cout << dp[v][j] << " ";
cout << endl;*/
}
void calculate_states()
{
ll ans = 0;
for (int i = 1; i <= n; i ++)
{
if (used[i])
continue;
dfs(i);
ll best = inf;
for (int j = 1; j <= n; j ++)
{
best = min(best, dp[i][j]);
}
ans += best;
}
cout << ans << endl;
}
void solve()
{
input();
compress();
calculate_states();
}
int main()
{
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
382 ms |
196948 KB |
Output is correct |
6 |
Correct |
370 ms |
196628 KB |
Output is correct |
7 |
Correct |
353 ms |
196808 KB |
Output is correct |
8 |
Correct |
357 ms |
196948 KB |
Output is correct |
9 |
Correct |
387 ms |
196856 KB |
Output is correct |
10 |
Correct |
356 ms |
196932 KB |
Output is correct |
11 |
Correct |
317 ms |
196624 KB |
Output is correct |
12 |
Correct |
354 ms |
197244 KB |
Output is correct |
13 |
Correct |
452 ms |
197164 KB |
Output is correct |
14 |
Correct |
354 ms |
197100 KB |
Output is correct |
15 |
Correct |
359 ms |
196940 KB |
Output is correct |
16 |
Correct |
358 ms |
196944 KB |
Output is correct |
17 |
Correct |
368 ms |
196644 KB |
Output is correct |
18 |
Correct |
298 ms |
196676 KB |
Output is correct |
19 |
Correct |
355 ms |
197108 KB |
Output is correct |
20 |
Correct |
384 ms |
196864 KB |
Output is correct |
21 |
Correct |
353 ms |
197040 KB |
Output is correct |
22 |
Correct |
366 ms |
196820 KB |
Output is correct |
23 |
Correct |
367 ms |
196880 KB |
Output is correct |
24 |
Correct |
357 ms |
197012 KB |
Output is correct |
25 |
Correct |
368 ms |
197144 KB |
Output is correct |
26 |
Correct |
357 ms |
197168 KB |
Output is correct |
27 |
Correct |
358 ms |
197512 KB |
Output is correct |
28 |
Correct |
355 ms |
197184 KB |
Output is correct |
29 |
Correct |
355 ms |
197460 KB |
Output is correct |
30 |
Correct |
351 ms |
196948 KB |
Output is correct |
31 |
Correct |
353 ms |
197064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
382 ms |
196948 KB |
Output is correct |
6 |
Correct |
370 ms |
196628 KB |
Output is correct |
7 |
Correct |
353 ms |
196808 KB |
Output is correct |
8 |
Correct |
357 ms |
196948 KB |
Output is correct |
9 |
Correct |
387 ms |
196856 KB |
Output is correct |
10 |
Correct |
356 ms |
196932 KB |
Output is correct |
11 |
Correct |
317 ms |
196624 KB |
Output is correct |
12 |
Correct |
354 ms |
197244 KB |
Output is correct |
13 |
Correct |
452 ms |
197164 KB |
Output is correct |
14 |
Correct |
354 ms |
197100 KB |
Output is correct |
15 |
Correct |
359 ms |
196940 KB |
Output is correct |
16 |
Correct |
358 ms |
196944 KB |
Output is correct |
17 |
Correct |
368 ms |
196644 KB |
Output is correct |
18 |
Correct |
298 ms |
196676 KB |
Output is correct |
19 |
Correct |
355 ms |
197108 KB |
Output is correct |
20 |
Correct |
384 ms |
196864 KB |
Output is correct |
21 |
Correct |
353 ms |
197040 KB |
Output is correct |
22 |
Correct |
366 ms |
196820 KB |
Output is correct |
23 |
Correct |
367 ms |
196880 KB |
Output is correct |
24 |
Correct |
357 ms |
197012 KB |
Output is correct |
25 |
Correct |
368 ms |
197144 KB |
Output is correct |
26 |
Correct |
357 ms |
197168 KB |
Output is correct |
27 |
Correct |
358 ms |
197512 KB |
Output is correct |
28 |
Correct |
355 ms |
197184 KB |
Output is correct |
29 |
Correct |
355 ms |
197460 KB |
Output is correct |
30 |
Correct |
351 ms |
196948 KB |
Output is correct |
31 |
Correct |
353 ms |
197064 KB |
Output is correct |
32 |
Correct |
359 ms |
196948 KB |
Output is correct |
33 |
Runtime error |
333 ms |
398464 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
382 ms |
196948 KB |
Output is correct |
6 |
Correct |
370 ms |
196628 KB |
Output is correct |
7 |
Correct |
353 ms |
196808 KB |
Output is correct |
8 |
Correct |
357 ms |
196948 KB |
Output is correct |
9 |
Correct |
387 ms |
196856 KB |
Output is correct |
10 |
Correct |
356 ms |
196932 KB |
Output is correct |
11 |
Correct |
317 ms |
196624 KB |
Output is correct |
12 |
Correct |
354 ms |
197244 KB |
Output is correct |
13 |
Correct |
452 ms |
197164 KB |
Output is correct |
14 |
Correct |
354 ms |
197100 KB |
Output is correct |
15 |
Correct |
359 ms |
196940 KB |
Output is correct |
16 |
Correct |
358 ms |
196944 KB |
Output is correct |
17 |
Correct |
368 ms |
196644 KB |
Output is correct |
18 |
Correct |
298 ms |
196676 KB |
Output is correct |
19 |
Correct |
355 ms |
197108 KB |
Output is correct |
20 |
Correct |
384 ms |
196864 KB |
Output is correct |
21 |
Correct |
353 ms |
197040 KB |
Output is correct |
22 |
Correct |
366 ms |
196820 KB |
Output is correct |
23 |
Correct |
367 ms |
196880 KB |
Output is correct |
24 |
Correct |
357 ms |
197012 KB |
Output is correct |
25 |
Correct |
368 ms |
197144 KB |
Output is correct |
26 |
Correct |
357 ms |
197168 KB |
Output is correct |
27 |
Correct |
358 ms |
197512 KB |
Output is correct |
28 |
Correct |
355 ms |
197184 KB |
Output is correct |
29 |
Correct |
355 ms |
197460 KB |
Output is correct |
30 |
Correct |
351 ms |
196948 KB |
Output is correct |
31 |
Correct |
353 ms |
197064 KB |
Output is correct |
32 |
Correct |
359 ms |
196948 KB |
Output is correct |
33 |
Runtime error |
333 ms |
398464 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |