#K57807. Minimum Total Watering

    ID: 30502 Type: Default 1000ms 256MiB

Minimum Total Watering

Minimum Total Watering

You are given n plants. Each plant i has a water requirement of W_i and a water capacity of C_i.

Additionally, there are m pipes connecting the plants. Each pipe connects two plants and defines an undirected edge. The plants that are connected (directly or indirectly) form a connected component.

For each connected component, let \( R = \sum_{i \in component} W_i \) be the total water requirement and \( C = \sum_{i \in component} C_i \) be the total water capacity. The watering for that component is defined as \( \min(R, C) \). Your task is to compute the sum of the watering for all connected components, which represents the minimum total watering needed.

inputFormat

The first line contains an integer n (the number of plants).

The second line contains n space-separated integers representing the water requirements \(W_1, W_2, ..., W_n\).

The third line contains n space-separated integers representing the water capacities \(C_1, C_2, ..., C_n\).

The fourth line contains an integer m (the number of pipes).

The following m lines each contains two space-separated integers u and v, indicating that there is a pipe connecting plant u and plant v.

outputFormat

Output a single integer representing the minimum total watering needed.

## sample
5
10 20 30 40 50
15 25 35 45 55
4
1 2
1 3
2 4
2 5
150