#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 200000 + 10;
const int INF = 1e9;
int n;
int d[MAXN];
int dp[MAXN];
std::vector <int> g[MAXN];
void getDist(int node, int p, int root)
{
d[node] = d[p] + 1;
for (const int &u : g[node])
{
if (u == p || u > root)
{
continue;
}
getDist(u, node, root);
}
}
void solve()
{
dp[1] = 0;
for (int i = 2 ; i <= n ; ++i)
{
std::fill(d + 1, d + 1 + n, -INF); d[0] = -1;
getDist(i, 0, i);
for (int j = i - 1 ; j >= 1 ; --j)
{
dp[i] = std::max(dp[i], dp[j] + d[j]);
}
}
std::cout << dp[n] << '\n';
}
void input()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> d[i];
}
for (int i = 2 ; i <= n ; ++i)
{
int u, v;
std::cin >> u >> v;
g[d[u]].push_back(d[v]);
g[d[v]].push_back(d[u]);
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
5032 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
5032 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
5032 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
109 ms |
5572 KB |
Output is correct |
19 |
Correct |
98 ms |
5452 KB |
Output is correct |
20 |
Correct |
97 ms |
5508 KB |
Output is correct |
21 |
Correct |
25 ms |
5452 KB |
Output is correct |
22 |
Correct |
24 ms |
5408 KB |
Output is correct |
23 |
Correct |
25 ms |
5400 KB |
Output is correct |
24 |
Correct |
25 ms |
5420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
5032 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
109 ms |
5572 KB |
Output is correct |
19 |
Correct |
98 ms |
5452 KB |
Output is correct |
20 |
Correct |
97 ms |
5508 KB |
Output is correct |
21 |
Correct |
25 ms |
5452 KB |
Output is correct |
22 |
Correct |
24 ms |
5408 KB |
Output is correct |
23 |
Correct |
25 ms |
5400 KB |
Output is correct |
24 |
Correct |
25 ms |
5420 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
81 ms |
5368 KB |
Output is correct |
27 |
Correct |
77 ms |
5480 KB |
Output is correct |
28 |
Correct |
78 ms |
5416 KB |
Output is correct |
29 |
Correct |
92 ms |
5424 KB |
Output is correct |
30 |
Correct |
29 ms |
5204 KB |
Output is correct |
31 |
Correct |
33 ms |
5304 KB |
Output is correct |
32 |
Correct |
34 ms |
5292 KB |
Output is correct |
33 |
Correct |
32 ms |
5236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
5032 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
109 ms |
5572 KB |
Output is correct |
19 |
Correct |
98 ms |
5452 KB |
Output is correct |
20 |
Correct |
97 ms |
5508 KB |
Output is correct |
21 |
Correct |
25 ms |
5452 KB |
Output is correct |
22 |
Correct |
24 ms |
5408 KB |
Output is correct |
23 |
Correct |
25 ms |
5400 KB |
Output is correct |
24 |
Correct |
25 ms |
5420 KB |
Output is correct |
25 |
Execution timed out |
2062 ms |
16604 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Execution timed out |
2048 ms |
15856 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4964 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
5032 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
109 ms |
5572 KB |
Output is correct |
19 |
Correct |
98 ms |
5452 KB |
Output is correct |
20 |
Correct |
97 ms |
5508 KB |
Output is correct |
21 |
Correct |
25 ms |
5452 KB |
Output is correct |
22 |
Correct |
24 ms |
5408 KB |
Output is correct |
23 |
Correct |
25 ms |
5400 KB |
Output is correct |
24 |
Correct |
25 ms |
5420 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
81 ms |
5368 KB |
Output is correct |
27 |
Correct |
77 ms |
5480 KB |
Output is correct |
28 |
Correct |
78 ms |
5416 KB |
Output is correct |
29 |
Correct |
92 ms |
5424 KB |
Output is correct |
30 |
Correct |
29 ms |
5204 KB |
Output is correct |
31 |
Correct |
33 ms |
5304 KB |
Output is correct |
32 |
Correct |
34 ms |
5292 KB |
Output is correct |
33 |
Correct |
32 ms |
5236 KB |
Output is correct |
34 |
Execution timed out |
2062 ms |
16604 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |