Orig Description
Score : 600 points
Problem StatementThe 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.
Each device v (1 \leq v \leq N) has a non-zero integer A_v, which represents the following:
If A_v < 0, Device v is a computer that consumes an electric power of -A_v.
If A_v > 0, Device v is a battery that supplies an electric power of A_v.
You 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:
There is no computer in the connected component. That is, A_v is positive for every device v that belongs to the connected component.
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.
At least how many cables do you need to disconnect in order to disable the server?
Constraints
1 \leq N \leq 5 000
1 \leq |A_i| \leq 10^9 (1 \leq i \leq N)
1 \leq U_i, V_i \leq N (1 \leq i \leq N - 1)
U_i \neq V_i (1 \leq i \leq N - 1)
Any two different devices are connected through some number of cables.
All values in input are integers.
InputInput is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
U_1 V_1
U_2 V_2
:
U_{N - 1} V_{N - 1}
OutputPrint the answer.
Sample Input 17
-2 7 5 6 -8 3 4
1 2
2 3
2 4
1 5
5 6
5 7
Sample Output 11
We should disconnect the cable connecting Device 1 and Device 2.
Sample Input 24
1 2 3 4
1 2
1 3
1 4
Sample Output 20
Sample Input 36
10 -1 10 -1 10 -1
1 2
2 3
3 4
4 5
5 6
Sample Output 35
Sample Input 48
-2 3 6 -2 -2 -5 3 2
3 4
7 6
6 2
8 2
5 3
1 8
3 7
Sample Output 43
Sample Input 510
3 4 9 6 1 5 -1 10 -10 -10
7 4
5 6
8 1
9 5
7 1
10 3
2 8
4 10
9 2
Sample Output 53
Extracted Specification
An integer \(N\) (1 ≤ \(N\) ≤ 5000), representing some quantity or size.
A list of \(N\) integers \(A\), where each integer is between -10^9 and 10^9.
A list of \(N-1\) pairs of integers \((U_i, V_i)\), where each integer in the pair is between 1 and \(N\).
### Example Input:
```
7
-2 7 5 6 -8 3 4
1 2
2 3
2 4
1 5
5 6
5 7
```
### Function Signature:
Write a function `f(N, A, edges)` that takes in the input.
```python
def f(N: int, A: List[int], edges: List[Tuple[int, int]]):
'''
N: an integer
A: a list of integers
edges: a list of tuples of integers
'''
```
Test Case Generator
Sure, I can help you write a test case generator for the given problem. The generator will create random values for \(N\), the array \(A\), and the cables connecting the devices. Here is a Python function to generate test cases for the problem statement:
```python
import random
def tcgen():
# Generate N
N = random.randint(1, 5000)
# Generate the array A with non-zero integers between -10^9 and 10^9
A = [random.randint(-10**9, 10**9) for _ in range(N)]
# Ensure no zero values in A
A = [x if x != 0 else 1 for x in A]
# Generate the cables
cables = []
available_nodes = [i + 1 for i in range(N)]
random.shuffle(available_nodes)
for i in range(1, N):
u = available_nodes[i]
v = available_nodes[random.randint(0, i-1)]
cables.append((u, v))
return N, A, cables
# Example of generating a test case
N, A, cables = tcgen()
print(N)
print(" ".join(map(str, A)))
for u, v in cables:
print(u, v)
```
### Explanation
1. **Generating \(N\)**: Randomly choose \(N\) between 1 and 5000.
2. **Generating the array \(A\)**: Create an array of \(N\) integers where each integer is a non-zero value between -10^9 and 10^9.
3. **Generating the cables**:
- Create a list of node indices from 1 to \(N\).
- Shuffle the list to randomize the order of nodes.
- Connect each node to a random previous node to ensure the structure remains a valid tree.
This generator ensures that the test cases adhere to the constraints and structure defined in the problem statement. You can run the `tcgen` function to generate multiple test cases for testing purposes.
Extract Arguments
def extract_arguments(fh):
N = int(fh.readline().strip())
A = list(map(int, fh.readline().strip().split()))
cables = []
for _ in range(N - 1):
U, V = map(int, fh.readline().strip().split())
cables.append((U, V))
return N, A, cables