#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
stack<int> st;
vector<vector<int>> G, NG;
int w[1002], w_team[10002], b[1002], b_team[1002];
bool seen1[1002];
void dfs1(int u, int id) {
    seen1[u] = true;
    w_team[id] += w[u];
    b_team[id] += b[u];
    NG[id].push_back(u);
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!seen1[v]) {
            dfs1(v, id);
        }
    }
}

int dp[1002][1002];

int main() {
    ios_base::sync_with_stdio(0);
    //cin.tie(0);
    int n, m, W; cin >> n >> m >> W;

    for(int i = 0; i < n; i++) cin >> w[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    G = vector<vector<int>> (n+2);
    for(int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int groups = 0;
    for(int i = 0; i < n; i++) {
        if(!seen1[i]) {
            NG.push_back(vector<int>());
            dfs1(i, groups++);
//            cout << w_team[groups-1] << "-" << b_team[groups-1] << endl;;
        }
    }

    int ans = 0;
    for(int i = 0; i < groups; i++) {
        for(int j = 0; j <= W; j++) {
            if(i) dp[i][j] = dp[i-1][j];
            if(j - w_team[i] >= 0) {
                if(i) dp[i][j] = max(dp[i][j], dp[i-1][j-w_team[i]]+b_team[i]);
                else dp[i][j] = max(dp[i][j], b_team[i]);
            }
            for(auto &x : NG[i]) {
                if(j - w[x] >= 0) {
                    if(i) dp[i][j] = max(dp[i][j], dp[i-1][j-w[x]]+b[x]);
                    else dp[i][j] = max(dp[i][j], b[x]);
                }
            }
//            cout << dp[i][j] << " ";
        }
//        cout << endl;
    }

    cout << dp[groups-1][W];

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
stack<int> st;
vector<vector<int>> G;
int team[1002], w[1002], w_team[10002], b[1002], b_team[1002];
bool seen1[1002], seen2[1002];
pair<int, int> dfs1(int u, int id) {
    seen1[u] = true;
    team[u] = id;
    pair<int, int> ret = make_pair(w[u], b[u]);
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!seen1[v]) {
            pair<int, int> tmp = dfs1(v, id);
            ret.first += tmp.first;
            ret.second += tmp.second;
        }
    }
    return ret;
}

void dfs2(int u, int nw, int nb) {
    seen2[u] = true;
    w_team[u] = nw;
    b_team[u] = nb;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!seen2[v]) {
            dfs2(v, nw, nb);
        }
    }
}

int dp[1002][1002];

int main() {
    ios_base::sync_with_stdio(0);
    //cin.tie(0);
    int n, m, W; cin >> n >> m >> W;

    for(int i = 0; i < n; i++) cin >> w[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    G = vector<vector<int>> (n+2);
    for(int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int id = 0;
    for(int i = 0; i < n; i++) {
        if(!seen1[i]) {
            pair<int, int> tmp = dfs1(i, id++);
            dfs2(i, tmp.first, tmp.second);
        }
    }

    int ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= W; j++) {
            if(j-w[i] >= 0) dp[i][j] = max(dp[i][j], b[i]);
            if(j-w_team[i] >= 0) dp[i][j] = max(dp[i][j], b_team[i]);
//            cout << b[i] << " " << b_team[i] << " " << dp[i][j] << endl;
            for(int k = 0; k < i; k++) {
                if(j - w[k] >= 0) {
                    if(team[k] == team[i])
                        dp[i][j] = max(dp[i][j], max(dp[k][j-w[k]], b[i]) );
                    else
                        dp[i][j] = max(dp[i][j], dp[k][j-w[k]]+b[i] );
                }
                if(j - w_team[k] >= 0){
                    if(team[k] == team[i])
                        dp[i][j] = max(dp[i][j], max(dp[k][j-w_team[k]], b_team[i]) );
                    else
                        dp[i][j] = max(dp[i][j], dp[k][j-w_team[k]]+b_team[i] );
                }
            }
            ans = max(ans, dp[i][j]);
//            cout << dp[i][j] << " ";
        }
//        cout << endl;
    }

    cout << ans;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
stack<int> st;
vector<vector<int>> G;
int team[1002], w[1002], w_team[10002], b[1002], b_team[1002];
bool seen1[1002], seen2[1002];
pair<int, int> dfs1(int u, int id) {
    seen1[u] = true;
    team[u] = id;
    pair<int, int> ret = make_pair(w[u], b[u]);
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!seen1[v]) {
            pair<int, int> tmp = dfs1(v, id);
            ret.first += tmp.first;
            ret.second += tmp.second;
        }
    }
    return ret;
}

void dfs2(int u, int nw, int nb) {
    seen2[u] = true;
    w_team[u] = nw;
    b_team[u] = nb;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(!seen2[v]) {
            dfs2(v, nw, nb);
        }
    }
}

int dp[1002][1002];

int main() {
    ios_base::sync_with_stdio(0);
    //cin.tie(0);
    int n, m, W; cin >> n >> m >> W;

    for(int i = 0; i < n; i++) cin >> w[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    G = vector<vector<int>> (n+2);
    for(int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int id = 0;
    for(int i = 0; i < n; i++) {
        if(!seen1[i]) {
            pair<int, int> tmp = dfs1(i, id++);
            dfs2(i, tmp.first, tmp.second);
        }
    }

    int ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= W; j++) {
//            if(j-w[i] >= 0) dp[i][j] = max(dp[i][j], b[i]);
//            if(j-w_team[i] >= 0) dp[i][j] = max(dp[i][j], b_team[i]);
//            cout << b[i] << " " << b_team[i] << " " << dp[i][j] << endl;
            for(int k = 0; k < i; k++) {
                if(j - w[k] >= 0) {
                    if(team[k] == team[i])
                        dp[i][j] = max(dp[i][j], max(dp[k][j-w[k]], b[i]) );
                    else
                        dp[i][j] = max(dp[i][j], dp[k][j-w[k]]+b[i] );
                }
                if(j - w_team[k] >= 0){
                    if(team[k] == team[i])
                        dp[i][j] = max(dp[i][j], max(dp[k][j-w_team[k]], b_team[i]) );
                    else
                        dp[i][j] = max(dp[i][j], dp[k][j-w_team[k]]+b_team[i] );
                }
            }
            ans = max(ans, dp[i][j]);
//            cout << dp[i][j] << " ";
        }
//        cout << endl;
    }

    cout << ans;

}


//Arpa's weak amphitheater and Mehrdad's valuable Hoses
# include <bits/stdc++.h>
using namespace std;

# define  mod 	     1000000007
# define  ll	     long long
# define  INF	     (1<<30)
# define  mp         make_pair
# define  pb         push_back
# define  fill(a,v)  memset(a, v, sizeof a)

ll N,M,W;
	pair <ll,ll> arr[1004];
	vector < ll >groups[1004];
	ll dp[1004][1004];
	bool vis[1004];
	vector <ll> connect[1004];
	ll groupnum=0;

void dfs(ll node)
{
	ll i,j;
	if(vis[node]==1)
		return ;
	groups[groupnum].pb(node);
	vis[node]=1;
	for(i=0;i<connect[node].size();i++)
		dfs(connect[node][i]);
}

ll fun(ll groupid,ll current_weight)
{
	ll i,j;
	if(groupid==groupnum)
		return 0;

	if(dp[groupid][current_weight]!=-1)
		return dp[groupid][current_weight];

	ll temp=fun(groupid+1,current_weight);

	ll beautytot=0;
	ll weighttot=0;

	for(i=0;i<groups[groupid].size();i++)
		{
			ll node=groups[groupid][i];
			beautytot+=arr[node].second;
			weighttot+=arr[node].first;
			if(current_weight+arr[node].first<=W)
			temp=max(temp,arr[node].second+fun(groupid+1,current_weight+arr[node].first));
		}
	if(weighttot+current_weight<=W)
		temp=max(temp,beautytot+fun(groupid+1,current_weight+weighttot));
	return temp;
}
int main()
{

	ll i,j;
	fill(dp,-1);
	fill(vis,0);

	scanf("%lld%lld%lld",&N,&M,&W);
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].first=X;
		}
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].second=X;
		}
	while(M--)
	{
		ll A,B;
		scanf("%lld%lld",&A,&B);
		connect[A].pb(B);
		connect[B].pb(A);
	}

	for(i=1;i<=N;i++)
	{
		if(!vis[i])
			dfs(i);
		groupnum++;
	}

	printf("%lld\n",fun(0,0));
return 0;
}


//Arpa's weak amphitheater and Mehrdad's valuable Hoses
# include <bits/stdc++.h>
using namespace std;

# define  mod 	     1000000007
# define  ll	     long long
# define  INF	     (1<<30)
# define  mp         make_pair
# define  pb         push_back
# define  fill(a,v)  memset(a, v, sizeof a)

ll N,M,W;
	pair <ll,ll> arr[1004];
	vector < ll >groups[1004];
	ll dp[1004][1004];
	bool vis[1004];
	vector <ll> connect[1004];
	ll groupnum=0;

void dfs(ll node)
{
	ll i,j;

	groups[groupnum].pb(node);
	vis[node]=1;
	for(i=0;i<connect[node].size();i++)
	{
		if(vis[connect[node][i]]==0)
		dfs(connect[node][i]);
	}
}

ll fun(ll groupid,ll current_weight)
{
	ll i,j;
	if(groupid==groupnum || current_weight==W)
		return 0;

	if(dp[groupid][current_weight]!=-1)
		return dp[groupid][current_weight];

	ll temp=fun(groupid+1,current_weight);

	ll beautytot=0;
	ll weighttot=0;

	for(i=0;i<groups[groupid].size();i++)
		{
			ll node=groups[groupid][i];
			beautytot+=arr[node].second;
			weighttot+=arr[node].first;
			temp=max(temp,arr[node].second+fun(groupid+1,current_weight+arr[node].first));
		}
	if(weighttot+current_weight<=W)
		temp=max(temp,beautytot+fun(groupid+1,current_weight+weighttot));
	return temp;
}
int main()
{

	ll i,j;
	fill(dp,-1);
	fill(vis,0);

	scanf("%lld%lld%lld",&N,&M,&W);
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].first=X;
		}
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].second=X;
		}
	for(j=1;j<=M;j++)
	{
		ll A,B;
		scanf("%lld%lld",&A,&B);
		connect[A].pb(B);
		connect[B].pb(A);
	}

	for(i=1;i<=N;i++)
	{
		if(!vis[i])
			dfs(i);
		groupnum++;
	}

	printf("%lld\n",fun(0,0));
return 0;
}


//Arpa's weak amphitheater and Mehrdad's valuable Hoses
# include <bits/stdc++.h>
using namespace std;

# define  mod 	     1000000007
# define  ll	     long long
# define  INF	     (1<<30)
# define  mp         make_pair
# define  pb         push_back
# define  fill(a,v)  memset(a, v, sizeof a)

ll N,M,W;
	pair <ll,ll> arr[1004];
	vector < ll >groups[1004];
	ll dp[1004][1004];
	bool vis[1004];
	vector <ll> connect[1004];
	ll groupnum=0;

void dfs(ll node)
{
	ll i,j;

	groups[groupnum].pb(node);
	vis[node]=1;
	for(i=0;i<connect[node].size();i++)
	{
		if(vis[connect[node][i]]==0)
		dfs(connect[node][i]);
	}
}

ll fun(ll groupid,ll current_weight)
{
	ll i,j;
	if(groupid==groupnum || current_weight==W)
		return 0;

	if(current_weight > W)
		return (-1)*INF;
	if(dp[groupid][current_weight]!=-1)
		return dp[groupid][current_weight];

	ll temp=fun(groupid+1,current_weight);

	ll beautytot=0;
	ll weighttot=0;

	for(i=0;i<groups[groupid].size();i++)
		{
			ll node=groups[groupid][i];
			beautytot+=arr[node].second;
			weighttot+=arr[node].first;

			temp=max(temp,arr[node].second+fun(groupid+1,current_weight+arr[node].first));
		}

		temp=max(temp,beautytot+fun(groupid+1,current_weight+weighttot));
	return temp;
}
int main()
{

	ll i,j;
	fill(dp,-1);
	fill(vis,0);

	scanf("%lld%lld%lld",&N,&M,&W);
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].first=X;
		}
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].second=X;
		}
	while(M--)
	{
		ll A,B;
		scanf("%lld%lld",&A,&B);
		connect[A].pb(B);
		connect[B].pb(A);
	}

	for(i=1;i<=N;i++)
	{
		if(!vis[i])
			dfs(i);
		groupnum++;
	}

	printf("%lld\n",fun(0,0));
return 0;
}


//Arpa's weak amphitheater and Mehrdad's valuable Hoses
# include <bits/stdc++.h>
using namespace std;

# define  mod 	     1000000007
# define  ll	     long long
# define  INF	     (1<<30)
# define  mp         make_pair
# define  pb         push_back
# define  fill(a,v)  memset(a, v, sizeof a)

ll N,M,W;
	pair <ll,ll> arr[1004];
	vector < ll >groups[1004];
	ll dp[1004][1004];
	bool vis[1004];
	vector <ll> connect[1004];
	ll groupnum=0;

void dfs(ll node)
{
	ll i,j;
	if(vis[node]==1)
		return ;
	groups[groupnum].pb(node);
	vis[node]=1;
	for(i=0;i<connect[node].size();i++)
		dfs(connect[node][i]);
}

ll fun(ll groupid,ll current_weight)
{
	ll i,j;
	if(groupid==groupnum)
		return 0;

	if(dp[groupid][current_weight]!=-1)
		return dp[groupid][current_weight];

	ll temp=fun(groupid+1,current_weight);

	ll beautytot=0;
	ll weighttot=0;

	for(i=0;i<groups[groupid].size();i++)
		{
			ll node=groups[groupid][i];
			beautytot+=arr[node].second;
			weighttot+=arr[node].first;
			if(current_weight+arr[node].first<=W)
			temp=max(temp,arr[node].second+fun(groupid+1,current_weight+arr[node].first));
		}
	if(weighttot+current_weight<=W)
		temp=max(temp,beautytot+fun(groupid+1,current_weight+weighttot));
	return dp[groupid][current_weight]=temp;
}
int main()
{

	ll i,j;
	fill(dp,-1);
	fill(vis,0);

	scanf("%lld%lld%lld",&N,&M,&W);
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].first=X;
		}
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].second=X;
		}
	while(M--)
	{
		ll A,B;
		scanf("%lld%lld",&A,&B);
		connect[A].pb(B);
		connect[B].pb(A);
	}

	for(i=1;i<=N;i++)
	{
		if(!vis[i])
			dfs(i);
		groupnum++;
	}

	printf("%lld\n",fun(0,0));
return 0;
}


//Arpa's weak amphitheater and Mehrdad's valuable Hoses
# include <bits/stdc++.h>
using namespace std;

# define  mod 	     1000000007
# define  ll	     long long
# define  INF	     (1<<30)
# define  mp         make_pair
# define  pb         push_back
# define  fill(a,v)  memset(a, v, sizeof a)

ll N,M,W;
	pair <ll,ll> arr[1004];
	vector < ll >groups[1004];
	ll dp[1004][1004];
	bool vis[1004];
	vector <ll> connect[1004];
	ll groupnum=0;

void dfs(ll node)
{
	ll i,j;

	groups[groupnum].pb(node);
	vis[node]=1;
	for(i=0;i<connect[node].size();i++)
	{
		if(vis[connect[node][i]]==0)
		dfs(connect[node][i]);
	}
}

ll fun(ll groupid,ll current_weight)
{
	ll i,j;
	if(groupid==groupnum)
		return 0;

	if(dp[groupid][current_weight]!=-1)
		return dp[groupid][current_weight];

	ll temp=fun(groupid+1,current_weight);

	ll beautytot=0;
	ll weighttot=0;

	for(i=0;i<groups[groupid].size();i++)
		{
			ll node=groups[groupid][i];
			beautytot+=arr[node].second;
			weighttot+=arr[node].first;
			if(current_weight+arr[node].first<=W)
			temp=max(temp,arr[node].second + fun(groupid+1,current_weight+arr[node].first));
		}
	if(weighttot+current_weight<=W)
		temp=max(temp,beautytot+fun(groupid+1,current_weight+weighttot));
	return dp[groupid][current_weight]=temp;
}
int main()
{

	ll i,j;
	fill(dp,-1);
	fill(vis,0);

	scanf("%lld%lld%lld",&N,&M,&W);
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].first=X;
		}
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].second=X;
		}
	for(i=0;i<M;i++)
	{
		ll A,B;
		scanf("%lld%lld",&A,&B);
		connect[A].pb(B);
		connect[B].pb(A);
	}

	for(i=1;i<=N;i++)
	{
		if(!vis[i])
			dfs(i);
		groupnum++;
	}

	printf("%lld\n",fun(0,0));
return 0;
}


//Arpa's weak amphitheater and Mehrdad's valuable Hoses
# include <bits/stdc++.h>
using namespace std;

# define  mod 	     1000000007
# define  ll	     long long
# define  INF	     (1<<30)
# define  mp         make_pair
# define  pb         push_back
# define  fill(a,v)  memset(a, v, sizeof a)

ll N,M,W;
	pair <ll,ll> arr[1004];
	vector < ll >groups[1004];
	ll dp[1004][1004];
	bool vis[1004];
	vector <ll> connect[1004];
	ll groupnum=0;

void dfs(ll node)
{
	ll i,j;

	groups[groupnum].pb(node);
	vis[node]=1;
	for(i=0;i<connect[node].size();i++)
	{
		if(vis[connect[node][i]]==0)
		dfs(connect[node][i]);
	}
}

ll fun(ll groupid,ll current_weight)
{
	ll i,j;
	if(groupid==groupnum)
		return 0;

	if(dp[groupid][current_weight]!=-1)
		return dp[groupid][current_weight];

	ll temp=fun(groupid+1,current_weight);

	ll beautytot=0;
	ll weighttot=0;

	for(i=0;i<groups[groupid].size();i++)
		{
			ll node=groups[groupid][i];
			beautytot+=arr[node].second;
			weighttot+=arr[node].first;
			if(current_weight+arr[node].first<=W)
			temp=max(temp,arr[node].second+fun(groupid+1,current_weight+arr[node].first));
		}
	if(weighttot+current_weight<=W)
		temp=max(temp,beautytot+fun(groupid+1,current_weight+weighttot));
	return temp;
}
int main()
{

	ll i,j;
	fill(dp,-1);
	fill(vis,0);

	scanf("%lld%lld%lld",&N,&M,&W);
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].first=X;
		}
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].second=X;
		}
	while(M--)
	{
		ll A,B;
		scanf("%lld%lld",&A,&B);
		connect[A].pb(B);
		connect[B].pb(A);
	}

	for(i=1;i<=N;i++)
	{
		if(!vis[i])
			dfs(i);
		groupnum++;
	}

	printf("%lld\n",fun(0,0));
return 0;
}


//Arpa's weak amphitheater and Mehrdad's valuable Hoses
# include <bits/stdc++.h>
using namespace std;

# define  mod 	     1000000007
# define  ll	     long long
# define  INF	     (1<<30)
# define  mp         make_pair
# define  pb         push_back
# define  fill(a,v)  memset(a, v, sizeof a)

ll N,M,W;
	pair <ll,ll> arr[1004];
	vector < ll >groups[1004];
	ll dp[1004][1004];
	bool vis[1004];
	vector <ll> connect[1004];
	ll groupnum=0;

void dfs(ll node)
{
	ll i,j;

	groups[groupnum].pb(node);
	vis[node]=1;
	for(i=0;i<connect[node].size();i++)
	{
		if(vis[connect[node][i]]==0)
		dfs(connect[node][i]);
	}
}

ll fun(ll groupid,ll current_weight)
{
	ll i,j;
	if(groupid==groupnum || current_weight==W)
		return 0;

	if(dp[groupid][current_weight]!=-1)
		return dp[groupid][current_weight];

	ll temp=fun(groupid+1,current_weight);

	ll beautytot=0;
	ll weighttot=0;

	for(i=0;i<groups[groupid].size();i++)
		{
			ll node=groups[groupid][i];
			beautytot+=arr[node].second;
			weighttot+=arr[node].first;
			if(current_weight+arr[node].first<=W)
			temp=max(temp,arr[node].second+fun(groupid+1,current_weight+arr[node].first));
		}
	if(weighttot+current_weight<=W)
		temp=max(temp,beautytot+fun(groupid+1,current_weight+weighttot));
	return temp;
}
int main()
{

	ll i,j;
	fill(dp,-1);
	fill(vis,0);

	scanf("%lld%lld%lld",&N,&M,&W);
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].first=X;
		}
	for (i=1;i<=N; ++i)
		{
			ll X;
			scanf("%lld",&X);
			arr[i].second=X;
		}
	while(M--)
	{
		ll A,B;
		scanf("%lld%lld",&A,&B);
		connect[A].pb(B);
		connect[B].pb(A);
	}

	for(i=1;i<=N;i++)
	{
		if(!vis[i])
			dfs(i);
		groupnum++;
	}

	printf("%lld\n",fun(0,0));
return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,w,p[maxn],v[maxn],b[maxn],now;
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}
ll dp[2][maxn],v1,b1,ma;
int main()
{
    int i,j;
    scanf("%d%d%d",&n,&m,&w);
    rep(i,0,n+1)p[i]=i;
    rep(i,1,n)scanf("%d",&v[i]);
    rep(i,1,n)scanf("%d",&b[i]);
    while(m--)scanf("%d%d",&j,&k),p[find(j)]=find(k);
    rep(i,1,n+1)
    {
        if(i==1||find(i)==find(i-1))
        {
            v1+=v[i],b1+=b[i];
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
        }
        else
        {
            for(j=w;j>=v1;j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v1]+b1),ma=max(ma,dp[now][j]);
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
            v1=b1=0;
            v1+=v[i],b1+=b[i];
            now^=1;
            for(j=0;j<=w;j++)dp[now][j]=dp[now^1][j];
        }
    }
    printf("%lld\n",ma);
    //system("Pause");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e3+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,w,p[maxn],v[maxn],b[maxn],now,cnt,pq[maxn][maxn];
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}
ll dp[2][maxn],v1,b1,ma;
int main()
{
    int i,j;
    scanf("%d%d%d",&n,&m,&w);
    rep(i,0,n+1)p[i]=i;
    rep(i,1,n)scanf("%d",&v[i]);
    rep(i,1,n)scanf("%d",&b[i]);
    while(m--){scanf("%d%d",&j,&k);pq[min(j,k)][max(j,k)]=1;}
    rep(i,1,n)
    {
        if(pq[i][i+1])
        {
            v1+=v[i],b1+=b[i];
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
        }
        else
        {
            v1+=v[i],b1+=b[i];
            for(j=w;j>=v1;j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v1]+b1),ma=max(ma,dp[now][j]);
            v1=b1=0;
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
            now^=1;
            for(j=0;j<=w;j++)dp[now][j]=dp[now^1][j]=max(dp[now][j],dp[now^1][j]);
        }
    }
    printf("%lld\n",ma);
    //system("Pause");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e3+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,w,v[maxn],b[maxn],now;
bool vis[maxn];
ll dp[2][maxn],v1,b1;
vi e[maxn];
void dfs(int p)
{
    vis[p]=true;
    v1+=v[p],b1+=b[p];
    for(int i=w;i>=v[p];i--)dp[now][i]=max(dp[now][i],dp[now^1][i-v[p]]+b[p]);
    for(int x:e[p])
    {
        if(!vis[x])
        {
            dfs(x);
        }
    }
}
int main()
{
    int i,j;
    scanf("%d%d%d",&n,&m,&w);
    rep(i,1,n)scanf("%d",&v[i]);
    rep(i,1,n)scanf("%d",&b[i]);
    rep(i,1,m)scanf("%d%d",&j,&k),e[j].pb(k),e[k].pb(j);
    rep(i,1,n)
    {
        if(!vis[i])
        {
            now^=1;
            v1=b1=0;
            rep(j,0,w)dp[now][j]=dp[now^1][j];
            dfs(i);
            for(j=w;j>=v1;j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v1]+b1);
        }
    }
    printf("%lld\n",dp[now][w]);
    //system("Pause");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,w,p[maxn],v[maxn],b[maxn],now;
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}
ll dp[2][maxn],v1,b1,ma;
int main()
{
    int i,j;
    scanf("%d%d%d",&n,&m,&w);
    rep(i,0,n+1)p[i]=i;
    rep(i,1,n)scanf("%d",&v[i]);
    rep(i,1,n)scanf("%d",&b[i]);
    while(m--)scanf("%d%d",&j,&k),p[find(j)]=find(k);
    rep(i,1,n+1)
    {
        if(i==1||find(i)==find(i-1))
        {
            v1+=v[i],b1+=b[i];
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
        }
        else
        {
            for(j=w;j>=v1;j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v1]+b1),ma=max(ma,dp[now][j]);
            v1=b1=0;
            v1+=v[i],b1+=b[i];
            now^=1;
            for(j=0;j<=w;j++)dp[now][j]=dp[now^1][j];
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
        }
    }
    printf("%lld\n",ma);
    //system("Pause");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,w,p[maxn],v[maxn],b[maxn],now,cnt;
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}
ll dp[2][maxn],v1,b1,ma;
int main()
{
    int i,j;
    scanf("%d%d%d",&n,&m,&w);
    rep(i,0,n+1)p[i]=i;
    rep(i,1,n)scanf("%d",&v[i]);
    rep(i,1,n)scanf("%d",&b[i]);
    while(m--)scanf("%d%d",&j,&k),p[find(j)]=find(k);
    rep(i,1,n+1)
    {
        if(i==1||find(i)==find(i-1))
        {
            v1+=v[i],b1+=b[i],cnt++;
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
        }
        else
        {
            if(cnt)for(j=w;j>=v1;j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v1]+b1),ma=max(ma,dp[now][j]);
            v1=b1=cnt=0;
            v1+=v[i],b1+=b[i];
            now^=1;
            for(j=0;j<=w;j++)dp[now][j]=dp[now^1][j]=max(dp[now][j],dp[now^1][j]);
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
        }
    }
    printf("%lld\n",ma);
    //system("Pause");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,w,p[maxn],v[maxn],b[maxn],now,cnt;
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}
ll dp[2][maxn],v1,b1,ma;
int main()
{
    int i,j;
    scanf("%d%d%d",&n,&m,&w);
    rep(i,0,n+1)p[i]=i;
    rep(i,1,n)scanf("%d",&v[i]);
    rep(i,1,n)scanf("%d",&b[i]);
    while(m--)scanf("%d%d",&j,&k),p[find(j)]=find(k);
    rep(i,1,n)
    {
        if(find(i+1)==find(i))
        {
            v1+=v[i],b1+=b[i];
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
        }
        else
        {
            v1+=v[i],b1+=b[i];
            for(j=w;j>=v1;j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v1]+b1),ma=max(ma,dp[now][j]);
            v1=b1=0;
            for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
            now^=1;
            for(j=0;j<=w;j++)dp[now][j]=dp[now^1][j]=max(dp[now][j],dp[now^1][j]);
        }
    }
    printf("%lld\n",ma);
    //system("Pause");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,k,t,w,p[maxn],v[maxn],b[maxn],now;
int find(int x)
{
    return p[x]==x?x:p[x]=find(p[x]);
}
ll dp[2][maxn],v1,b1,ma;
int main()
{
    int i,j;
    scanf("%d%d%d",&n,&m,&w);
    rep(i,0,n+1)p[i]=i;
    rep(i,1,n)v[i]=read();
    rep(i,1,n)b[i]=read();
    while(m--)j=read(),k=read(),p[find(j)]=find(k);
    rep(i,1,n+1)
    {
        if(find(i)==find(i-1))
        {
            v1+=v[i],b1+=b[i];
        }
        else
        {
            for(j=w;j>=v1;j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v1]+b1),ma=max(ma,dp[now][j]);
            v1=b1=0;
            v1+=v[i],b1+=b[i];
            now^=1;
            for(j=0;j<=w;j++)dp[now][j]=dp[now^1][j];
        }
        for(j=w;j>=v[i];j--)dp[now][j]=max(dp[now][j],dp[now^1][j-v[i]]+b[i]),ma=max(ma,dp[now][j]);
    }
    printf("%lld\n",ma);
    //system("Pause");
    return 0;
}


#include <algorithm>
#include <iostream>
#include <set>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// Useful globals
ll N, M, W;
vector<ll> weights;
vector<ll> bs;

// Useful utility methods
template<class T>
static inline void cin_vector(ull N, vector<T> & v) {
  for (ull i = 0; i < N ; i++) {
    T a;
    cin >> a;
    v.push_back(a);
  }
}

template<class T>
T gcd(T a, T b) {
  if (b > a) {
    swap(a, b);
  }
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

// dp[n][w] : best attainable, constrained by weight <= w, and
// only using the first n friendship groups.
ll dp[1010][1010];

// main procedure
int main() {
  ios::sync_with_stdio(false);
  cout.precision(8);

  // your code here
  cin >> N >> M >> W;
  bs.push_back(0);
  weights.push_back(0);
  cin_vector(N, weights);
  cin_vector(N, bs);
  set<pair<ll, ll>> fs;
  vector<vector<ll>> groups;

  for (ll i = 0 ; i < M ; i++) {
    ll x, y;
    cin >> x >> y;
    if (x > y) {
      swap(x, y);
    }
    fs.insert(make_pair(x, y));
  }

  // construct friendship groups
  vector<ll> first_group = {1};
  groups.push_back(first_group); // means nothing
  groups.push_back(first_group);
  for (ll i = 2 ; i <= N ; i++) {
    if (fs.find(make_pair(i - 1, i)) == fs.end()) {
      groups.push_back(vector<ll>());
    }
    groups.back().push_back(i);
  }
  ll G = groups.size() - 1;
  /*
  for (ll i = 1 ; i <= G ; i++) {
    cout << "g" << i << ": ";
    for (ll g : groups[i]) {
      cout << g << ", ";
    }
    cout << endl;
  }
  */

  for (ll i = 1 ; i <= G ; i++) {
    // from all groups, select the one with the least number of a
    vector<pair<ll, ll>> arr;
    vector<ll> something(1010, 0);
    ll sum_beauty = 0;
    ll sum_weight = 0;

    for (ll member : groups[i]) {
      sum_beauty += bs[member];
      sum_weight += weights[member];
      arr.push_back(make_pair(weights[member], bs[member]));
    }

    /*
    sort(arr.begin(), arr.end());
    for (ll t = 0 ; t < arr ; t++) {
      something[arr[t].first] = max(
          arr[t].second, something[arr[t].first]);
    }

    for (ll t = 1 ; i < groups[i].size() ; t++) {
      something[t] = max(something[t-1], something[t]);
    }
    */

    for (ll j = 0 ; j <= W ; j++) {
      // don't pick anyone from this group
      ll possible = 0;

      for (auto p : arr) {
        if (p.first <= j) {
          dp[i][j] = max(dp[i][j], max(
              dp[i-1][j], dp[i-1][j-p.first] + p.second));
        }
      }
      if (sum_weight <= j) {
        dp[i][j] = max(dp[i][j], max(dp[i-1][j], dp[i-1][j-sum_weight] + sum_beauty));
      }

      dp[i][j] = max(dp[i][j], dp[i-1][j]);
    }
  }

  cout << dp[G][W] << endl;

  return 0;
}


#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// Useful globals
ll N, M, W;
vector<ll> weights;
vector<ll> bs;

// Useful utility methods
template<class T>
static inline void cin_vector(ull N, vector<T> & v) {
  for (ull i = 0; i < N ; i++) {
    T a;
    cin >> a;
    v.push_back(a);
  }
}

template<class T>
T gcd(T a, T b) {
  if (b > a) {
    swap(a, b);
  }
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

// Useful classes
class DisjointSet{
public:
  vector<int> parent;

  DisjointSet(int n): parent(n) { for(int i=0; i<n; i++) parent[i] = i; }

  void join(int a, int b) { parent[find(b)] = find(a); }

  int find(int a){ return a == parent[a] ? a : parent[a] = find(parent[a]); }

  bool check(int a, int b){ return find(a) == find(b); }
};

// dp[n][w] : best attainable, constrained by weight <= w, and
// only using the first n friendship groups.
ll dp[1010][1010];

// main procedure
int main() {
  ios::sync_with_stdio(false);
  cout.precision(8);

  // your code here
  cin >> N >> M >> W;
  bs.push_back(0);
  weights.push_back(0);
  cin_vector(N, weights);
  cin_vector(N, bs);
  map<int, vector<ll>> groups_map;
  vector<vector<ll>> groups;
  DisjointSet d(N + 5);

  for (ll i = 0 ; i < M ; i++) {
    ll x, y;
    cin >> x >> y;
    d.join(x, y);
  }

  // construct friendship groups
  for (int i = 1 ; i <= N ; i++) {
    int g = d.find(i);
    groups_map[g].push_back(i);
  }

  groups.push_back({1}); // dummy
  for (auto p : groups_map) {
    groups.push_back(p.second);
  }

  // remove redundant groups
  ll G = groups.size() - 1;

  for (ll i = 1 ; i <= G ; i++) {
    // from all groups, select the one with the least number of a
    vector<pair<ll, ll>> arr;
    vector<ll> something(1010, 0);
    ll sum_beauty = 0;
    ll sum_weight = 0;

    for (ll member : groups[i]) {
      sum_beauty += bs[member];
      sum_weight += weights[member];
      arr.push_back(make_pair(weights[member], bs[member]));
    }

    for (ll j = 0 ; j <= W ; j++) {
      // don't pick anyone from this group
      ll possible = 0;

      for (auto p : arr) {
        if (p.first <= j) {
          possible = max(dp[i-1][j-p.first] + p.second, possible);
        }
      }
      if (sum_weight <= j) {
        possible = max(possible, dp[i-1][j-sum_weight] + sum_beauty);
      }

      dp[i][j] = max(possible, dp[i-1][j]);
    }
  }

  cout << dp[G][W] << endl;

  return 0;
}


#include <algorithm>
#include <iostream>
#include <set>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// Useful globals
ll N, M, W;
vector<ll> weights;
vector<ll> bs;

// Useful utility methods
template<class T>
static inline void cin_vector(ull N, vector<T> & v) {
  for (ull i = 0; i < N ; i++) {
    T a;
    cin >> a;
    v.push_back(a);
  }
}

template<class T>
T gcd(T a, T b) {
  if (b > a) {
    swap(a, b);
  }
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

// dp[n][w] : best attainable, constrained by weight <= w, and
// only using the first n friendship groups.
ll dp[1010][1010];

// main procedure
int main() {
  ios::sync_with_stdio(false);
  cout.precision(8);

  // your code here
  cin >> N >> M >> W;
  bs.push_back(0);
  weights.push_back(0);
  cin_vector(N, weights);
  cin_vector(N, bs);
  set<pair<ll, ll>> fs;
  vector<vector<ll>> groups;

  for (ll i = 0 ; i < M ; i++) {
    ll x, y;
    cin >> x >> y;
    if (x > y) {
      swap(x, y);
    }
    fs.insert(make_pair(x, y));
  }

  // construct friendship groups
  vector<ll> first_group = {1};
  groups.push_back(first_group); // means nothing
  groups.push_back(first_group);
  for (ll i = 2 ; i <= N ; i++) {
    if (fs.find(make_pair(i - 1, i)) == fs.end()) {
      groups.push_back(vector<ll>());
    }
    groups.back().push_back(i);
  }
  ll G = groups.size() - 1;
  /*
  for (ll i = 1 ; i <= G ; i++) {
    cout << "g" << i << ": ";
    for (ll g : groups[i]) {
      cout << g << ", ";
    }
    cout << endl;
  }
  */

  for (ll i = 1 ; i <= G ; i++) {
    // from all groups, select the one with the least number of a
    vector<pair<ll, ll>> arr;
    vector<ll> something(1010, 0);
    ll sum_beauty = 0;
    ll sum_weight = 0;

    for (ll member : groups[i]) {
      sum_beauty += bs[member];
      sum_weight += weights[member];
      arr.push_back(make_pair(weights[member], bs[member]));
    }

    for (ll j = 0 ; j <= W ; j++) {
      // don't pick anyone from this group
      ll possible = 0;

      for (auto p : arr) {
        if (p.first <= j) {
          possible = max(dp[i-1][j-p.first] + p.second, possible);
        }
      }
      if (sum_weight <= j) {
        possible = max(possible, dp[i-1][j-sum_weight] + sum_beauty);
      }

      dp[i][j] = max(possible, dp[i-1][j]);
    }
  }

  cout << dp[G][W] << endl;

  return 0;
}


#include <algorithm>
#include <iostream>
#include <set>
#include <queue>
#include <sstream>
#include <string>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// Useful globals
int N, M, W;
vector<int> weights;
vector<int> bs;

// Useful utility methods
template<class T>
static inline void cin_vector(ull N, vector<T> & v) {
  for (ull i = 0; i < N ; i++) {
    T a;
    cin >> a;
    v.push_back(a);
  }
}

template<class T>
T gcd(T a, T b) {
  if (b > a) {
    swap(a, b);
  }
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

// dp[n][w] : best attainable, constrained by weight <= w, and
// only using the first n friendship groups.
int dp[1010][1010];

// main procedure
int main() {
  ios::sync_with_stdio(false);
  cout.precision(8);

  // your code here
  cin >> N >> M >> W;
  bs.push_back(0);
  weights.push_back(0);
  cin_vector(N, weights);
  cin_vector(N, bs);
  set<pair<int, int>> fs;
  vector<vector<int>> groups;

  for (int i = 0 ; i < M ; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y) {
      swap(x, y);
    }
    fs.insert(make_pair(x, y));
  }

  // construct friendship groups
  vector<int> first_group = {1};
  groups.push_back(first_group); // means nothing
  groups.push_back(first_group);
  for (int i = 2 ; i <= N ; i++) {
    if (fs.find(make_pair(i - 1, i)) == fs.end()) {
      groups.push_back(vector<int>());
    }
    groups.back().push_back(i);
  }
  int G = groups.size() - 1;
  /*
  for (int i = 1 ; i <= G ; i++) {
    cout << "g" << i << ": ";
    for (int g : groups[i]) {
      cout << g << ", ";
    }
    cout << endl;
  }
  */

  for (int i = 1 ; i <= G ; i++) {
    // from all groups, select the one with the least number of a
    vector<pair<int, int>> arr;
    vector<int> something(1010, 0);
    int sum_beauty = 0;
    int sum_weight = 0;

    for (int member : groups[i]) {
      sum_beauty += bs[member];
      sum_weight += weights[member];
      arr.push_back(make_pair(weights[member], bs[member]));
    }

    /*
    sort(arr.begin(), arr.end());
    for (int t = 0 ; t < arr ; t++) {
      something[arr[t].first] = max(
          arr[t].second, something[arr[t].first]);
    }

    for (int t = 1 ; i < groups[i].size() ; t++) {
      something[t] = max(something[t-1], something[t]);
    }
    */

    for (int j = 0 ; j <= W ; j++) {
      // don't pick anyone from this group
      int possible = 0;

      for (auto p : arr) {
        if (p.first <= j) {
          dp[i][j] = max(dp[i][j], max(
              dp[i-1][j], dp[i-1][j-p.first] + p.second));
        }
      }
      if (sum_weight <= j) {
        dp[i][j] = max(dp[i][j], max(dp[i-1][j], dp[i-1][j-sum_weight] + sum_beauty));
      }

      dp[i][j] = max(dp[i][j], dp[i-1][j]);
    }
  }

  cout << dp[G][W] << endl;

  return 0;
}


#include <iostream>
#include <vector>
#include <queue>
using namespace std;

long long n,m,W,w[1002],b[1002],v[1001],d[1002][1002];
vector<long long> G[1002];
deque<long long> q;
vector<vector<pair<long long,long long>>> cc;

int main() {

    cin >> n >> m >> W;
    for (int i=1; i<=n; i++) {
        cin >> w[i];
    }
    for (int i=1; i<=n; i++) {
        cin >> b[i];
    }
    for (int i=1; i<=m; i++) {
        long long x,y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    if (n==1) {
        cout << w[1];
        return 0;
    }

    for (int i=1; i<=n; i++) {
        if (!v[i]) {
            vector<pair<long long,long long>> ca;
            long long w1 = 0,b1 = 0;
            v[i] = 1;
            q.push_back(i);
            while (!q.empty()) {
                long long aux = q.front();
                q.pop_front();
                w1 += w[aux];
                b1 += b[aux];
                ca.push_back(make_pair(w[aux],b[aux]));
                for (auto x:G[aux]) {
                    if (!v[x]) {
                        q.push_back(x);
                        v[x] = 1;
                    }
                }
            }
            ca.push_back(make_pair(w1,b1));
            cc.push_back(ca);
        }
    }

//    for (int i=0; i<cc.size(); i++) {
//        cout << i << ':';
//        for (auto x:cc[i]) {
//            cout << '(' << x.first << ' ' << x.second << ')';
//        }
//        cout << endl;
//    }

    for (int i=0; i<cc.size(); i++) {
        for (int j = 0; j <= W; j++) {
            for (auto x:cc[i]) {
                if (j-x.first >=0 && j-x.first <= W) {
                    d[i+1][j] = max(d[i+1][j],d[i][j-x.first]+x.second);
                    d[i+1][j] = max(d[i+1][j], d[i][j]);
                }
            }
        }
    }
//
//    for (int i=0; i<=cc.size(); i++) {
//        for (int j = 0; j <= W; j++) {
//            cout << d[i][j] << ' ';
//        }
//        cout << endl;
//    }

    long long rs = 0;
    for (int i=0;i<=W; i++) {
        rs = max(rs, d[cc.size()][i]);
    }

    cout << rs;

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n,m,W,w[1002],b[1002],v[1001],d[1002][1002];
vector<int> G[1002];
deque<int> q;
vector<vector<pair<int,int>>> cc;

int main() {

    cin >> n >> m >> W;
    for (int i=1; i<=n; i++) {
        cin >> w[i];
    }
    for (int i=1; i<=n; i++) {
        cin >> b[i];
    }
    for (int i=1; i<=m; i++) {
        int x,y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for (int i=1; i<=n; i++) {
        if (!v[i]) {
            vector<pair<int,int>> ca;
            int w1 = 0,b1 = 0;
            v[i] = 1;
            q.push_back(i);
            while (!q.empty()) {
                int aux = q.front();
                q.pop_front();
                w1 += w[aux];
                b1 += b[aux];
                ca.push_back(make_pair(w[aux],b[aux]));
                for (auto x:G[aux]) {
                    if (!v[x]) {
                        q.push_back(x);
                        v[x] = 1;
                    }
                }
            }
            ca.push_back(make_pair(w1,b1));
            cc.push_back(ca);
        }
    }

//    for (int i=0; i<cc.size(); i++) {
//        cout << i << ':';
//        for (auto x:cc[i]) {
//            cout << '(' << x.first << ' ' << x.second << ')';
//        }
//        cout << endl;
//    }

    for (int i=0; i<cc.size(); i++) {
        for (int j = 0; j <= W; j++) {
            for (auto x:cc[i]) {
                if (j-x.first >=0 && j-x.first <= W) {
                    d[i+1][j] = max(d[i+1][j],d[i][j-x.first]+x.second);
                    d[i+1][j] = max(d[i+1][j], d[i][j]);
                }
            }
        }
    }
//
//    for (int i=0; i<=cc.size(); i++) {
//        for (int j = 0; j <= W; j++) {
//            cout << d[i][j] << ' ';
//        }
//        cout << endl;
//    }

    int rs = 0;
    for (int i=0;i<=W; i++) {
        rs = max(rs, d[cc.size()][i]);
    }

    cout << rs;

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
using namespace std;

long long n,m,W,w[1002],b[1002],v[1001],d[1002][1002];
vector<long long> G[1002];
deque<long long> q;
vector<vector<pair<long long,long long>>> cc;

int main() {

    cin >> n >> m >> W;
    for (int i=1; i<=n; i++) {
        cin >> w[i];
    }
    for (int i=1; i<=n; i++) {
        cin >> b[i];
    }
    for (int i=1; i<=m; i++) {
        long long x,y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    if (n==1) {
        cout << w[1];
        return 0;
    }

    for (int i=1; i<=n; i++) {
        if (!v[i]) {
            vector<pair<long long,long long>> ca;
            long long w1 = 0,b1 = 0;
            v[i] = 1;
            q.push_back(i);
            while (!q.empty()) {
                long long aux = q.front();
                q.pop_front();
                w1 += w[aux];
                b1 += b[aux];
                ca.push_back(make_pair(w[aux],b[aux]));
                for (auto x:G[aux]) {
                    if (!v[x]) {
                        q.push_back(x);
                        v[x] = 1;
                    }
                }
            }
            ca.push_back(make_pair(w1,b1));
            cc.push_back(ca);
        }
    }

//    for (int i=0; i<cc.size(); i++) {
//        cout << i << ':';
//        for (auto x:cc[i]) {
//            cout << '(' << x.first << ' ' << x.second << ')';
//        }
//        cout << endl;
//    }

    for (int i=0; i<cc.size(); i++) {
        for (int j = 0; j <= W; j++) {
            d[i+1][j] = max(d[i+1][j], d[i][j]);
            for (auto x:cc[i]) {
                if (j-x.first >=0 && j-x.first <= W) {
                    d[i+1][j] = max(d[i+1][j],d[i][j-x.first]+x.second);
                }
            }
        }
    }

//    for (int i=0; i<=cc.size(); i++) {
//        for (int j = 0; j <= W; j++) {
//            cout << d[i][j] << ' ';
//        }
//        cout << endl;
//    }

    long long rs = 0;
    for (int i=0;i<=W; i++) {
        rs = max(rs, d[cc.size()][i]);
    }

    cout << rs;

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
using namespace std;

long long n,m,W,w[1002],b[1002],v[1001],d[1002][1002];
vector<int> G[1002];
deque<int> q;
vector<vector<pair<int,int>>> cc;

int main() {

    cin >> n >> m >> W;
    for (int i=1; i<=n; i++) {
        cin >> w[i];
    }
    for (int i=1; i<=n; i++) {
        cin >> b[i];
    }
    for (int i=1; i<=m; i++) {
        int x,y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    if (n==1) {
        cout << w[1];
        return 0;
    }

    for (int i=1; i<=n; i++) {
        if (!v[i]) {
            vector<pair<int,int>> ca;
            int w1 = 0,b1 = 0;
            v[i] = 1;
            q.push_back(i);
            while (!q.empty()) {
                int aux = q.front();
                q.pop_front();
                w1 += w[aux];
                b1 += b[aux];
                ca.push_back(make_pair(w[aux],b[aux]));
                for (auto x:G[aux]) {
                    if (!v[x]) {
                        q.push_back(x);
                        v[x] = 1;
                    }
                }
            }
            ca.push_back(make_pair(w1,b1));
            cc.push_back(ca);
        }
    }

//    for (int i=0; i<cc.size(); i++) {
//        cout << i << ':';
//        for (auto x:cc[i]) {
//            cout << '(' << x.first << ' ' << x.second << ')';
//        }
//        cout << endl;
//    }

    for (int i=0; i<cc.size(); i++) {
        for (int j = 0; j <= W; j++) {
            for (auto x:cc[i]) {
                if (j-x.first >=0 && j-x.first <= W) {
                    d[i+1][j] = max(d[i+1][j],d[i][j-x.first]+x.second);
                    d[i+1][j] = max(d[i+1][j], d[i][j]);
                }
            }
        }
    }
//
//    for (int i=0; i<=cc.size(); i++) {
//        for (int j = 0; j <= W; j++) {
//            cout << d[i][j] << ' ';
//        }
//        cout << endl;
//    }

    int rs = 0;
    for (int i=0;i<=W; i++) {
        rs = max(rs, d[cc.size()][i]);
    }

    cout << rs;

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n,m,W,w[1002],b[1002],v[1001],d[1002][1002];
vector<int> G[1002];
deque<int> q;
vector<vector<pair<int,int>>> cc;

int main() {

    cin >> n >> m >> W;
    for (int i=1; i<=n; i++) {
        cin >> w[i];
    }
    for (int i=1; i<=n; i++) {
        cin >> b[i];
    }
    for (int i=1; i<=m; i++) {
        int x,y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    if (n==1) {
        cout << w[1];
        return 0;
    }

    for (int i=1; i<=n; i++) {
        if (!v[i]) {
            vector<pair<int,int>> ca;
            int w1 = 0,b1 = 0;
            v[i] = 1;
            q.push_back(i);
            while (!q.empty()) {
                int aux = q.front();
                q.pop_front();
                w1 += w[aux];
                b1 += b[aux];
                ca.push_back(make_pair(w[aux],b[aux]));
                for (auto x:G[aux]) {
                    if (!v[x]) {
                        q.push_back(x);
                        v[x] = 1;
                    }
                }
            }
            ca.push_back(make_pair(w1,b1));
            cc.push_back(ca);
        }
    }

//    for (int i=0; i<cc.size(); i++) {
//        cout << i << ':';
//        for (auto x:cc[i]) {
//            cout << '(' << x.first << ' ' << x.second << ')';
//        }
//        cout << endl;
//    }

    for (int i=0; i<cc.size(); i++) {
        for (int j = 0; j <= W; j++) {
            for (auto x:cc[i]) {
                if (j-x.first >=0 && j-x.first <= W) {
                    d[i+1][j] = max(d[i+1][j],d[i][j-x.first]+x.second);
                    if (cc.size() == 1)
                        d[i+1][j] = max(d[i+1][j], d[i][j]);
                }
            }
        }
    }
//
//    for (int i=0; i<=cc.size(); i++) {
//        for (int j = 0; j <= W; j++) {
//            cout << d[i][j] << ' ';
//        }
//        cout << endl;
//    }

    int rs = 0;
    for (int i=0;i<=W; i++) {
        rs = max(rs, d[cc.size()][i]);
    }

    cout << rs;

    return 0;
}


#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n,m,W,w[1002],b[1002],v[1001],d[1002][1002];
vector<int> G[1002];
deque<int> q;
vector<vector<pair<int,int>>> cc;

int main() {

    cin >> n >> m >> W;
    for (int i=1; i<=n; i++) {
        cin >> w[i];
    }
    for (int i=1; i<=n; i++) {
        cin >> b[i];
    }
    for (int i=1; i<=m; i++) {
        int x,y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    if (n==1) {
        cout << w[1];
        return 0;
    }

    for (int i=1; i<=n; i++) {
        if (!v[i]) {
            vector<pair<int,int>> ca;
            int w1 = 0,b1 = 0;
            v[i] = 1;
            q.push_back(i);
            while (!q.empty()) {
                int aux = q.front();
                q.pop_front();
                w1 += w[aux];
                b1 += b[aux];
                ca.push_back(make_pair(w[aux],b[aux]));
                for (auto x:G[aux]) {
                    if (!v[x]) {
                        q.push_back(x);
                        v[x] = 1;
                    }
                }
            }
            ca.push_back(make_pair(w1,b1));
            cc.push_back(ca);
        }
    }

//    for (int i=0; i<cc.size(); i++) {
//        cout << i << ':';
//        for (auto x:cc[i]) {
//            cout << '(' << x.first << ' ' << x.second << ')';
//        }
//        cout << endl;
//    }

    for (int i=0; i<cc.size(); i++) {
        for (int j = 0; j <= W; j++) {
            for (auto x:cc[i]) {
                if (j-x.first >=0 && j-x.first <= W) {
                    d[i+1][j] = max(d[i+1][j],d[i][j-x.first]+x.second);
                    d[i+1][j] = max(d[i+1][j], d[i][j]);
                }
            }
        }
    }
//
//    for (int i=0; i<=cc.size(); i++) {
//        for (int j = 0; j <= W; j++) {
//            cout << d[i][j] << ' ';
//        }
//        cout << endl;
//    }

    int rs = 0;
    for (int i=0;i<=W; i++) {
        rs = max(rs, d[cc.size()][i]);
    }

    cout << rs;

    return 0;
}


// in the name of God

# include <algorithm>
# include <iostream>
# include <iomanip>
# include <vector>
# include <set>
# include <map>
# include <deque>
# include <queue>
# include <cmath>
# include <ctime>
# include <string>
# include <cstdlib>
# include <cstring>
# include <cstdio>
# include <list>
# include <fstream>

using namespace std;

//# define cout fout
//# define cin fin
# define pb push_back
# define slark cerr << "slark" << endl;
# define dagon cout << endl;
# define XX first
# define YY second

const int oo = 999999999;
const double PI = 3.1415931;
const double eps = 1e-9;

typedef long long ll;
typedef vector <ll> vint;
typedef pair < ll , ll > pii;

const int maxN = 1000 + 10;
ll b[maxN] , beauty[maxN];
ll n , m , w[maxN] , W , weight[maxN] , C[maxN] , last[maxN];
bool mark[maxN];
ll dp[maxN][maxN];
vint v[maxN];
int comp;

void dfs (int x , int comp)
{
   mark[x] = true;
   C[x] = comp;
   beauty[comp] += b[x];
   weight[comp] += w[x];
   for (int i = 0 ; i < v[x].size() ; i++)
   {
      if (!mark[v[x][i]])
         dfs(v[x][i] , comp);
   }
}

int main()
{
    ios_base::sync_with_stdio(false);

      cin >> n >> m >> W;
      for (int i = 1 ; i <= n ; i++)
         cin >> w[i];
      for (int i = 1 ; i <= n ; i++)
         cin >> b[i];
      for (int i = 1 ; i <= m ; i++)
      {
         int x , y;
         cin >> x >> y;
         v[x].pb(y);
         v[y].pb(x);
      }
      vector < pii > afrad;
      afrad.pb(pii(0 , 0));
      for (int i = 1 ; i <= n ; i++)
      {
         if (!mark[i])
         {
            comp++;
            dfs(i , comp);
         }
         afrad.pb(pii(C[i] , i));
      }
      sort(afrad.begin() , afrad.end());
      for (int i = 0 ; i < afrad.size() ; i++)
      {
         last[afrad[i].XX] = i;
      }
      ll maxi = 0;
      last[0] = 0;
      for (int i = 1 ; i < afrad.size() ; i++)
      {
         int now = afrad[i].YY;
         int nowComp = afrad[i].XX;
         for (int j = 0 ; j <= W ; j++)
         {
            if (j >= weight[C[now]])
            {
               dp[i][j] = dp[last[nowComp - 1]][j - weight[nowComp]] + beauty[nowComp];
            }
            if (j >= w[now])
            {
               dp[i][j] = max(dp[i][j] , dp[last[nowComp - 1]][j - w[now]] + b[now]);
            }
            dp[i][j] = max(dp[i][j] , dp[i - 1][j]);
            maxi = max(maxi , dp[i][j]);
         }
      }
      cout << maxi << endl;
    return 0;
}


// in the name of God

# include <algorithm>
# include <iostream>
# include <iomanip>
# include <vector>
# include <set>
# include <map>
# include <deque>
# include <queue>
# include <cmath>
# include <ctime>
# include <string>
# include <cstdlib>
# include <cstring>
# include <cstdio>
# include <list>
# include <fstream>

using namespace std;

//# define cout fout
//# define cin fin
# define pb push_back
# define slark cerr << "slark" << endl;
# define dagon cout << endl;
# define XX first
# define YY second

const int oo = 999999999;
const double PI = 3.1415931;
const double eps = 1e-9;

typedef long long ll;
typedef vector <ll> vint;
typedef pair < ll , ll > pii;

const int maxN = 1000 + 10;
ll b[maxN] , beauty[maxN];
ll n , m , w[maxN] , W , weight[maxN] , C[maxN] , last[maxN];
bool mark[maxN];
ll dp[maxN][maxN];
vint v[maxN];
int comp;
ll B[maxN] , newweight[maxN];
void dfs (int x , int comp)
{
	mark[x] = true;
	C[x] = comp;
	beauty[comp] += b[x];
	weight[comp] += w[x];
	for (int i = 0 ; i < v[x].size() ; i++)
	{
		if (!mark[v[x][i]])
			dfs(v[x][i] , comp);
	}
}

int main()
{
    ios_base::sync_with_stdio(false);

   	cin >> n >> m >> W;
   	for (int i = 1 ; i <= n ; i++)
   		cin >> w[i];
   	for (int i = 1 ; i <= n ; i++)
   		cin >> b[i];
   	for (int i = 1 ; i <= m ; i++)
   	{
   		int x , y;
   		cin >> x >> y;
   		v[x].pb(y);
   		v[y].pb(x);
   	}
   	vector < pii > afrad;
   	afrad.pb(pii(0 , 0));
   	for (int i = 1 ; i <= n ; i++)
   	{
   		if (!mark[i])
   		{
   			comp++;
   			dfs(i , comp);
   		}
   		afrad.pb(pii(C[i] , i));
   		B[afrad.size() - 1] = b[i];
   		newweight[afrad.size() - 1] = w[i];
   	}
   	sort(afrad.begin() , afrad.end());
   	for (int i = 0 ; i < afrad.size() ; i++)
   	{
   		last[afrad[i].XX] = i;
   	}
   	ll maxi = 0;
   	last[0] = 0;
   	for (int i = 1 ; i < afrad.size() ; i++)
   	{
   		int now = afrad[i].YY;
   		int nowComp = afrad[i].XX;
   		for (int j = 0 ; j <= W ; j++)
   		{
   			if (j >= weight[C[now]])
   			{
   				dp[i][j] = dp[last[nowComp - 1]][j - weight[nowComp]] + beauty[nowComp];
   			}
   			if (j >= newweight[i])
   			{
   				dp[i][j] = max(dp[i][j] , dp[last[nowComp - 1]][j - newweight[i]] + B[i]);
   			}
   			dp[i][j] = max(dp[i][j] , dp[i - 1][j]);
   			maxi = max(maxi , dp[i][j]);
   		}
   	}
   	cout << maxi << endl;
    return 0;
}


// in the name of God

# include <algorithm>
# include <iostream>
# include <iomanip>
# include <vector>
# include <set>
# include <map>
# include <deque>
# include <queue>
# include <cmath>
# include <ctime>
# include <string>
# include <cstdlib>
# include <cstring>
# include <cstdio>
# include <list>
# include <fstream>

using namespace std;

//# define cout fout
//# define cin fin
# define pb push_back
# define slark cerr << "slark" << endl;
# define dagon cout << endl;
# define XX first
# define YY second

const int oo = 999999999;
const double PI = 3.1415931;
const double eps = 1e-9;

typedef long long ll;
typedef vector <ll> vint;
typedef pair < ll , ll > pii;

const int maxN = 1000 + 10;
ll b[maxN] , beauty[maxN];
ll n , m , w[maxN] , W , weight[maxN] , C[maxN] , last[maxN];
bool mark[maxN];
ll dp[maxN][maxN];
vint v[maxN];
int comp;

void dfs (int x , int comp)
{
	mark[x] = true;
	C[x] = comp;
	beauty[comp] += b[x];
	weight[comp] += w[x];
	for (int i = 0 ; i < v[x].size() ; i++)
	{
		if (!mark[v[x][i]])
			dfs(v[x][i] , comp);
	}
}

int main()
{
    ios_base::sync_with_stdio(false);

   	cin >> n >> m >> W;
   	for (int i = 1 ; i <= n ; i++)
   		cin >> w[i];
   	for (int i = 1 ; i <= n ; i++)
   		cin >> b[i];
   	for (int i = 1 ; i <= m ; i++)
   	{
   		int x , y;
   		cin >> x >> y;
   		v[x].pb(y);
   		v[y].pb(x);
   	}
   	vector < pii > afrad;
   	afrad.pb(pii(0 , 0));
   	for (int i = 1 ; i <= n ; i++)
   	{
   		if (!mark[i])
   		{
   			comp++;
   			dfs(i , comp);
   		}
   		afrad.pb(pii(C[i] , i));
   	}
   	sort(afrad.begin() , afrad.end());
   	for (int i = 0 ; i < afrad.size() ; i++)
   	{
   		last[afrad[i].XX] = i;
   	}
   	for (int i = 1 ; i < afrad.size() ; i++)
   	{
   		int now = afrad[i].YY;
   		int nowComp = afrad[i].XX;
   		for (int j = 0 ; j <= W ; j++)
   		{
   			if (j >= weight[C[now]])
   			{
   				dp[i][j] = dp[last[nowComp - 1]][j - weight[nowComp]] + beauty[nowComp];
   			}
   			if (j >= w[i])
   			{
   				dp[i][j] = max(dp[i][j] , dp[last[nowComp - 1]][j - w[i]] + b[i]);
   			}
   			dp[i][j] = max(dp[i][j] , dp[i - 1][j]);
   		}
   	}
   	cout << dp[(int)afrad.size() - 1][W] << endl;
    return 0;
}


// in the name of God

# include <algorithm>
# include <iostream>
# include <iomanip>
# include <vector>
# include <set>
# include <map>
# include <deque>
# include <queue>
# include <cmath>
# include <ctime>
# include <string>
# include <cstdlib>
# include <cstring>
# include <cstdio>
# include <list>
# include <fstream>

using namespace std;

//# define cout fout
//# define cin fin
# define pb push_back
# define slark cerr << "slark" << endl;
# define dagon cout << endl;
# define XX first
# define YY second

const int oo = 999999999;
const double PI = 3.1415931;
const double eps = 1e-9;

typedef long long ll;
typedef vector <ll> vint;
typedef pair < ll , ll > pii;

const int maxN = 1000 + 10;
ll b[maxN] , beauty[maxN];
ll n , m , w[maxN] , W , weight[maxN] , C[maxN] , last[maxN];
bool mark[maxN];
ll dp[maxN][maxN];
vint v[maxN];
int comp;

void dfs (int x , int comp)
{
	mark[x] = true;
	C[x] = comp;
	beauty[comp] += b[x];
	weight[comp] += w[x];
	for (int i = 0 ; i < v[x].size() ; i++)
	{
		if (!mark[v[x][i]])
			dfs(v[x][i] , comp);
	}
}

int main()
{
    ios_base::sync_with_stdio(false);

   	cin >> n >> m >> W;
   	for (int i = 1 ; i <= n ; i++)
   		cin >> w[i];
   	for (int i = 1 ; i <= n ; i++)
   		cin >> b[i];
   	for (int i = 1 ; i <= m ; i++)
   	{
   		int x , y;
   		cin >> x >> y;
   		v[x].pb(y);
   		v[y].pb(x);
   	}
   	vector < pii > afrad;
   	afrad.pb(pii(0 , 0));
   	for (int i = 1 ; i <= n ; i++)
   	{
   		if (!mark[i])
   		{
   			comp++;
   			dfs(i , comp);
   		}
   		afrad.pb(pii(C[i] , i));
   	}
   	sort(afrad.begin() , afrad.end());
   	for (int i = 0 ; i < afrad.size() ; i++)
   	{
   		last[afrad[i].XX] = i;
   	}
   	ll maxi = 0;
   	last[0] = 0;
   	for (int i = 1 ; i < afrad.size() ; i++)
   	{
   		int now = afrad[i].YY;
   		int nowComp = afrad[i].XX;
   		for (int j = 0 ; j <= W ; j++)
   		{
   			if (j >= weight[C[now]])
   			{
   				dp[i][j] = dp[last[nowComp - 1]][j - weight[nowComp]] + beauty[nowComp];
   			}
   			if (j >= w[i])
   			{
   				dp[i][j] = max(dp[i][j] , dp[last[nowComp - 1]][j - w[i]] + b[i]);
   			}
   			dp[i][j] = max(dp[i][j] , dp[i - 1][j]);
   			maxi = max(maxi , dp[i][j]);
   		}
   	}
   	cout << maxi << endl;
    return 0;
}


#include <cstdio>
#include <vector>

using namespace std;

const int N = 1010;

vector<int> g[N], cps[N];
int a[N], b[N], c[N], sw[N], d[2][N];
bool u[N];
int n, m, w;

void go(int v, int comp)
{
	cps[comp].push_back(v);
	c[comp] += a[v];
	sw[comp] += b[v];
	u[v] = true;
	for (auto to : g[v])
	{
		if (u[to]) continue;
		go(to, comp);
	}
}

int main()
{
	scanf("%d%d%d", &n, &m, &w);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int i = 0; i < n; ++i)
		scanf("%d", &b[i]);
	for (int i = 0; i < m; ++i)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		--x; --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	int ans = 0;
	for (int i = 0; i < n; ++i)
		if (a[i] <= w)
			ans = max(ans, b[i]);

	int comp = 0;
	for (int i = 0; i < n; ++i)
		if (!u[i])
		{
			go(i, comp);
			++comp;
		}

	for (int i = 0; i < comp; ++i)
		if (c[i] <= w)
			ans = max(ans, sw[i]);

	int x = 0, y = 1;
	for (int i = 0; i < comp; ++i)
	{
		d[y][c[i]] = max(d[y][c[i]], sw[i]);
		for (int j = 0; j <= w && j + c[i] <= w; ++j)
			if (d[x][j] != 0)
				d[y][j + c[i]] = max(d[y][j + c[i]], d[x][j] + sw[i]);
		for (auto k : cps[i])
		{
			d[y][a[k]] = max(d[y][a[k]], b[k]);
			for (int j = 0; j <= w && j + a[k] <= w; ++j)
				if (d[x][j] != 0)
					d[y][j + a[k]] = max(d[y][j + a[k]], d[x][j] + b[k]);
		}
		for (int i = 0; i <= w; ++i)
			d[x][i] = d[y][i];
		// x ^= 1; y ^= 1;
	}

	// for (int i = 1; i <= w; ++i)
	// 	printf("%d ", d[x][i]);
	// printf("\n");

	for (int i = 1; i <= w; ++i)
		ans = max(ans, d[x][i]);
	printf("%d\n", ans);

	return 0;
}


#include <cstdio>
#include <vector>

using namespace std;

const int N = 1010;

vector<int> g[N], cps[N];
int a[N], b[N], c[N], sw[N], d[2][N];
bool u[N];
int n, m, w;

void go(int v, int comp)
{
	cps[comp].push_back(v);
	c[comp] += a[v];
	sw[comp] += b[v];
	u[v] = true;
	for (auto to : g[v])
	{
		if (u[to]) continue;
		go(to, comp);
	}
}

int main()
{
	scanf("%d%d%d", &n, &m, &w);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int i = 0; i < n; ++i)
		scanf("%d", &b[i]);
	for (int i = 0; i < m; ++i)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		--x; --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	int ans = 0;
	for (int i = 0; i < n; ++i)
		if (a[i] <= w)
			ans = max(ans, b[i]);

	int comp = 0;
	for (int i = 0; i < n; ++i)
		if (!u[i])
		{
			go(i, comp);
			++comp;
		}

	for (int i = 0; i < comp; ++i)
		if (c[i] <= w)
			ans = max(ans, sw[i]);

	int x = 0, y = 1;
	for (int i = 0; i < comp; ++i)
	{
		d[y][c[i]] = max(d[y][c[i]], sw[i]);
		for (int j = 0; j <= w && j + c[i] <= w; ++j)
			if (d[x][j] != 0)
				d[y][j + c[i]] = max(d[y][j + c[i]], d[x][j] + sw[i]);
		for (auto k : cps[i])
		{
			d[y][a[k]] = max(d[y][a[k]], b[k]);
			for (int j = 0; j <= w && j + a[k] <= w; ++j)
				if (d[x][j] != 0)
					d[y][j + a[k]] = max(d[y][j + a[k]], d[x][j] + b[k]);
		}
		x ^= 1; y ^= 1;
	}

	// for (int i = 1; i <= w; ++i)
	// 	printf("%d ", d[x][i]);
	// printf("\n");

	for (int i = 1; i <= w; ++i)
		ans = max(ans, d[x][i]);
	printf("%d\n", ans);

	return 0;
}


#include <cstdio>
#include <vector>

using namespace std;

const int N = 1010;

vector<int> g[N], cps[N];
int a[N], b[N], c[N], sw[N], d[2][N];
bool u[N];
int n, m, w;

void go(int v, int comp)
{
	cps[comp].push_back(v);
	c[comp] += a[v];
	sw[comp] += b[v];
	u[v] = true;
	for (auto to : g[v])
	{
		if (u[to]) continue;
		go(to, comp);
	}
}

int main()
{
	scanf("%d%d%d", &n, &m, &w);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int i = 0; i < n; ++i)
		scanf("%d", &b[i]);
	for (int i = 0; i < m; ++i)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		--x; --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	int ans = 0;
	for (int i = 0; i < n; ++i)
		if (a[i] <= w)
			ans = max(ans, b[i]);

	int comp = 0;
	for (int i = 0; i < n; ++i)
		if (!u[i])
		{
			go(i, comp);
			++comp;
		}

	for (int i = 0; i < comp; ++i)
		if (c[i] <= w)
			ans = max(ans, sw[i]);

	int x = 0, y = 1;
	for (int i = 0; i < comp; ++i)
	{
		for (int j = 0; j <= w && j + c[i] <= w; ++j)
			d[y][j + c[i]] = max(d[y][j + c[i]], d[x][j] + sw[i]);
		for (auto k : cps[i])
			for (int j = 0; j <= w && j + a[k] <= w; ++j)
				d[y][j + a[k]] = max(d[y][j + a[k]], d[x][j] + b[k]);
		x ^= 1; y ^= 1;
	}

	for (int i = 1; i <= w; ++i)
		ans = max(ans, d[x][i]);
	printf("%d\n", ans);

	return 0;
}


#include <cstdio>
#include <vector>

using namespace std;

const int N = 1010;

vector<int> g[N], cps[N];
int a[N], b[N], c[N], sw[N], d[2][N];
bool u[N];
int n, m, w;

void go(int v, int comp)
{
	cps[comp].push_back(v);
	c[comp] += a[v];
	sw[comp] += b[v];
	u[v] = true;
	for (auto to : g[v])
	{
		if (u[to]) continue;
		go(to, comp);
	}
}

int main()
{
	scanf("%d%d%d", &n, &m, &w);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int i = 0; i < n; ++i)
		scanf("%d", &b[i]);
	for (int i = 0; i < m; ++i)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		--x; --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	int ans = 0;
	for (int i = 0; i < n; ++i)
		if (a[i] <= w)
			ans = max(ans, b[i]);

	int comp = 0;
	for (int i = 0; i < n; ++i)
		if (!u[i])
		{
			go(i, comp);
			++comp;
		}

	for (int i = 0; i < comp; ++i)
		if (c[i] <= w)
			ans = max(ans, sw[i]);

	int x = 0, y = 1;
	for (int i = 0; i < comp; ++i)
	{
		if (c[i] <= w) {
			d[y][c[i]] = max(d[y][c[i]], sw[i]);
		}
		for (int j = 0; j <= w && j + c[i] <= w; ++j)
			if (d[x][j] != 0)
				d[y][j + c[i]] = max(d[y][j + c[i]], d[x][j] + sw[i]);
		for (auto k : cps[i])
		{
			d[y][a[k]] = max(d[y][a[k]], b[k]);
			for (int j = 0; j <= w && j + a[k] <= w; ++j)
				if (d[x][j] != 0)
					d[y][j + a[k]] = max(d[y][j + a[k]], d[x][j] + b[k]);
		}
		for (int i = 0; i <= w; ++i)
			d[x][i] = d[y][i];
		// x ^= 1; y ^= 1;
	}

	// for (int i = 1; i <= w; ++i)
	// 	printf("%d ", d[x][i]);
	// printf("\n");

	for (int i = 1; i <= w; ++i)
		ans = max(ans, d[x][i]);
	printf("%d\n", ans);

	return 0;
}


#include "bits/stdc++.h"
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long int
#define sd(x) scanf("%lld",&x)
#define inf 1e18+9
#define pll pair<ll,ll>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int MAXN = 1005;
ll group;
ll W[MAXN], B[MAXN], sumw[MAXN]={0}, sumb[MAXN]={0}, visited[MAXN]={0};
vector<ll> grp[MAXN];
vector<ll> graph[MAXN];
ll N,M,mxW;
ll dp[MAXN][MAXN];

void dfs(ll u, ll g) {
	visited[u] = 1;
	grp[g].pb(u);
	sumw[g] += W[u];
	sumb[g] += B[u];

	for(ll i=0;i<graph[u].size();i++) {
		ll v = graph[u][i];
		if(visited[v]==0) {
			dfs(v,g);
		}
	}
}

ll solve(ll indx, ll checkw) {
	if(indx>group) {
		if(checkw<=mxW) return 0;
		return -inf;
	}

	if(checkw==mxw) return 0;

	if(checkw>mxW) return -inf;

	if(dp[indx][checkw]!=-1) return dp[indx][checkw];

	ll ans = -inf;

	ans = max(ans,solve(indx+1,checkw));
	if(checkw+sumw[indx]<=mxW)
	ans = max(ans,sumb[indx]+solve(indx+1,checkw+sumw[indx]));

	for(ll i=0;i<grp[indx].size();i++) {
		ll v = grp[indx][i];
		if(checkw+W[v]<mxW)
		ans = max(ans, B[v]+solve(indx+1,checkw+W[v]));
	}

	return dp[indx][checkw] = ans;
}

int main() {
	fastio;

	cin>>N>>M>>mxW;

	for(ll i=1;i<=N;i++) {
		cin>>W[i];
	}

	for(ll i=1;i<=N;i++) {
		cin>>B[i];
	}

	for(ll i=0;i<MAXN;i++) {
		for(ll j=0;j<MAXN;j++) {
			dp[i][j]=-1;
		}
	}

	ll x,y;

	for(ll i=1;i<=M;i++) {
		cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}

	group = 0;
	for(ll i=1;i<=N;i++) {
		if(visited[i]==0) {
			dfs(i,++group);
			// cout<<sumw[group]<<" "<<sumb[group]<<endl;
		}
	}

	ll ans = solve(1,0);
	ans = max(0ll,ans);
	cout<<ans<<endl;

}


#include "bits/stdc++.h"
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long int
#define sd(x) scanf("%lld",&x)
#define inf 1e18+9
#define pll pair<ll,ll>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int MAXN = 1005;
ll group;
ll W[MAXN], B[MAXN], sumw[MAXN]={0}, sumb[MAXN]={0}, visited[MAXN]={0};
vector<ll> grp[MAXN];
vector<ll> graph[MAXN];
ll N,M,mxW;
ll dp[MAXN][MAXN];

void dfs(ll u, ll g) {
	visited[u] = 1;
	grp[g].pb(u);
	sumw[g] += W[u];
	sumb[g] += B[u];

	for(ll i=0;i<graph[u].size();i++) {
		ll v = graph[u][i];
		if(visited[v]==0) {
			dfs(v,g);
		}
	}
}

ll solve(ll indx, ll checkw) {
	if(indx>group) {
		if(checkw<=mxW) return 0;
		return -inf;
	}

	if(checkw>mxW) return -inf;

	if(dp[indx][checkw]!=-1) return dp[indx][checkw];

	ll ans = -inf;

	ans = max(ans,solve(indx+1,checkw));
	if(checkw+sumw[indx]<=mxW)
	ans = max(ans,sumb[indx]+solve(indx+1,checkw+sumw[indx]));

	for(ll i=0;i<grp[indx].size();i++) {
		ll v = grp[indx][i];
		if(checkw+W[v]<mxW)
		ans = max(ans, B[v]+solve(indx+1,checkw+W[v]));
	}

	return dp[indx][checkw] = ans;
}

int main() {
	fastio;

	cin>>N>>M>>mxW;

	for(ll i=1;i<=N;i++) {
		cin>>W[i];
	}

	for(ll i=1;i<=N;i++) {
		cin>>B[i];
	}

	for(ll i=0;i<MAXN;i++) {
		for(ll j=0;j<MAXN;j++) {
			dp[i][j]=-1;
		}
	}

	ll x,y;

	for(ll i=1;i<=M;i++) {
		cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}

	group = 0;
	for(ll i=1;i<=N;i++) {
		if(visited[i]==0) {
			dfs(i,++group);
			// cout<<sumw[group]<<" "<<sumb[group]<<endl;
		}
	}

	ll ans = solve(1,0);
	ans = max(0ll,ans);
	cout<<ans<<endl;

}


#include "bits/stdc++.h"
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long int
#define sd(x) scanf("%lld",&x)
#define inf 1e18+9
#define pll pair<ll,ll>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int MAXN = 1005;
ll group;
ll W[MAXN], B[MAXN], sumw[MAXN]={0}, sumb[MAXN]={0}, visited[MAXN]={0};
vector<ll> grp[MAXN];
vector<ll> graph[MAXN];
ll N,M,mxW;
ll dp[MAXN][MAXN];

void dfs(ll u, ll g) {
	visited[u] = 1;
	grp[g].pb(u);
	sumw[g] += W[u];
	sumb[g] += B[u];

	for(ll i=0;i<graph[u].size();i++) {
		ll v = graph[u][i];
		if(visited[v]==0) {
			dfs(v,g);
		}
	}
}

ll solve(ll indx, ll checkw) {
	if(indx>group) {
		if(checkw<=mxW) return 0;
		return -inf;
	}

// 	if(checkw==mxw) return 0;

	if(checkw>mxW) return -inf;

	if(dp[indx][checkw]!=-1) return dp[indx][checkw];

	ll ans = -inf;

	ans = max(ans,solve(indx+1,checkw));
	if(checkw+sumw[indx]<=mxW)
	ans = max(ans,sumb[indx]+solve(indx+1,checkw+sumw[indx]));

	for(ll i=0;i<grp[indx].size();i++) {
		ll v = grp[indx][i];
		if(checkw+W[v]<=mxW)
		ans = max(ans, B[v]+solve(indx+1,checkw+W[v]));
	}

	return dp[indx][checkw] = ans;
}

int main() {
	fastio;

	cin>>N>>M>>mxW;

	for(ll i=1;i<=N;i++) {
		cin>>W[i];
	}

	for(ll i=1;i<=N;i++) {
		cin>>B[i];
	}

	for(ll i=0;i<MAXN;i++) {
		for(ll j=0;j<MAXN;j++) {
			dp[i][j]=-1;
		}
	}

	ll x,y;

	for(ll i=1;i<=M;i++) {
		cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}

	group = 0;
	for(ll i=1;i<=N;i++) {
		if(visited[i]==0) {
			dfs(i,++group);
			// cout<<sumw[group]<<" "<<sumb[group]<<endl;
		}
	}

	ll ans = solve(1,0);
	ans = max(0ll,ans);
	cout<<ans<<endl;

}


#include<bits/stdc++.h>
#define pii pair<int, int>
#define pipi pair<int, pair<int, int> >
#define mp make_pair
using namespace std;

vector<pipi> v;
vector<int> nex;
int ress[1005][1005];
int dis[1005];

int enc(int x){
    if(dis[x]==x)return x;
    return dis[x] = enc(dis[x]);
}

void unir(int x, int y){
    x = enc(x);
    y = enc(y);
    dis[y] = x;
    return;
}

int dinamica(int pos, int peso){
    if(pos>=v.size())return 0;
    if(ress[pos][peso]!=-1) return ress[pos][peso];
    int res = 0;
    if(peso>=v[pos].second.first){
        res = dinamica(nex[pos], peso - v[pos].second.first) + v[pos].second.second;
    }
    return ress[pos][peso] = max(res, dinamica(pos+1, peso));
}

int main(){
    int i, n, m, w, aux, x, y;
    memset(ress, -1, sizeof ress);
    vector<int> a,b;
    scanf("%d%d%d",&n,&m,&w);
    for(i=0;i<n;i++){
        dis[i]=i;
        scanf("%d",&aux);
        a.push_back(aux);
    }
    for(i=0;i<n;i++){
        scanf("%d",&aux);
        b.push_back(aux);
    }
    for(i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        unir(x-1,y-1);
    }
    map<int, pii> mm;
    vector<int> indd;
    for(i=0;i<n;i++){
        if(mm.count(enc(i))==0)indd.push_back(enc(i));
        mm[enc(i)].first+=a[i];
        mm[enc(i)].second+=b[i];
        v.push_back(mp(enc(i), mp(a[i], b[i])));
        nex.push_back(-1);
    }
    for(i=0;i<indd.size();i++){
        v.push_back(mp(indd[i], mm[indd[i]]));
        nex.push_back(-1);
    }
    sort(v.begin(), v.end());
    int pos = v.back().first;
    int ne = v.size();
    for(i=v.size()-1;i>=0;i--){
        //cout<<i<<" "<<v[i].first<<" "<<v[i].second.first<<" "<<v[i].second.second<<endl;
        if(v[i].first!=pos){
            ne = i+1;
            pos = v[i].first;
        }
        nex[i]=ne;
    }
    cout<<dinamica(0, w);
}


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int, ll>
#define pipi pair<int, pair<int, ll> >
#define mp make_pair
using namespace std;

vector<pipi> v;
vector<int> nex;
ll ress[2005][1005];
int dis[1005];

int enc(int x){
    if(dis[x]==x)return x;
    return dis[x] = enc(dis[x]);
}

void unir(int x, int y){
    x = enc(x);
    y = enc(y);
    dis[y] = x;
    return;
}

ll dinamica(int pos, int peso){
    if(pos>=v.size())return 0;
    if(ress[pos][peso]!=-1) return ress[pos][peso];
    ll res = 0;
    if(peso>=v[pos].second.first){
        res = dinamica(nex[pos], peso - v[pos].second.first) + v[pos].second.second;
    }
    return ress[pos][peso] = max(res, dinamica(pos+1, peso));
}

int main(){
    int i, n, m, w, aux, x, y;
    memset(ress, -1, sizeof ress);
    vector<int> a,b;
    scanf("%d%d%d",&n,&m,&w);
    for(i=0;i<n;i++){
        dis[i]=i;
        scanf("%d",&aux);
        a.push_back(aux);
    }
    for(i=0;i<n;i++){
        scanf("%d",&aux);
        b.push_back(aux);
    }
    for(i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        unir(x-1,y-1);
    }
    map<int, pii> mm;
    vector<int> indd;
    for(i=0;i<n;i++){
        if(mm.count(enc(i))==0)indd.push_back(enc(i));
        mm[enc(i)].first+=a[i];
        mm[enc(i)].second+=b[i];
        v.push_back(mp(enc(i), mp(a[i], b[i])));
        nex.push_back(-1);
    }
    for(i=0;i<indd.size();i++){
        v.push_back(mp(indd[i], mm[indd[i]]));
        nex.push_back(-1);
    }
    sort(v.begin(), v.end());
    int pos = v.back().first;
    int ne = v.size();
    for(i=v.size()-1;i>=0;i--){
        if(v[i].first!=pos){
            ne = i+1;
            pos = v[i].first;
        }
        nex[i]=ne;
        //cout<<i<<" "<<v[i].first<<" "<<v[i].second.first<<" "<<v[i].second.second<<" "<<nex[i]<<endl;
    }
    cout<<dinamica(0, w)<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int, ll>
#define pipi pair<int, pair<int, ll> >
#define mp make_pair
using namespace std;

vector<pipi> v;
vector<int> nex;
ll ress[2005][1005];
int dis[1005];

int enc(int x){
    if(dis[x]==x)return x;
    return dis[x] = enc(dis[x]);
}

void unir(int x, int y){
    x = enc(x);
    y = enc(y);
    dis[y] = x;
    return;
}

ll dinamica(int pos, int peso){
    if(pos>=v.size())return 0;
    if(ress[pos][peso]!=-1) return ress[pos][peso];
    ll res = 0;
    if(peso>=v[pos].second.first){
        res = dinamica(nex[pos], peso - v[pos].second.first) + v[pos].second.second;
    }
    return ress[pos][peso] = max(res, dinamica(pos+1, peso));
}

int main(){
    int i, n, m, w, aux, x, y;
    memset(ress, -1, sizeof ress);
    vector<int> a,b;
    scanf("%d%d%d",&n,&m,&w);
    for(i=0;i<n;i++){
        dis[i]=i;
        scanf("%d",&aux);
        a.push_back(aux);
    }
    for(i=0;i<n;i++){
        scanf("%d",&aux);
        b.push_back(aux);
    }
    for(i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        unir(x-1,y-1);
    }
    map<int, pii> mm;
    vector<int> indd;
    for(i=0;i<n;i++){
        if(mm.count(enc(i))==0)indd.push_back(enc(i));
        mm[enc(i)].first+=a[i];
        mm[enc(i)].second+=b[i];
        v.push_back(mp(enc(i), mp(a[i], b[i])));
        nex.push_back(-1);
    }
    for(i=0;i<indd.size();i++){
        v.push_back(mp(indd[i], mm[indd[i]]));
        nex.push_back(-1);
    }
    sort(v.begin(), v.end());
    int pos = v.back().first;
    int ne = v.size();
    for(i=v.size()-1;i>=0;i--){
        if(v[i].first!=pos){
            ne = i+1;
            pos = v[i].first;
        }
        nex[i]=ne;
        cout<<i<<" "<<v[i].first<<" "<<v[i].second.first<<" "<<v[i].second.second<<" "<<nex[i]<<endl;
    }
    cout<<dinamica(0, w)<<endl;
}


#include<bits/stdc++.h>
#define ll long long
#define pii pair<int, ll>
#define pipi pair<int, pair<int, ll> >
#define mp make_pair
using namespace std;

vector<pipi> v;
vector<int> nex;
ll ress[1005][1005];
int dis[1005];

int enc(int x){
    if(dis[x]==x)return x;
    return dis[x] = enc(dis[x]);
}

void unir(int x, int y){
    x = enc(x);
    y = enc(y);
    dis[y] = x;
    return;
}

ll dinamica(int pos, int peso){
    if(pos>=v.size())return 0;
    if(ress[pos][peso]!=-1) return ress[pos][peso];
    ll res = 0;
    if(peso>=v[pos].second.first){
        res = dinamica(nex[pos], peso - v[pos].second.first) + v[pos].second.second;
    }
    return ress[pos][peso] = max(res, dinamica(pos+1, peso));
}

int main(){
    int i, n, m, w, aux, x, y;
    memset(ress, -1, sizeof ress);
    vector<int> a,b;
    scanf("%d%d%d",&n,&m,&w);
    for(i=0;i<n;i++){
        dis[i]=i;
        scanf("%d",&aux);
        a.push_back(aux);
    }
    for(i=0;i<n;i++){
        scanf("%d",&aux);
        b.push_back(aux);
    }
    for(i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        unir(x-1,y-1);
    }
    map<int, pii> mm;
    vector<int> indd;
    for(i=0;i<n;i++){
        if(mm.count(enc(i))==0)indd.push_back(enc(i));
        mm[enc(i)].first+=a[i];
        mm[enc(i)].second+=b[i];
        v.push_back(mp(enc(i), mp(a[i], b[i])));
        nex.push_back(-1);
    }
    for(i=0;i<indd.size();i++){
        v.push_back(mp(indd[i], mm[indd[i]]));
        nex.push_back(-1);
    }
    sort(v.begin(), v.end());
    int pos = v.back().first;
    int ne = v.size();
    for(i=v.size()-1;i>=0;i--){
        //cout<<i<<" "<<v[i].first<<" "<<v[i].second.first<<" "<<v[i].second.second<<endl;
        if(v[i].first!=pos){
            ne = i+1;
            pos = v[i].first;
        }
        nex[i]=ne;
    }
    cout<<dinamica(0, w)<<endl;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 1003;
int N, M, W, S[maxn], R[maxn], beauty[maxn], peso[maxn], b[maxn], w[maxn];
int dp[maxn][maxn];
int find_set( int nodo )
{
    if( S[nodo] != nodo )
        S[nodo] = find_set(S[nodo]);
    return S[nodo];
}

void join_set( int a, int b ){
    if( R[a] < R[b] )
        swap(a,b);
    R[a] += R[b];
    S[b] = S[a];
}

vector<int>sets[maxn];

int marks[maxn];

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm

    cin >> N >> M >> W;
    for( int i = 1; i <= N; i++ )
        cin >> peso[i];

    for( int i = 1; i <= N; i++ )
        cin >> b[i];

    for( int i = 1; i <= N; i++ )
        S[i] = i, R[i] = 1;

    int a, c;
    for( int i = 1; i <= M; i++ )
    {
        cin >> a >> c;
        int seta = find_set(a);
        int setb = find_set(c);
        if( seta != setb )
            join_set(seta,setb);
    }

    int nsets = 0;
    for( int i = 1; i <= N; i++ ){
        int si = find_set(i);
        if( !marks[si] )
            marks[si] = ++nsets;
        S[i] = marks[si];
        beauty[marks[si]] += b[i];
        sets[marks[si]].push_back(i);
        w[marks[si]] += peso[i];
    }

    for( int i = 1; i <= W; i++ )
        dp[0][i] = -1<<30;
    dp[0][0] = 0;
    for( int i = 1; i <= nsets; i++ )
    {
        for( int j = 0; j <= W; j++ )
            dp[i][j] = dp[i-1][j];
        for( int j = W; j >= w[i]; j-- )
            dp[i][j] = max(dp[i][j],dp[i-1][j-w[i]]+beauty[i]);
        for( auto j : sets[i] )
        {
            for( int k = W; k >= peso[j]; k-- )
                dp[i][k] = max(dp[i][k], dp[i-1][k-peso[j]]+b[j]);
        }
    }

    int sol = -1;

    for( int i = 0; i <= W; i++ )
        sol = max(sol,dp[nsets][i]);

    cout << sol;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 1003;
int N, M, W, S[maxn], R[maxn], beauty[maxn], peso[maxn], b[maxn], w[maxn];
int dp[maxn][maxn];
bool can[maxn][maxn];
int find_set( int nodo )
{
    if( S[nodo] != nodo )
        S[nodo] = find_set(S[nodo]);
    return S[nodo];
}

void join_set( int a, int b ){
    if( R[a] < R[b] )
        swap(a,b);
    R[a] += R[b];
    S[b] = S[a];
}

vector<int>sets[maxn];

int marks[maxn];

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm

    cin >> N >> M >> W;
    for( int i = 1; i <= N; i++ )
        cin >> peso[i];

    for( int i = 1; i <= N; i++ )
        cin >> b[i];

    for( int i = 1; i <= N; i++ )
        S[i] = i, R[i] = 1;

    int a, c;
    for( int i = 1; i <= M; i++ )
    {
        cin >> a >> c;
        int seta = find_set(a);
        int setb = find_set(c);
        if( seta != setb )
            join_set(seta,setb);
    }

    int nsets = 0;

    for( int i = 1; i <= N; i++ ){
        int si = find_set(i);
        if( !marks[si] )
            marks[si] = ++nsets;
        beauty[marks[si]] += b[i];
        sets[marks[si]].push_back(i);
        w[marks[si]] += peso[i];
    }

    dp[0][0] = 0;
    can[0][0] = 1;
    for( int i = 1; i <= nsets; i++ )
    {
        can[i][0] = 1;
        for( int j = W; j >= w[i]; j-- )
        {
            if( can[i-1][j] )
            {
                can[i][j] = 1;
                dp[i][j] = dp[i-1][j];
            }
            if( can[i-1][j-w[i]] )
            {
                can[i][j] = 1;
                dp[i][j] = max(dp[i][j],dp[i-1][j-w[i]]+beauty[i]);
            }
        }
        for( auto j : sets[i] )
        {
            for( int k = W; k >= peso[j]; k-- )
            {
                if( can[i-1][k] ){
                    can[i][k] = 1;
                    dp[i][k] = max(dp[i][k],dp[i-1][k]);
                }
                if( can[i-1][k-peso[j]] )
                {
                    can[i][k] = 1;
                    dp[i][k] = max(dp[i][k],dp[i-1][k-peso[j]]+b[j]);
                }
            }
        }
    }

    int sol = 0;

    for( int i = 0; i <= W; i++ )
        sol = max(sol,dp[nsets][i]);

    cout << sol;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 1003;
int N, M, W, S[maxn], R[maxn], beauty[maxn], peso[maxn], b[maxn], w[maxn];
int dp[maxn][maxn];
bool can[maxn][maxn];
int find_set( int nodo )
{
    if( S[nodo] != nodo )
        S[nodo] = find_set(S[nodo]);
    return S[nodo];
}

void join_set( int a, int b ){
    if( R[a] < R[b] )
        swap(a,b);
    R[a] += R[b];
    S[b] = S[a];
}

vector<int>sets[maxn];

int marks[maxn];

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm

    cin >> N >> M >> W;
    for( int i = 1; i <= N; i++ )
        cin >> peso[i];

    for( int i = 1; i <= N; i++ )
        cin >> b[i];

    for( int i = 1; i <= N; i++ )
        S[i] = i, R[i] = 1;

    int a, c;
    for( int i = 1; i <= M; i++ )
    {
        cin >> a >> c;
        int seta = find_set(a);
        int setb = find_set(c);
        if( seta != setb )
            join_set(seta,setb);
    }

    int nsets = 0;

    for( int i = 1; i <= N; i++ ){
        int si = find_set(i);
        if( !marks[si] )
            marks[si] = ++nsets;
        beauty[marks[si]] += b[i];
        sets[marks[si]].push_back(i);
        w[marks[si]] += peso[i];
    }

    dp[0][0] = 0;
    can[0][0] = 1;
    for( int i = 1; i <= nsets; i++ )
    {
        for( int j = 0; j <= W; j++ )
            if( can[i-1][j] )
            {
                can[i][j] = 1;
                dp[i][j] = dp[i-1][j];
            }
        for( int j = W; j >= w[i]; j-- )
        {
            if( can[i-1][j-w[i]] )
            {
                can[i][j] = 1;
                dp[i][j] = max(dp[i][j],dp[i-1][j-w[i]]+beauty[i]);
            }
        }
        for( auto j : sets[i] )
        {
            for( int k = W; k >= peso[j]; k-- )
            {
                if( can[i-1][k-peso[j]] )
                {
                    can[i][k] = 1;
                    dp[i][k] = max(dp[i][k],dp[i-1][k-peso[j]]+b[j]);
                }
            }
        }
    }

    int sol = 0;

    for( int i = 0; i <= W; i++ )
        sol = max(sol,dp[nsets][i]);

    cout << sol;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 1003;
int N, M, W, S[maxn], R[maxn], beauty[maxn], peso[maxn], b[maxn], w[maxn];
int dp[maxn][maxn];
bool can[maxn][maxn];
int find_set( int nodo )
{
    if( S[nodo] != nodo )
        S[nodo] = find_set(S[nodo]);
    return S[nodo];
}

void join_set( int a, int b ){
    if( R[a] < R[b] )
        swap(a,b);
    R[a] += R[b];
    S[b] = S[a];
}

vector<int>sets[maxn];

int marks[maxn];

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm

    cin >> N >> M >> W;
    for( int i = 1; i <= N; i++ )
        cin >> peso[i];

    for( int i = 1; i <= N; i++ )
        cin >> b[i];

    for( int i = 1; i <= N; i++ )
        S[i] = i, R[i] = 1;

    int a, c;
    for( int i = 1; i <= M; i++ )
    {
        cin >> a >> c;
        int seta = find_set(a);
        int setb = find_set(c);
        if( seta != setb )
            join_set(seta,setb);
    }

    int nsets = 0;
    for( int i = 1; i <= N; i++ ){
        int si = find_set(i);
        if( !marks[si] )
            marks[si] = ++nsets;
        S[i] = marks[si];
        beauty[marks[si]] += b[i];
        sets[marks[si]].push_back(i);
        w[marks[si]] += peso[i];
    }

    dp[0][0] = 0;
    can[0][0] = 1;
    for( int i = 1; i <= nsets; i++ )
    {
        can[i][0] = 1;
        for( int j = W; j >= w[i]; j-- )
        {
            if( can[i-1][j] )
            {
                can[i][j] = 1;
                dp[i][j] = dp[i-1][j];
            }
            if( can[i-1][j-w[i]] )
            {
                can[i][j] = 1;
                dp[i][j] = max(dp[i][j],dp[i-1][j-w[i]]+beauty[i]);
            }
        }
        for( auto j : sets[i] )
        {
            for( int k = W; k >= peso[j]; k-- )
            {
                if( can[i-1][k] ){
                    can[i][k] = 1;
                    dp[i][k] = max(dp[i][k],dp[i-1][k]);
                }
                if( can[i-1][k-peso[j]] )
                {
                    can[i][k] = 1;
                    dp[i][k] = max(dp[i][k],dp[i-1][k-peso[j]]+b[j]);
                }
            }
        }
    }

    int sol = 0;

    for( int i = 0; i <= W; i++ )
        sol = max(sol,dp[nsets][i]);

    cout << sol;

    return 0;
}


# include <iostream>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <vector>
using namespace std;
const int maxn = 1000 + 5;
int w[maxn], b[maxn];
int f[maxn];
int vis[maxn];
int id[maxn];
int dp[maxn][maxn];
int n, m, v;
int idd;

struct Node {
    int w, b;
    Node() : w(0), b(0) { }
    Node(int w, int b) : w(w), b(b) { }
};

vector<Node> jh[maxn];

int Find(int x) {
    int r = x;
    while (r != f[r]) r = f[r];
    while (x != r) {
        int y = f[x];
        x = f[x];
        f[y] = r;
    }
    return r;
}

void Merge(int x, int y) {
    int a = Find(x), b = Find(y);
    if (a < b) f[a] = b;
    else if (a > b) f[b] = a;
}

void init() {
    idd = 0;
    memset(vis, 0, sizeof vis);
    memset(id, -1, sizeof id);
    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= n; ++i) f[i] = i;
    for (int i = 0; i < maxn; ++i) jh[i].clear();
}

int main(void)
{
    while (~scanf("%d %d %d", &n, &m, &v)) {
        init();
        for (int i = 1; i <= n; ++i) scanf("%d", w + i);
        for (int i = 1; i <= n; ++i) scanf("%d", b + i);
        for (int i = 0; i < m; ++i) {
            int u, v; scanf("%d %d", &u, &v);
            int fu = Find(u), fv = Find(v);
            if (fu != fv) Merge(u, v);
        }
        for (int i = 1; i <= n; ++i) {
            int fi = Find(i);
            if (id[fi] == -1) id[fi] = idd++;
            jh[id[fi]].push_back(Node(w[i], b[i]));
        }
        for (int i = 0; i < idd; ++i) {
            if (jh[i].size() == 1) continue;
            int sw = 0, sb = 0;
            for (int j = 0; j < jh[i].size(); ++j) {
                sw += jh[i][j].w; sb += jh[i][j].b;
            }
            jh[i].push_back(Node(sw, sb));
        }

        for (int j = 0; j < jh[0].size(); ++j) {
            dp[0][v - jh[0][j].w] = max(dp[0][v - jh[0][j].w], jh[0][j].b);
        }

        for (int i = 1; i < idd; ++i) {
            for (int vv = 0; vv <= v; ++vv) {
                dp[i][vv] = dp[i - 1][vv];
                for (int j = 0; j < jh[i].size(); ++j) {
                    dp[i][vv] = max(dp[i][vv], dp[i - 1][vv + jh[i][j].w] + jh[i][j].b);
                }
            }
        }
        int ans = 0;
        for (int i = 0; i <= v; ++i) ans = max(ans, dp[idd - 1][i]);
        printf("%d\n", ans);
    }

    return 0;
}


# include <iostream>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <vector>
using namespace std;
const int maxn = 1000 + 5;
int w[maxn], b[maxn];
int f[maxn];
int vis[maxn];
int id[maxn];
int dp[maxn][maxn];
int n, m, v;
int idd;

struct Node {
    int w, b;
    Node() : w(0), b(0) { }
    Node(int w, int b) : w(w), b(b) { }
};

vector<Node> jh[maxn];

int Find(int x) {
    int r = x;
    while (r != f[r]) r = f[r];
    while (x != r) {
        int y = f[x];
        x = f[x];
        f[y] = r;
    }
    return r;
}

void Merge(int x, int y) {
    int a = Find(x), b = Find(y);
    if (a < b) f[a] = b;
    else if (a > b) f[b] = a;
}

void init() {
    idd = 0;
    memset(vis, 0, sizeof vis);
    memset(id, -1, sizeof id);
    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= n; ++i) f[i] = i;
    for (int i = 0; i < maxn; ++i) jh[i].clear();
}

void fdp();

int main(void)
{
    while (~scanf("%d %d %d", &n, &m, &v)) {
        init();
        for (int i = 1; i <= n; ++i) scanf("%d", w + i);
        for (int i = 1; i <= n; ++i) scanf("%d", b + i);
        for (int i = 0; i < m; ++i) {
            int u, v; scanf("%d %d", &u, &v);
            int fu = Find(u), fv = Find(v);
            if (fu != fv) Merge(u, v);
        }
        for (int i = 1; i <= n; ++i) {
            int fi = Find(i);
            if (id[fi] == -1) id[fi] = idd++;
            jh[id[fi]].push_back(Node(w[i], b[i]));
        }
        for (int i = 0; i < idd; ++i) {
            if (jh[i].size() == 1) continue;
            int sw = 0, sb = 0;
            for (int j = 0; j < jh[i].size(); ++j) {
                sw += jh[i][j].w; sb += jh[i][j].b;
            }
            jh[i].push_back(Node(sw, sb));
        }

        fdp();
        int ans = 0;
        for (int i = 0; i <= v; ++i) ans = max(ans, dp[idd - 1][i]);
        printf("%d\n", ans);
    }

    return 0;
}

void fdp() {
    for (int j = 0; j < jh[0].size(); ++j)
        if (v > jh[0][j].w) dp[0][v - jh[0][j].w] = max(dp[0][v - jh[0][j].w], jh[0][j].b);

    for (int i = 1; i < idd; ++i) {
        for (int vv = 0; vv <= v; ++vv) {
            dp[i][vv] = dp[i - 1][vv];
            for (int j = 0; j < jh[i].size(); ++j) {
                if (vv + jh[i][j].w <= v)
                    dp[i][vv] = max(dp[i][vv], dp[i - 1][vv + jh[i][j].w] + jh[i][j].b);
            }
        }
    }
}


# include <iostream>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <vector>
using namespace std;
const int maxn = 1000 + 5;
int w[maxn], b[maxn];
int f[maxn];
int vis[maxn];
int id[maxn];
int dp[maxn][maxn];
int n, m, v;
int idd;

struct Node {
    int w, b;
    Node() : w(0), b(0) { }
    Node(int w, int b) : w(w), b(b) { }
};

vector<Node> jh[maxn];

int Find(int x) {
    int r = x;
    while (r != f[r]) r = f[r];
    while (x != r) {
        int y = f[x];
        x = f[x];
        f[y] = r;
    }
    return r;
}

void Merge(int x, int y) {
    int a = Find(x), b = Find(y);
    if (a < b) f[a] = b;
    else if (a > b) f[b] = a;
}

void init() {
    idd = 0;
    memset(vis, 0, sizeof vis);
    memset(id, -1, sizeof id);
    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= n; ++i) f[i] = i;
    for (int i = 0; i < maxn; ++i) jh[i].clear();
}

void fdp();

int main(void)
{
    while (~scanf("%d %d %d", &n, &m, &v)) {
        init();
        for (int i = 1; i <= n; ++i) scanf("%d", w + i);
        for (int i = 1; i <= n; ++i) scanf("%d", b + i);
        for (int i = 0; i < m; ++i) {
            int u, v; scanf("%d %d", &u, &v);
            int fu = Find(u), fv = Find(v);
            if (fu != fv) Merge(u, v);
        }
        for (int i = 1; i <= n; ++i) {
            int fi = Find(i);
            if (id[fi] == -1) id[fi] = idd++;
            jh[id[fi]].push_back(Node(w[i], b[i]));
        }
        for (int i = 0; i < idd; ++i) {
            if (jh[i].size() == 1) continue;
            int sw = 0, sb = 0;
            for (int j = 0; j < jh[i].size(); ++j) {
                sw += jh[i][j].w; sb += jh[i][j].b;
            }
            jh[i].push_back(Node(sw, sb));
        }

        fdp();
        int ans = 0;
        for (int i = 0; i <= v; ++i) ans = max(ans, dp[idd - 1][i]);
        printf("%d\n", ans);
    }

    return 0;
}

void fdp() {
    for (int j = 0; j < jh[0].size(); ++j) {
        dp[0][v - jh[0][j].w] = max(dp[0][v - jh[0][j].w], jh[0][j].b);
    }

    for (int i = 1; i < idd; ++i) {
        for (int vv = 0; vv <= v; ++vv) {
            dp[i][vv] = dp[i - 1][vv];
            for (int j = 0; j < jh[i].size(); ++j) {
                if (vv + jh[i][j].w <= v)
                    dp[i][vv] = max(dp[i][vv], dp[i - 1][vv + jh[i][j].w] + jh[i][j].b);
            }
        }
    }
}