#include <bits/stdc++.h>
using namespace std;
const int N = 405;
const int INF = (int)1e9;
int n, v[N], a[N];
int f[N][N], g[N][N], h[N][N], dp[N];
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) {
    dp[i] = -INF;
    for (int j = 0; j <= n; j++) f[i][j] = g[i][j] = h[i][j] = -INF;
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), f[i][i] = v[1], g[i][i] = h[i][i] = 0;
  for (int L = 2; L <= n; L++) {
    for (int i = 1; i <= n - L + 1; i++) {
      int j = i + L - 1;
      for (int k = i + 1; k <= j; k++) {
        if (a[k] == a[i] + 1 && g[k][j] != -INF) {
          if (i + 1 <= k - 1)
            g[i][j] = max(g[i][j], f[i + 1][k - 1] + g[k][j]);
          else
            g[i][j] = max(g[i][j], g[k][j]);
        }
        if (a[k] == a[i] - 1 && h[k][j] != -INF) {
          if (i + 1 <= k - 1)
            h[i][j] = max(h[i][j], f[i + 1][k - 1] + h[k][j]);
          else
            h[i][j] = max(h[i][j], h[k][j]);
        }
        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] + v[2 * a[k] - a[i] - a[j] + 1]);
    }
  }
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < i; j++) dp[i] = max(dp[i], dp[j] + f[j + 1][i]);
  }
  printf("%d\n", dp[n]);
  return 0;
}
