#include <bits/stdc++.h>
using namespace std;
int n, i, j, k, l, f[402][402], g[402][402], h[402][402], a[402], v[402],
    ans[402];
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &v[i]);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (l = 1; l <= n; l++) {
    for (i = 1; i + l - 1 <= n; i++) {
      j = i + l - 1;
      if (i == j) {
        g[i][j] = h[i][j] = 0;
        f[i][j] = v[1];
        continue;
      }
      g[i][j] = h[i][j] = -1e9;
      for (k = i + 1; k <= j; k++)
        if (a[i] + 1 == a[k])
          g[i][j] = max(g[i][j], f[i + 1][k - 1] + g[k][j]);
        else if (a[i] - 1 == a[k])
          h[i][j] = max(h[i][j], f[i + 1][k - 1] + h[k][j]);
      f[i][j] = -1e9;
      for (k = i; k < j; k++) f[i][j] = max(f[i][j], f[i][k] + f[k + 1][j]);
      for (k = i; k <= j; k++)
        if (g[i][k] > -1e8 && h[k][j] > -1e8)
          f[i][j] =
              max(f[i][j], g[i][k] + h[k][j] + v[a[k] * 2 - a[i] - a[j] + 1]);
    }
  }
  for (i = 1; i <= n; i++) {
    ans[i] = ans[i - 1];
    for (j = 0; j < i; j++) ans[i] = max(ans[i], ans[j] + f[j + 1][i]);
  }
  printf("%d", ans[n]);
}
