#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& num) {
  bool start = false, neg = false;
  char c;
  num = 0;
  while ((c = getchar()) != EOF) {
    if (c == '-')
      start = neg = true;
    else if (c >= '0' && c <= '9') {
      start = true;
      num = num * 10 + c - '0';
    } else if (start)
      break;
  }
  if (neg) num = -num;
}
const int maxn = 405;
const long long INF = (long long)(1e18);
int ai[maxn], vi[maxn];
long long f[maxn][maxn][3], g[maxn];
inline void chkmax(long long& x, long long v) { x = max(x, v); }
int main() {
  int n;
  read(n);
  for (int i = (1); i <= (n); i++) read(vi[i]);
  for (int i = (1); i <= (n); i++) read(ai[i]);
  for (int p = (0); p <= (n - 1); p++) {
    for (int i = (1); i <= (n - p); i++) {
      int j = i + p;
      f[i][j][0] = -INF;
      f[i][j][1] = -INF;
      f[i][j][2] = -INF;
      if (i == j) {
        f[i][j][0] = f[i][j][1] = 0;
        f[i][j][2] = vi[1];
      } else {
        if (ai[i] + 1 == ai[i + 1]) {
          chkmax(f[i][j][0], f[i + 1][j][0]);
        }
        if (ai[j - 1] + 1 == ai[j]) {
          chkmax(f[i][j][0], f[i][j - 1][0]);
        }
        if (ai[i] - 1 == ai[i + 1]) {
          chkmax(f[i][j][1], f[i + 1][j][1]);
        }
        if (ai[j - 1] - 1 == ai[j]) {
          chkmax(f[i][j][1], f[i][j - 1][1]);
        }
        for (int k = (i + 1); k <= (j - 1); k++) {
          if (ai[k - 1] + 1 == ai[j])
            chkmax(f[i][j][0], f[i][k - 1][0] + f[k][j - 1][2]);
          if (ai[i] + 1 == ai[k + 1])
            chkmax(f[i][j][0], f[k + 1][j][0] + f[i + 1][k][2]);
        }
        for (int k = (i + 1); k <= (j - 1); k++) {
          if (ai[k - 1] - 1 == ai[j])
            chkmax(f[i][j][1], f[i][k - 1][1] + f[k][j - 1][2]);
          if (ai[i] - 1 == ai[k + 1])
            chkmax(f[i][j][1], f[k + 1][j][1] + f[i + 1][k][2]);
        }
        if (ai[i] <= ai[j] && ai[j] - ai[i] + 1 <= n)
          chkmax(f[i][j][2], f[i][j][0] + vi[ai[j] - ai[i] + 1]);
        if (ai[i] >= ai[j] && ai[i] - ai[j] + 1 <= n)
          chkmax(f[i][j][2], f[i][j][1] + vi[ai[i] - ai[j] + 1]);
        for (int k = (i); k <= (j - 1); k++) {
          chkmax(f[i][j][2], f[i][k][2] + f[k + 1][j][2]);
        }
        for (int k = (i + 1); k <= (j - 1); k++)
          if (ai[k] >= ai[i] && ai[k] >= ai[j] &&
              ai[k] - ai[i] + ai[k] - ai[j] + 1 <= n) {
            chkmax(f[i][j][2], f[i][k][0] + f[k][j][1] +
                                   vi[ai[k] - ai[i] + ai[k] - ai[j] + 1]);
          }
      }
    }
  }
  for (int i = (1); i <= (n); i++) {
    g[i] = g[i - 1];
    for (int j = (1); j <= (i); j++) {
      chkmax(g[i], g[j - 1] + f[j][i][2]);
    }
  }
  printf("%lld\n", g[n]);
  return 0;
}
