/**
____ ____ ____ ____ ____ ____
||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)
{
for (int j = 1; j <= n; j ++)
{
dp[v][j] = c[v];
if (j == tag[h[v]])
dp[v][j] = 0;
}
used[v] = 1;
for (int u : adj[v])
{
dfs(u);
ll best = inf;
for (int j = n; j >= 1; j --)
{
best = min(best, dp[u][j]);
dp[v][j] += best;
}
}
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 |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
2632 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
368 ms |
197020 KB |
Output is correct |
6 |
Correct |
380 ms |
197156 KB |
Output is correct |
7 |
Correct |
364 ms |
196680 KB |
Output is correct |
8 |
Correct |
367 ms |
197280 KB |
Output is correct |
9 |
Correct |
398 ms |
196692 KB |
Output is correct |
10 |
Correct |
361 ms |
196692 KB |
Output is correct |
11 |
Correct |
329 ms |
196804 KB |
Output is correct |
12 |
Correct |
369 ms |
197456 KB |
Output is correct |
13 |
Correct |
479 ms |
197236 KB |
Output is correct |
14 |
Correct |
367 ms |
197716 KB |
Output is correct |
15 |
Correct |
374 ms |
197088 KB |
Output is correct |
16 |
Correct |
369 ms |
197028 KB |
Output is correct |
17 |
Correct |
380 ms |
196688 KB |
Output is correct |
18 |
Correct |
312 ms |
196944 KB |
Output is correct |
19 |
Correct |
366 ms |
197184 KB |
Output is correct |
20 |
Correct |
402 ms |
197016 KB |
Output is correct |
21 |
Correct |
359 ms |
196944 KB |
Output is correct |
22 |
Correct |
370 ms |
197056 KB |
Output is correct |
23 |
Correct |
378 ms |
196656 KB |
Output is correct |
24 |
Correct |
368 ms |
197204 KB |
Output is correct |
25 |
Correct |
380 ms |
196972 KB |
Output is correct |
26 |
Correct |
370 ms |
197460 KB |
Output is correct |
27 |
Correct |
369 ms |
196944 KB |
Output is correct |
28 |
Correct |
374 ms |
197412 KB |
Output is correct |
29 |
Correct |
367 ms |
197184 KB |
Output is correct |
30 |
Correct |
366 ms |
197132 KB |
Output is correct |
31 |
Correct |
366 ms |
197148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
2632 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
368 ms |
197020 KB |
Output is correct |
6 |
Correct |
380 ms |
197156 KB |
Output is correct |
7 |
Correct |
364 ms |
196680 KB |
Output is correct |
8 |
Correct |
367 ms |
197280 KB |
Output is correct |
9 |
Correct |
398 ms |
196692 KB |
Output is correct |
10 |
Correct |
361 ms |
196692 KB |
Output is correct |
11 |
Correct |
329 ms |
196804 KB |
Output is correct |
12 |
Correct |
369 ms |
197456 KB |
Output is correct |
13 |
Correct |
479 ms |
197236 KB |
Output is correct |
14 |
Correct |
367 ms |
197716 KB |
Output is correct |
15 |
Correct |
374 ms |
197088 KB |
Output is correct |
16 |
Correct |
369 ms |
197028 KB |
Output is correct |
17 |
Correct |
380 ms |
196688 KB |
Output is correct |
18 |
Correct |
312 ms |
196944 KB |
Output is correct |
19 |
Correct |
366 ms |
197184 KB |
Output is correct |
20 |
Correct |
402 ms |
197016 KB |
Output is correct |
21 |
Correct |
359 ms |
196944 KB |
Output is correct |
22 |
Correct |
370 ms |
197056 KB |
Output is correct |
23 |
Correct |
378 ms |
196656 KB |
Output is correct |
24 |
Correct |
368 ms |
197204 KB |
Output is correct |
25 |
Correct |
380 ms |
196972 KB |
Output is correct |
26 |
Correct |
370 ms |
197460 KB |
Output is correct |
27 |
Correct |
369 ms |
196944 KB |
Output is correct |
28 |
Correct |
374 ms |
197412 KB |
Output is correct |
29 |
Correct |
367 ms |
197184 KB |
Output is correct |
30 |
Correct |
366 ms |
197132 KB |
Output is correct |
31 |
Correct |
366 ms |
197148 KB |
Output is correct |
32 |
Correct |
369 ms |
197064 KB |
Output is correct |
33 |
Runtime error |
332 ms |
398440 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 |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
2632 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
368 ms |
197020 KB |
Output is correct |
6 |
Correct |
380 ms |
197156 KB |
Output is correct |
7 |
Correct |
364 ms |
196680 KB |
Output is correct |
8 |
Correct |
367 ms |
197280 KB |
Output is correct |
9 |
Correct |
398 ms |
196692 KB |
Output is correct |
10 |
Correct |
361 ms |
196692 KB |
Output is correct |
11 |
Correct |
329 ms |
196804 KB |
Output is correct |
12 |
Correct |
369 ms |
197456 KB |
Output is correct |
13 |
Correct |
479 ms |
197236 KB |
Output is correct |
14 |
Correct |
367 ms |
197716 KB |
Output is correct |
15 |
Correct |
374 ms |
197088 KB |
Output is correct |
16 |
Correct |
369 ms |
197028 KB |
Output is correct |
17 |
Correct |
380 ms |
196688 KB |
Output is correct |
18 |
Correct |
312 ms |
196944 KB |
Output is correct |
19 |
Correct |
366 ms |
197184 KB |
Output is correct |
20 |
Correct |
402 ms |
197016 KB |
Output is correct |
21 |
Correct |
359 ms |
196944 KB |
Output is correct |
22 |
Correct |
370 ms |
197056 KB |
Output is correct |
23 |
Correct |
378 ms |
196656 KB |
Output is correct |
24 |
Correct |
368 ms |
197204 KB |
Output is correct |
25 |
Correct |
380 ms |
196972 KB |
Output is correct |
26 |
Correct |
370 ms |
197460 KB |
Output is correct |
27 |
Correct |
369 ms |
196944 KB |
Output is correct |
28 |
Correct |
374 ms |
197412 KB |
Output is correct |
29 |
Correct |
367 ms |
197184 KB |
Output is correct |
30 |
Correct |
366 ms |
197132 KB |
Output is correct |
31 |
Correct |
366 ms |
197148 KB |
Output is correct |
32 |
Correct |
369 ms |
197064 KB |
Output is correct |
33 |
Runtime error |
332 ms |
398440 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |