{"problem":{"name":"Game on Tree 3","description":{"content":"There is a rooted tree with $N$ vertices, Vertex $1$ being the root. For each $i = 1, 2, \\ldots, N-1$, the $i$\\-th edge connects Vertex $u_i$ and Vertex $v_i$. Each vertex other than the root has a po","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":6000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc246_g"},"statements":[{"statement_type":"Markdown","content":"There is a rooted tree with $N$ vertices, Vertex $1$ being the root. For each $i = 1, 2, \\ldots, N-1$, the $i$\\-th edge connects Vertex $u_i$ and Vertex $v_i$. Each vertex other than the root has a positive integer written on it: for each $i = 2, 3, \\ldots, N$, the integer written on Vertex $i$ is $A_i$. Takahashi and Aoki will use this rooted tree and a piece to play the following game against each other.\nThe piece starts on Vertex $1$. Until the game ends, they repeat the following procedure.\n\n1.  First, Aoki chooses a non-root vertex and replaces the integer written on that vertex with $0$.\n2.  Next, Takahashi moves the piece to a (direct) child of the vertex the piece is on.\n3.  Then, the game ends if the piece is on a leaf. Even if that is not the case, Takahashi can choose to end the game immediately.\n\nAt the end of the game, Takahashi's score will be the integer written at that time on the vertex the piece is on. Takahashi wants to make his score as large as possible, while Aoki wants to make it as small as possible. Print the score Takahashi will get when both players play optimally for their respective purposes.\n\n## Constraints\n\n*   $2 \\leq N \\leq 2 \\times 10^5$\n*   $1 \\leq A_i \\leq 10^9$\n*   $1 \\leq u_i, v_i \\leq N$\n*   The given graph is a tree.\n*   All values in input are integers.\n\n## Input\n\nInput is given from Standard Input in the following format:\n\n$N$\n$A_2$ $\\ldots$ $A_N$\n$u_1$ $v_1$\n$u_2$ $v_2$\n$\\vdots$\n$u_{N-1}$ $v_{N-1}$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc246_g","tags":[],"sample_group":[["7\n2 4 6 5 6 10\n1 2\n1 3\n2 4\n2 5\n5 6\n5 7","5\n\nHere is a possible progression of the game when both players play optimally.\n\n1.  The piece starts on Vertex $1$.\n2.  Aoki changes the integer written on Vertex $7$ from $10$ to $0$.\n3.  Takahashi moves the piece from Vertex $1$ to Vertex $2$.\n4.  Aoki changes the integer written on Vertex $4$ from $6$ to $0$.\n5.  Takahashi moves the piece from Vertex $2$ to Vertex $5$.\n6.  Takahashi chooses to end the game.\n\nAt the end of the game, the piece is on Vertex $5$, on which the integer $5$ is written at that time, so Takahashi's score will be $5$."],["30\n29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62 32 38 84 49 93 53 26 13 25\n13 15\n14 22\n17 24\n12 3\n4 3\n5 8\n26 15\n3 2\n2 9\n4 25\n4 13\n2 10\n28 15\n6 4\n2 5\n19 9\n2 7\n2 14\n23 30\n17 2\n7 16\n21 13\n13 23\n13 20\n1 2\n6 18\n27 6\n21 29\n11 8","70"]],"created_at":"2026-03-03 11:01:13"}}