#include <bits/stdc++.h>
using namespace std;
int n;
long long v[405], w[405], dp[405][405], peak[405][405], l[405][405],
    r[405][405], ans[405];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &v[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= i; j++) v[i] = max(v[i], v[j] + v[i - j]);
  for (int i = 1; i <= n; i++) scanf("%lld", &w[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      dp[i][j] = peak[i][j] = l[i][j] = r[i][j] = -1e12;
  for (int i = 1; i <= n; i++)
    peak[i][i] = dp[i][i] = v[1],
    l[i][i] = r[i][i] = l[i][i - 1] = r[i][i - 1] = dp[i][i - 1] = 0;
  for (int i = 2; i <= n; i++)
    for (int j = 1; j <= n - i + 1; j++) {
      int ranl = j, ranr = i + j - 1;
      for (int k = ranl; k < ranr; k++)
        if (w[k] == w[ranr] - 1)
          l[ranl][ranr] = max(l[ranl][ranr], l[ranl][k] + dp[k + 1][ranr - 1]);
      for (int k = ranl + 1; k <= ranr; k++)
        if (w[k] == w[ranl] - 1)
          r[ranl][ranr] = max(r[ranl][ranr], r[k][ranr] + dp[ranl + 1][k - 1]);
      for (int k = ranl; k <= ranr; k++)
        if (2 * w[k] - w[ranl] - w[ranr] + 1 >= 1 &&
            2 * w[k] - w[ranl] - w[ranr] + 1 <= ranr - ranl + 1)
          peak[ranl][ranr] =
              max(peak[ranl][ranr], l[ranl][k] + r[k][ranr] +
                                        v[2 * w[k] - w[ranl] - w[ranr] + 1]);
      for (int k = ranl; k <= ranr; k++)
        dp[ranl][ranr] = max(dp[ranl][ranr], peak[ranl][k] + dp[k + 1][ranr]);
    }
  ans[0] = 0;
  long long ret = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < i; j++)
      ans[i] = max(ans[i], ans[j] + max(dp[j + 1][i], 0LL));
    ret = max(ret, ans[i]);
  }
  printf("%lld\n", ret);
}
