#include <iostream>
using namespace std;

int main(void) {
  int i,j, m[10], profit[10],k, number, max_expected_profit[10],max_profit;

  cout << "Enter the Number Of Locations  ";  
  cin >> number;

  cout << "Enter the all miles m[i] separated by spaces ";
  for (i=1;i<= number; i++)
    cin >> m[i];

  m[0] = 0;

  cout << " Input Distances Are \n";
  
  for (i=1;i<= number; i++)
    cout << m[i] << " ," ;
    cout << endl;
    cout << "Input profits separated by spaces " ;
    for (i=1; i<= number; i++)
      cin >> profit[i];
    cout << " Enter the constraint on miles apart :  ";
    cin >> k;
    cout << " Constraint Distance " << k << endl;

    cout << " Profits at " << endl;
    for (i=1;i<= number; i++)
      cout << i << " are " <<profit[i] << endl; ;


   
    for (i= 1; i <= number ; i++)
      { 
	max_expected_profit[i] = profit[i];
	for (j = 1; j < i; j++)
	  {
	    if ((m[i] - m [j]) >= k )
	      if (max_expected_profit[i] < (max_expected_profit[j]+profit[i]))
		max_expected_profit[i] = max_expected_profit[j]+profit[i];
	  }
      }

    max_profit= 0;
    for (i=1; i <=number ; i++)
      if (max_profit < max_expected_profit[i])
	max_profit = max_expected_profit[i];
    cout << max_profit << endl;
}
