#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> tag(n);
  for (int j = 0; j < n; j++) {
    cin >> tag[j];
  }
  vector<int> s(n);
  for (int j = 0; j < n; j++) {
    cin >> s[j];
  }
  vector<int64_t> dp(n, 0);
  for (int j = 1; j < n; j++) {
    for (int k = j - 1; k >= 0; k--) {
      if (tag[j] != tag[k]) {
        int64_t tmp1 = dp[k] + abs(s[k] - s[j]);
        int64_t tmp2 = dp[j] + abs(s[k] - s[j]);
        dp[j] = max(dp[j], tmp1);
        dp[k] = max(dp[k], tmp2);
      }
    }
  }
  int64_t ans = 0;
  for (int j = 0; j < n; j++) {
    ans = max(ans, dp[j]);
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T = 1;
  while (T--) {
    solve();
  }
}
