#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char ch = getchar();
  long long x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long dp[410][410], l[410][410], r[410][410], f[410][410];
long long v[410], w[410], res[410];
inline int check(int i, int j, int k) {
  if (i == k || j == k) return 2;
  return 3;
}
int main() {
  int 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 <= (i - 1); j++) v[i] = max(v[i], v[j] + v[i - j]);
  }
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (n); j++)
      dp[i][j] = l[i][j] = r[i][j] = f[i][j] = -1e13;
  for (int i = (1); i <= (n); i++) {
    dp[i][i] = f[i][i] = v[1];
    l[i][i] = r[i][i] = 0;
    dp[i][i - 1] = 0;
  }
  for (int len = (1); len <= (n - 1); len++) {
    for (int i = (1); i <= (n - len); i++) {
      int j = i + len;
      for (int k = (i); k <= (j); k++) {
        if (w[k] == w[j] - 1) {
          l[i][j] = max(l[i][j], l[i][k] + dp[k + 1][j - 1]);
        }
      }
      for (int k = (i); k <= (j); k++) {
        if (w[k] == w[i] - 1) {
          r[i][j] = max(r[i][j], r[k][j] + dp[i + 1][k - 1]);
        }
      }
      for (int k = (i); k <= (j); k++) {
        if (2 * w[k] - w[i] - w[j] + 1 >= check(i, j, k) &&
            2 * w[k] - w[i] - w[j] + 1 <= len + 1) {
          f[i][j] =
              max(f[i][j], l[i][k] + r[k][j] + v[2 * w[k] - w[i] - w[j] + 1]);
        }
      }
      for (int k = (i); k <= (j); k++) {
        dp[i][j] = max(dp[i][j], f[i][k] + dp[k + 1][j]);
      }
    }
  }
  for (int i = (1); i <= (n); i++) {
    res[i] = res[i - 1];
    for (int j = (0); j <= (i - 1); j++) {
      res[i] = max(res[i], res[j] + max(0ll, dp[j + 1][i]));
    }
  }
  cout << res[n] << endl;
  return 0;
}
