{"problem":{"name":"Attack to a Tree","description":{"content":"The server in company A has a structure where $N$ devices numbered $1, 2, ..., N$ are connected with $N - 1$ cables. The $i$\\-th cable connects Device $U_i$ and Device $V_i$. Any two different devices","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"aising2019_e"},"statements":[{"statement_type":"Markdown","content":"The server in company A has a structure where $N$ devices numbered $1, 2, ..., N$ are connected with $N - 1$ cables. The $i$\\-th cable connects Device $U_i$ and Device $V_i$. Any two different devices are connected through some number of cables.\nEach device $v$ ($1 \\leq v \\leq N$) has a non-zero integer $A_v$, which represents the following:\n\n*   If $A_v < 0$, Device $v$ is a computer that consumes an electric power of $-A_v$.\n*   If $A_v > 0$, Device $v$ is a battery that supplies an electric power of $A_v$.\n\nYou have decided to disconnect some number of cables (possibly zero) to disable the server. When some cables are disconnected, the devices will be divided into some number of connected components. The server will be disabled if all of these connected components satisfy one of the following conditions:\n\n*   There is no computer in the connected component. That is, $A_v$ is positive for every device $v$ that belongs to the connected component.\n*   There is not enough supply of electric power in the connected component. That is, the sum of $A_v$ over all devices $v$ that belong to the connected component is negative.\n\nAt least how many cables do you need to disconnect in order to disable the server?\n\n## Constraints\n\n*   $1 \\leq N \\leq 5$ $000$\n*   $1 \\leq |A_i| \\leq 10^9$ ($1 \\leq i \\leq N$)\n*   $1 \\leq U_i, V_i \\leq N$ ($1 \\leq i \\leq N - 1$)\n*   $U_i \\neq V_i$ ($1 \\leq i \\leq N - 1$)\n*   Any two different devices are connected through some number of cables.\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_1$ $A_2$ $...$ $A_N$\n$U_1$ $V_1$\n$U_2$ $V_2$\n$:$\n$U_{N - 1}$ $V_{N - 1}$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"aising2019_e","tags":[],"sample_group":[["7\n-2 7 5 6 -8 3 4\n1 2\n2 3\n2 4\n1 5\n5 6\n5 7","1\n\nWe should disconnect the cable connecting Device $1$ and Device $2$."],["4\n1 2 3 4\n1 2\n1 3\n1 4","0"],["6\n10 -1 10 -1 10 -1\n1 2\n2 3\n3 4\n4 5\n5 6","5"],["8\n-2 3 6 -2 -2 -5 3 2\n3 4\n7 6\n6 2\n8 2\n5 3\n1 8\n3 7","3"],["10\n3 4 9 6 1 5 -1 10 -10 -10\n7 4\n5 6\n8 1\n9 5\n7 1\n10 3\n2 8\n4 10\n9 2","3"]],"created_at":"2026-03-03 11:01:14"}}