Problem Statement

NOTE: This problem is the same as Treason II with different constraints.

You are given a connected graph with nn nodes and n1n - 1 edges.

Each node in your graph represents a biome in Minecraft. There are many biomes, and lots of them represent different seasons. We have simplified it such that there are only four seasons.

Each node in your graph has a single season associated with it (a number from 11 to 44).

Determine the longest distance between the two nodes with different seasons.

Input Format

Your first line will contain a single integer nn. Your next line will contain nn space-separated integers, the ithi^{th} integer represents the season of the ithi^{th} node. Your next n1n - 1 lines will contain two integers each, uu and vv denoting that uu has an undirected edge to vv. You are guaranteed these edges will form a connected graph.

Output Format

Output a single integer, the longest distance between two trees of different colour.

Constraints

  • 2n1032 \leq n \leq 10^3
  • 0u,v<n0 \leq u, v \lt n
  • You are guaranteed there are no repeated edges.
  • You are guaranteed the edges will form a connected graph.
  • You are guaranteed at least two nodes will have different colours.

Sample Cases

Input 1
6
1 2 1 2 1 1
0 1
1 2
1 3
2 4
4 5
Output 1
4

Here nodes 33 and 55 have different seasons - season 22 and season 11 respectively - and are distance 44 apart. This is the longest distance between any two nodes of different seasons.

Input 2
10
3 4 4 4 4 1 1 2 3 4
2 3
2 8
3 5
5 4
3 6
6 0
3 1
6 7
5 9
Output 2
4

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.