#include <bits/stdc++.h>
using namespace std;
const int N = 405;
const int INF = 1e9;
inline int read() {
  int x = 0;
  int w = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - 48, ch = getchar();
  }
  return x * w;
}
inline int mx(int a, int b) { return a > b ? a : b; }
inline int mn(int a, int b) { return a < b ? a : b; }
int w[N], v[N];
int f[N][N], g[N][N], h[N][N];
int ans[N];
inline void Debug(int i, int j, int condition) {
  printf("%d->%d : %d(%d)\n", i, j, f[i][j], condition);
}
int main() {
  int n;
  n = read();
  for (int i = 1; i <= n; i++) v[i] = read();
  for (int i = 1; i <= n; i++) w[i] = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) f[i][j] = g[i][j] = h[i][j] = -INF;
  for (register int i = n; i >= 1; i--) {
    f[i][i] = v[1], g[i][i] = h[i][i] = 0;
    for (register int j = i + 1; j <= n; j++) {
      for (register int k = i; k < j - 1; k++) {
        if (w[k] + 1 == w[j])
          g[i][j] = mx(g[i][k] + f[k + 1][j - 1], g[i][j]);
        else if (w[k] - 1 == w[j])
          h[i][j] = mx(h[i][k] + f[k + 1][j - 1], h[i][j]);
      }
      if (w[j - 1] + 1 == w[j])
        g[i][j] = mx(g[i][j], g[i][j - 1]);
      else if (w[j - 1] - 1 == w[j])
        h[i][j] = mx(h[i][j], h[i][j - 1]);
    }
    for (register int j = i; j <= n; j++) {
      if (w[j] - w[i] >= 0 && w[j] - w[i] < n)
        f[i][j] = mx(f[i][j], g[i][j] + v[w[j] - w[i] + 1]);
      if (w[i] - w[j] >= 0 && w[i] - w[j] < n)
        f[i][j] = mx(f[i][j], h[i][j] + v[w[i] - w[j] + 1]);
      for (register int k = i; k < j; k++) {
        f[i][j] = mx(f[i][k] + f[k + 1][j], f[i][j]);
      }
      for (register int k = i + 1; k < j; k++) {
        if (2 * w[k] - w[i] - w[j] >= 0 && 2 * w[k] - w[i] - w[j] < n)
          f[i][j] =
              mx(f[i][j], g[i][k] + h[k][j] + v[2 * w[k] - w[i] - w[j] + 1]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ans[i] = ans[i - 1];
    for (int j = 0; j < i; j++) {
      ans[i] = mx(ans[j] + f[j + 1][i], ans[i]);
    }
  }
  printf("%d\n", ans[n]);
  return 0;
}
