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 w_{i} and some beauty b_{i}. 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 a_1, a_2, ..., a_{k} such that a_{i} and a_{i} + 1 are friends for each 1 ≤ i < k, and a_1 = x and a_{k} = y.

 [Image] 

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-----

The first line contains integers n, m and w (1  ≤  n  ≤  1000, $0 \leq m \leq \operatorname{min}(\frac{n \cdot(n - 1)}{2}, 10^{5})$, 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 w_1, w_2, ..., w_{n} (1 ≤ w_{i} ≤ 1000) — the weights of the Hoses.

The third line contains n integers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 10^6) — the beauties of the Hoses.

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


-----Output-----

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.


-----Examples-----
Input
3 1 5
3 2 5
2 4 2
1 2

Output
6

Input
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3

Output
7



-----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.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Spanning trees

Dynamic programming

Graph traversal


Solutions:
(n, m, k) = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
ab = [(0, 0)] + list(zip(a, b))
l = list(range(n + 1))

def f(x):
	if x == l[x]:
		return x
	l[x] = f(l[x])
	return l[x]
for i in range(m):
	(x, y) = map(int, input().split())
	(x, y) = (f(x), f(y))
	if x != y:
		l[y] = x
p = [[] for j in range(n + 1)]
for i in range(1, n + 1):
	p[f(i)].append(i)
r = (k + 1) * [0]
r[0] = 1
for i in p:
	if len(i) > 1:
		l = [ab[x] for x in i]
		x0 = sum((x[0] for x in l))
		x1 = sum((x[1] for x in l))
		l.append((x0, x1))
		l.sort()
		for j in range(k, -1, -1):
			if r[j]:
				for (w, b) in l:
					if j + w > k:
						break
					r[j + w] = max(r[j + w], r[j] + b)
	elif len(i) == 1:
		(w, b) = ab[i[0]]
		for j in range(k - w, -1, -1):
			if r[j]:
				r[j + w] = max(r[j + w], r[j] + b)
res = max(r) - 1
print(res)


from sys import stdin, stdout
from collections import defaultdict as dd
(read, write) = (stdin.readline, stdout.write)

class DisjointSetUnion:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n

	def find(self, a):
		acopy = a
		while a != self.parent[a]:
			a = self.parent[a]
		while acopy != a:
			(self.parent[acopy], acopy) = (a, self.parent[acopy])
		return a

	def union(self, a, b):
		(a, b) = (self.find(a), self.find(b))
		if a != b:
			if self.size[a] < self.size[b]:
				(a, b) = (b, a)
			self.num_sets -= 1
			self.parent[b] = a
			self.size[a] += self.size[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets
(n, m, w) = map(int, read().split())
weights = list(map(int, read().split()))
beauties = list(map(int, read().split()))
DSU = DisjointSetUnion(n)
for _ in range(m):
	(h1, h2) = map(int, read().split())
	DSU.union(h1 - 1, h2 - 1)
groups = dd(list)
for i in range(n):
	DSU.find(i)
for (hose, parent) in enumerate(DSU.parent):
	groups[parent].append(hose)
dp = [0] * (w + 1)
for friends in groups.values():
	dp_aux = dp[:]
	group_weight = group_beauty = 0
	for friend in friends:
		(f_weight, f_beauty) = (weights[friend], beauties[friend])
		group_weight += f_weight
		group_beauty += f_beauty
		for weight in range(f_weight, w + 1):
			dp[weight] = max(dp[weight], dp_aux[weight - f_weight] + f_beauty)
	for weight in range(group_weight, w + 1):
		dp[weight] = max(dp[weight], dp_aux[weight - group_weight] + group_beauty)
print(dp[-1])


def g(x):
	if x == t[x]:
		return x
	t[x] = g(t[x])
	return t[x]
f = lambda : map(int, input().split())
(hoses, pairOfFriends, weight) = f()
weightsAndBeauties = [(0, 0)] + list(zip(f(), f()))
t = list(range(hoses + 1))
for i in range(pairOfFriends):
	(f1, f2) = f()
	(f1, f2) = (g(f1), g(f2))
	if f1 != f2:
		t[f2] = f1
p = [[] for j in range(hoses + 1)]
for i in range(1, hoses + 1):
	p[g(i)].append(i)
beauties = [1] + [0] * weight
for q in p:
	if len(q) > 1:
		t = [weightsAndBeauties[i] for i in q]
		t.append((sum((f1[0] for f1 in t)), sum((f1[1] for f1 in t))))
		t.sort(key=lambda f1: f1[0])
		for j in range(weight, -1, -1):
			if beauties[j]:
				for (w, b) in t:
					if j + w > weight:
						break
					else:
						beauties[j + w] = max(beauties[j + w], beauties[j] + b)
	elif len(q) == 1:
		(w, b) = weightsAndBeauties[q[0]]
		for j in range(weight - w, -1, -1):
			if beauties[j]:
				beauties[j + w] = max(beauties[j + w], beauties[j] + b)
maxBeauty = max(beauties) - 1
print(maxBeauty)


f = lambda : map(int, input().split())
(n, m, s) = f()
wb = [(0, 0)] + list(zip(f(), f()))
t = list(range(n + 1))

def g(x):
	if x == t[x]:
		return x
	t[x] = g(t[x])
	return t[x]
for i in range(m):
	(x, y) = f()
	(x, y) = (g(x), g(y))
	if x != y:
		t[y] = x
p = [[] for j in range(n + 1)]
for i in range(1, n + 1):
	p[g(i)].append(i)
d = [1] + [0] * s
for q in p:
	if len(q) > 1:
		t = [wb[i] for i in q]
		t.append((sum((x[0] for x in t)), sum((x[1] for x in t))))
		t.sort(key=lambda x: x[0])
		for j in range(s, -1, -1):
			if d[j]:
				for (w, b) in t:
					if j + w > s:
						break
					d[j + w] = max(d[j + w], d[j] + b)
	elif len(q) == 1:
		(w, b) = wb[q[0]]
		for j in range(s - w, -1, -1):
			if d[j]:
				d[j + w] = max(d[j + w], d[j] + b)
print(max(d) - 1)


(n, m, wn) = map(int, input().split())
Wi = [(0, 0)] + list(zip(map(int, input().split()), map(int, input().split())))
listaux3 = list(range(n + 1))

def aux(x):
	if x == listaux3[x]:
		return x
	listaux3[x] = aux(listaux3[x])
	return listaux3[x]
for i in range(m):
	(x, y) = map(int, input().split())
	(x, y) = (aux(x), aux(y))
	if x != y:
		listaux3[y] = x
listaux1 = [[] for _ in range(n + 1)]
for i in range(1, n + 1):
	listaux1[aux(i)].append(i)
listaux2 = [1] + [0] * wn
for k in listaux1:
	if len(k) > 1:
		listaux3 = [Wi[i] for i in k]
		listaux3.append((sum((x[0] for x in listaux3)), sum((x[1] for x in listaux3))))
		listaux3.sort(key=lambda x: x[0])
		for j in range(wn, -1, -1):
			if listaux2[j]:
				for (w, b) in listaux3:
					if j + w > wn:
						break
					listaux2[j + w] = max(listaux2[j + w], listaux2[j] + b)
	elif len(k) == 1:
		(w, b) = Wi[k[0]]
		for j in range(wn - w, -1, -1):
			if listaux2[j]:
				listaux2[j + w] = max(listaux2[j + w], listaux2[j] + b)
print(max(listaux2) - 1)


(n, m, o) = map(int, input().split())
wn = map(int, input().split())
bn = map(int, input().split())
wb = [(0, 0)] + list(zip(wn, bn))
l = list(range(n + 1))

def f(x):
	if x == l[x]:
		return x
	l[x] = f(l[x])
	return l[x]
for i in range(m):
	(x, y) = map(int, input().split())
	(x, y) = (f(x), f(y))
	if x != y:
		l[y] = x
p = [[] for j in range(n + 1)]
for i in range(1, n + 1):
	p[f(i)].append(i)
r = (o + 1) * [0]
r[0] = 1
for i in p:
	if len(i) > 1:
		l = [wb[x] for x in i]
		x0 = sum((x[0] for x in l))
		x1 = sum((x[1] for x in l))
		l.append((x0, x1))
		l.sort()
		for j in range(o, -1, -1):
			if r[j]:
				for (w, b) in l:
					if j + w > o:
						break
					r[j + w] = max(r[j + w], r[j] + b)
	elif len(i) == 1:
		(w, b) = wb[i[0]]
		for j in range(o - w, -1, -1):
			if r[j]:
				r[j + w] = max(r[j + w], r[j] + b)
res = max(r) - 1
print(res)


R = lambda : map(int, input().split())
(n, m, w) = R()
ws = [0] + list(R())
bs = [0] + list(R())
g = [[] for x in range(n + 1)]
for i in range(m):
	(x, y) = R()
	g[x].append(y)
	g[y].append(x)
cs = [0] * (n + 1)
cnt = 1
for i in range(1, n + 1):
	if not cs[i]:
		cs[i] = cnt
		q = []
		q.append(i)
		while q:
			nxt = q.pop()
			for x in g[nxt]:
				if not cs[x]:
					cs[x] = cnt
					q.append(x)
		cnt += 1
gs = [[] for i in range(cnt)]
for i in range(1, n + 1):
	gs[cs[i]].append(i)
dp = [[0] * (w + 1) for i in range(cnt)]
for i in range(1, cnt):
	tw = sum((ws[k] for k in gs[i]))
	tb = sum((bs[k] for k in gs[i]))
	for j in range(1, w + 1):
		dp[i][j] = max(dp[i][j], dp[i - 1][j], dp[i - 1][j - tw] + tb if j >= tw else 0)
		for k in gs[i]:
			dp[i][j] = max(dp[i][j], dp[i - 1][j], dp[i - 1][j - ws[k]] + bs[k] if j >= ws[k] else 0)
print(dp[-1][w])


f = lambda : map(int, input().split())
(n, m, w) = f()
wb = [(0, 0)] + list(zip(f(), f()))
t = list(range(n + 1))

def g(x):
	if x == t[x]:
		return x
	t[x] = g(t[x])
	return t[x]
for i in range(m):
	(x, y) = f()
	(x, y) = (g(x), g(y))
	if x != y:
		t[y] = x
p = [[] for j in range(n + 1)]
for i in range(1, n + 1):
	p[g(i)].append(i)
d = [1] + [0] * w
for q in p:
	if len(q) > 1:
		WB = [wb[i] for i in q]
		SW = sum((q[0] for q in WB))
		SB = sum((q[1] for q in WB))
		for D in range(w, -1, -1):
			if d[D]:
				if D + SW <= w:
					d[D + SW] = max(d[D + SW], d[D] + SB)
				for (W, B) in WB:
					if D + W <= w:
						d[D + W] = max(d[D + W], d[D] + B)
	elif len(q) == 1:
		(W, B) = wb[q[0]]
		for D in range(w - W, -1, -1):
			if d[D]:
				d[D + W] = max(d[D + W], d[D] + B)
print(max(d) - 1)


R = lambda : map(int, input().split())
(n, m, w) = R()
ws = list(R())
bs = list(R())
anc = [-1] * n

def get(x):
	if anc[x] < 0:
		return x
	anc[x] = get(anc[x])
	return anc[x]

def join(x1, x2):
	(x1, x2) = (get(x1), get(x2))
	if x1 != x2:
		anc[x1] = x2
for i in range(m):
	(x1, x2) = R()
	join(x1 - 1, x2 - 1)
gs = [list() for i in range(n)]
for i in range(n):
	gs[get(i)].append(i)
gs = [x for x in gs if x]
dp = [[0] * (w + 1) for i in range(len(gs) + 1)]
for i in range(len(gs)):
	tw = sum((ws[k] for k in gs[i]))
	tb = sum((bs[k] for k in gs[i]))
	for j in range(w + 1):
		dp[i][j] = max(dp[i][j], dp[i - 1][j], dp[i - 1][j - tw] + tb if j >= tw else 0)
		for k in gs[i]:
			dp[i][j] = max(dp[i][j], dp[i - 1][j], dp[i - 1][j - ws[k]] + bs[k] if j >= ws[k] else 0)
print(dp[len(gs) - 1][w])


maxn = 1002

def criar_lista_com_zeros(n):
	l = []
	for i in range(n):
		l.append(0)
	return l

def criar_lista_de_listas(n):
	l = []
	for i in range(n):
		l.append([])
	return l
memoria = criar_lista_com_zeros(maxn)
pesos = criar_lista_com_zeros(maxn)
belezas = criar_lista_com_zeros(maxn)
somas_pesos = criar_lista_com_zeros(maxn)
somas_belezas = criar_lista_com_zeros(maxn)
vt = criar_lista_de_listas(maxn)
pr = criar_lista_com_zeros(maxn)
(n, m, w) = (0, 0, 0)

def encontrar(x):
	global pr
	if pr[x] == x:
		return x
	pr[x] = encontrar(pr[x])
	return pr[x]

def juntar(x, y):
	f1 = encontrar(x)
	f2 = encontrar(y)
	if f1 != f2:
		pr[f1] = f2
(n, m, w) = [int(x) for x in input().split()]
for i in range(maxn):
	memoria[i] = 0
	pr[i] = i
	somas_pesos[i] = 0
	somas_belezas[i] = 0
pesos = [0] + [int(x) for x in input().split()]
belezas = [0] + [int(x) for x in input().split()]
for i in range(1, m + 1):
	(x, y) = [int(x) for x in input().split()]
	juntar(x, y)
for i in range(1, n + 1):
	x = encontrar(i)
	vt[x].append((pesos[i], belezas[i]))
	somas_pesos[x] += pesos[i]
	somas_belezas[x] += belezas[i]
for i in range(1, n + 1):
	if len(vt[i]) == 0:
		continue
	vt[i].append((somas_pesos[i], somas_belezas[i]))
for i in range(1, n + 1):
	if len(vt[i]) == 0:
		continue
	for j in range(w, -1, -1):
		for k in range(0, len(vt[i])):
			W = vt[i][k][0]
			B = vt[i][k][1]
			if j - W >= 0:
				memoria[j] = max(memoria[j], memoria[j - W] + B)
print('%d' % memoria[w])


class disjoint:

	def __init__(self, n):
		self.rank = [1] * n
		self.parent = [i for i in range(n)]

	def find(self, x):
		if self.parent[x] != x:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, x, y):
		xid = self.find(x)
		yid = self.find(y)
		if xid == yid:
			return
		if self.rank[xid] < self.rank[yid]:
			self.parent[xid] = yid
			return
		elif self.rank[xid] > self.rank[yid]:
			self.parent[yid] = xid
			return
		else:
			self.parent[yid] = xid
			self.rank[xid] += 1
			return
import sys
input = sys.stdin.readline
(n, m, w) = map(int, input().split())
obj = disjoint(n)
z = list(map(int, input().split()))
be = list(map(int, input().split()))
ans = []
for i in range(len(z)):
	ans.append([i, z[i], be[i]])
for i in range(m):
	(p, q) = map(int, input().split())
	obj.union(p - 1, q - 1)
from collections import *
al = defaultdict(list)
for i in range(len(ans)):
	y = obj.find(ans[i][0])
	al[y].append(ans[i][0])
dp = [[0 for i in range(len(al) + 1)] for i in range(w + 1)]
c1 = 0
maxa = 0
for i in al:
	if c1 == 0:
		q = al[i]
		total = 0
		bea = 0
		for x in q:
			wei = ans[x][1]
			if wei <= w:
				dp[wei][c1] = max(dp[wei][c1], ans[x][2])
				maxa = max(maxa, dp[wei][c1])
			total += wei
			bea += ans[x][2]
		if total <= w:
			dp[total][c1] = max(dp[total][c1], bea)
			maxa = max(maxa, dp[total][c1])
		c1 += 1
	else:
		q = al[i]
		total = 0
		dil = 0
		bea = 0
		wei = 0
		for x in q:
			wei = ans[x][1]
			bea = ans[x][2]
			total += wei
			dil += bea
			for j in range(w + 1):
				if wei > j:
					dp[j][c1] = max(dp[j][c1 - 1], dp[j][c1])
					maxa = max(maxa, dp[j][c1])
					continue
				else:
					dp[j][c1] = max(dp[j][c1], dp[j - wei][c1 - 1] + bea, dp[j][c1 - 1])
					maxa = max(maxa, dp[j][c1])
		if total <= w:
			dp[total][c1] = max(dp[total][c1 - 1], dil, dp[total][c1])
			maxa = max(maxa, dp[total][c1])
			for j in range(w + 1):
				if j <= total:
					continue
				else:
					dp[j][c1] = max(dp[j][c1], dil + dp[j - total][c1 - 1])
					maxa = max(maxa, dp[j][c1])
		c1 += 1
print(maxa)


import math, sys, bisect, heapq, os
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
from functools import lru_cache
int1 = lambda x: int(x) - 1

def input():
	return sys.stdin.readline().rstrip('\r\n')
aj = lambda : list(map(int, input().split()))

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])

def solve():
	G = defaultdict(list)

	def addEdge(a, b):
		G[a].append(b)
		G[b].append(a)

	def dfs(node):
		d = deque()
		d.append(node)
		vis[node] = True
		T = []
		S = []
		tot = 0
		wt = 0
		while d:
			x = d.pop()
			S.append(B[x - 1])
			T.append(W[x - 1])
			for i in G.get(x, []):
				if not vis[i]:
					vis[i] = True
					d.append(i)
		return (S, T)
	(n, m, w) = aj()
	W = aj()
	B = aj()
	vis = [False] * (n + 1)
	for i in range(m):
		(u, v) = aj()
		addEdge(u, v)
	A1 = []
	A2 = []
	Id = 1
	for i in range(1, n + 1):
		if not vis[i]:
			(c, d) = dfs(i)
			A1.append(c)
			A2.append(d)
	dp = [[0] * (w + 1) for i in range(len(A1) + 1)]
	for i in range(len(A1)):
		weight_sum = sum((x for x in A2[i]))
		beauty_sum = sum((x for x in A1[i]))
		for j in range(w + 1):
			dp[i][j] = max(beauty_sum + dp[i - 1][j - weight_sum] if weight_sum <= j else 0, dp[i - 1][j])
			for k in range(len(A1[i])):
				dp[i][j] = max(dp[i][j], dp[i - 1][j - A2[i][k]] + A1[i][k] if A2[i][k] <= j else 0)
	print(dp[len(A1) - 1][w])
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
solve()


def ler():
	return [int(x) for x in input().split()]

def dfs(u, adj, visited, s, Pesos, Belezas):
	visited[u] = True
	total_p = Pesos[u]
	total_b = Belezas[u]
	s.append(u)
	for v in adj[u]:
		if not visited[v]:
			(w, b) = dfs(v, adj, visited, s, Pesos, Belezas)
			total_p += w
			total_b += b
	return (total_p, total_b)
(n, m, w) = ler()
Pesos = ler()
Belezas = ler()
adj = [[] for _ in range(n)]
for _ in range(m):
	(x, y) = ler()
	x -= 1
	y -= 1
	adj[x].append(y)
	adj[y].append(x)
visited = [False] * n
f = [0] * (w + 1)
for i in range(n):
	if visited[i]:
		continue
	s = []
	(total_p, total_b) = dfs(i, adj, visited, s, Pesos, Belezas)
	for j in range(w, -1, -1):
		jw = j + total_p
		if jw <= w:
			f[jw] = max(f[jw], f[j] + total_b)
		for v in s:
			jw = j + Pesos[v]
			if jw <= w:
				f[jw] = max(f[jw], f[j] + Belezas[v])
print(f[w])


def inp():
	return [int(x) for x in input().split()]

def dfs(u, adj, visited, s, W, B):
	visited[u] = True
	total_w = W[u]
	total_b = B[u]
	s.append(u)
	for v in adj[u]:
		if not visited[v]:
			(w, b) = dfs(v, adj, visited, s, W, B)
			total_w += w
			total_b += b
	return (total_w, total_b)

def main():
	(n, m, w) = inp()
	W = inp()
	B = inp()
	adj = [[] for _ in range(n)]
	for _ in range(m):
		(x, y) = inp()
		x -= 1
		y -= 1
		adj[x].append(y)
		adj[y].append(x)
	visited = [False] * n
	f = [0] * (w + 1)
	for i in range(n):
		if visited[i]:
			continue
		s = []
		(total_w, total_b) = dfs(i, adj, visited, s, W, B)
		for j in range(w, -1, -1):
			jw = j + total_w
			if jw <= w:
				f[jw] = max(f[jw], f[j] + total_b)
			for v in s:
				jw = j + W[v]
				if jw <= w:
					f[jw] = max(f[jw], f[j] + B[v])
	print(f[w])

def __starting_point():
	main()
__starting_point()


(hoses, pairs, total_weight) = map(int, input().split())
weight_arr = list(map(int, input().split()))
beauty_arr = list(map(int, input().split()))
arr = [-1] * hoses

def get(hose):
	if arr[hose] < 0:
		return hose
	arr[hose] = get(arr[hose])
	return arr[hose]

def join(left, right):
	(left, right) = (get(left), get(right))
	if left != right:
		arr[left] = right
for i in range(pairs):
	(left, right) = map(int, input().split())
	join(left - 1, right - 1)
groups = [list() for i in range(hoses)]
for i in range(hoses):
	groups[get(i)].append(i)
groups = [group for group in groups if group]
dp = [[0] * (total_weight + 1) for i in range(len(groups) + 1)]
for i in range(len(groups)):
	weight_sum = sum((weight_arr[x] for x in groups[i]))
	beauty_sum = sum((beauty_arr[x] for x in groups[i]))
	for j in range(total_weight + 1):
		dp[i][j] = max(beauty_sum + dp[i - 1][j - weight_sum] if weight_sum <= j else 0, dp[i - 1][j])
		for k in groups[i]:
			dp[i][j] = max(dp[i][j], dp[i - 1][j - weight_arr[k]] + beauty_arr[k] if weight_arr[k] <= j else 0)
print(dp[len(groups) - 1][total_weight])


from collections import defaultdict

class UnionFind:

	def __init__(self, n):
		self.n = n
		self.parents = [-1] * n

	def find(self, x):
		if self.parents[x] < 0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self, x, y):
		x = self.find(x)
		y = self.find(y)
		if x == y:
			return
		if self.parents[x] > self.parents[y]:
			(x, y) = (y, x)
		self.parents[x] += self.parents[y]
		self.parents[y] = x

	def size(self, x):
		return -self.parents[self.find(x)]

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def members(self, x):
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i) == root]

	def roots(self):
		return [i for (i, x) in enumerate(self.parents) if x < 0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		group_members = defaultdict(list)
		for member in range(self.n):
			group_members[self.find(member)].append(member)
		return group_members
(n, m, w) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
uf = UnionFind(n)
for _ in range(m):
	(x, y) = map(int, input().split())
	uf.union(x - 1, y - 1)
l = uf.roots()
ll = len(l)
dp = [[-10 ** 18] * (w + 1) for i in range(ll)]
(ca, cb) = (0, 0)
dp[0][0] = 0
for x in uf.members(l[0]):
	if a[x] <= w:
		dp[0][a[x]] = max(dp[0][a[x]], b[x])
	ca += a[x]
	cb += b[x]
if ca <= w:
	dp[0][ca] = max(dp[0][ca], cb)
for i in range(1, ll):
	(ca, cb) = (0, 0)
	for x in uf.members(l[i]):
		for j in range(w + 1):
			dp[i][j] = max(dp[i][j], dp[i - 1][j])
			if j - a[x] >= 0:
				dp[i][j] = max(dp[i][j], dp[i - 1][j - a[x]] + b[x])
		ca += a[x]
		cb += b[x]
	for j in range(w + 1):
		if j - ca >= 0:
			dp[i][j] = max(dp[i][j], dp[i - 1][j], dp[i - 1][j - ca] + cb)
ans = 0
for j in range(w + 1):
	ans = max(ans, dp[ll - 1][j])
print(ans)