Arpa's weak amphitheater and Mehrdad's valuable Hoses
Contest: Codeforces Round 383 (Div. 1)
Difficulty rating: 1600
Tags: dfs and similar, dp, dsu

Just to remind, girls in Arpa's land are really nice.

Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y.

Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.

Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.

Input format:
The first line contains integers n, m and w (1  ≤  n  ≤  1000, $$0 \leq m \leq \min\left(\frac{n(n-1)}{2}, 10^5\right)$$, 1 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.

The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 1000) — the weights of the Hoses.

The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 106) — the beauties of the Hoses.

The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.

Output format:
Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.

In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.

Example input:
3 1 5
3 2 5
2 4 2
1 2

Example output:
6

Example input:
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3

Example output:
7

Editorial:
It’s a simple knapsack problem. Let’s solve this version of knapsack problem first: we have n sets of items, each item has value and weight, find the maximum value we can earn if we can choose at most one item from each set and the sum of the chosen items must be less than or equal to W. Let dpw be the max value we can earn if the sum of weights of chosen items is less than or equal to w. Now iterate on sets one by one and update dp as follows: for each item X, and for each weight w, newDpw = max(newDpw, oldDpw - X.weight + X.value).
Run dfs and find groups at first. The problem is same with above problem, each group is some set in above problem, just add the whole group as an item to the set that related to this group.
Time complexity: $${ \mathcal { O } } ( n \cdot W )$$.