#include<bits/stdc++.h>
using namespace std;
long long int n,w,dp[101][101];
int h[100],v[100];
void scan()
{
for(int i=0;i<n;i++)
{
cin>>h[i];cin>>v[i];
}
cin>>w;
}
int f(int i,int j)
{
if(dp[i][j]!=-1)return dp[i][j];
if(i==n)return 0;
else if(j<h[i])
{
return dp[i][j]=f(i+1,j);
}
else
{
return dp[i][j]=max(f(i+1,j),f(i+1,j-h[i])+v[i]);
}
}
int main(void)
{
while(cin>>n)
{
memset(dp,-1,sizeof(dp));
scan();
cout<<f(0,w)<<endl;
}
return 0;
}