#include <bits/stdc++.h>
using namespace std;
const int N = 505, inf = -1e8;
int n, a[N], val[N], f[N][N], g[N][N], h[N][N], ans[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++) f[i][j] = g[i][j] = h[i][j] = inf;
  for (int i = 1; i <= n; i++) ans[i] = inf;
  for (int i = 1; i <= n; i++) f[i][i] = val[1], g[i][i] = h[i][i] = 0;
  for (int l = 2; l <= n; l++)
    for (int i = 1; i <= n; i++) {
      int j = i + l - 1;
      if (j > n) break;
      for (int k = i + 1; k <= j; k++) {
        if (a[k] == a[i] + 1 && g[k][j] > inf)
          g[i][j] = max(g[i][j], g[k][j] + (i > k - 2 ? 0 : f[i + 1][k - 1]));
        if (a[k] == a[i] - 1 && h[k][j] > inf)
          h[i][j] = max(h[i][j], h[k][j] + (i > k - 2 ? 0 : f[i + 1][k - 1]));
        f[i][j] = max(f[i][j], f[i][k - 1] + f[k][j]);
      }
      for (int k = i; k <= j; k++)
        if (g[i][k] > inf && h[k][j] > inf)
          f[i][j] =
              max(f[i][j], g[i][k] + h[k][j] + val[2 * a[k] - a[i] - a[j] + 1]);
    }
  for (int i = 1; i <= n; i++) {
    ans[i] = ans[i - 1];
    for (int j = 1; j <= i; j++) ans[i] = max(ans[i], ans[j - 1] + f[j][i]);
  }
  printf("%d\n", ans[n]);
  return 0;
}
