#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int sum = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  if (f) return -sum;
  return sum;
}
const int N = 405;
const int INF = 1e9 + 7;
const int dnf = 1e6;
int n, v[N], a[N], g[N];
int f[N][N], up[N][N], dn[N][N];
inline void upd(int &x, int y) {
  if (y > x) x = y;
}
int main() {
  int i, j, k, t;
  n = read();
  for (i = 1; i <= n; i++) v[i] = read();
  for (i = 1; i <= n; i++) a[i] = read();
  for (i = 1; i <= n; i++) f[i][i] = v[1];
  for (t = 2; t <= n; t++)
    for (i = 1; i + t - 1 <= n; i++) {
      j = i + t - 1;
      up[i][j] = dn[i][j] = f[i][j] = -INF;
      for (k = i + 1; k <= j; k++)
        if (a[k] == a[i] + 1)
          upd(up[i][j], f[i + 1][k - 1] + up[k][j]);
        else if (a[k] == a[i] - 1)
          upd(dn[i][j], f[i + 1][k - 1] + dn[k][j]);
      for (k = i; k < j; k++) upd(f[i][j], f[i][k] + f[k + 1][j]);
      for (k = i; k <= j; k++)
        if (up[i][k] > -dnf && dn[k][j] > -dnf)
          upd(f[i][j], up[i][k] + dn[k][j] + v[(a[k] << 1) - a[i] - a[j] + 1]);
    }
  for (i = 1; i <= n; i++)
    for (g[i] = g[i - 1], j = 1; j <= i; j++) upd(g[i], g[j - 1] + f[j][i]);
  printf("%d", g[n]);
  return 0;
}
