#include <bits/stdc++.h>
using namespace std;
const int N = 410, INF = 1e9;
int v[N], w[N];
int ans[N], dp[N][N], f[N][N], g[N][N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> v[i];
  for (int i = 0; i < n; i++) cin >> w[i];
  for (int i = 0; i < n; i++) dp[i][i + 1] = v[1];
  for (int len = 2; len <= n; len++)
    for (int l = 0; l + len <= n; l++) {
      int r = l + len;
      f[l][r] = g[l][r] = dp[l][r] = -INF;
      if (w[l] < w[r - 1]) {
        for (int k = l + 1; k < r; k++)
          if (w[k] == w[l] + 1) f[l][r] = max(f[l][r], dp[l + 1][k] + f[k][r]);
      }
      if (w[l] > w[r - 1]) {
        for (int k = l + 1; k < r; k++)
          if (w[k] == w[l] - 1) g[l][r] = max(g[l][r], dp[l + 1][k] + g[k][r]);
      }
      for (int k = l + 1; k < r - 1; k++)
        if (w[k] > w[l] && w[k] > w[r - 1]) {
          int tool = w[k] - w[l] + w[k] - w[r - 1] + 1;
          if (tool <= n)
            dp[l][r] = max(dp[l][r], v[tool] + f[l][k + 1] + g[k][r]);
        }
      if (w[r - 1] > w[l] && w[r - 1] - w[l] < n)
        dp[l][r] = max(dp[l][r], f[l][r] + v[w[r - 1] - w[l] + 1]);
      if (w[r - 1] < w[l] && w[l] - w[r - 1] < n)
        dp[l][r] = max(dp[l][r], g[l][r] + v[w[l] - w[r - 1] + 1]);
      for (int k = l + 1; k < r; k++)
        dp[l][r] = max(dp[l][r], dp[l][k] + dp[k][r]);
    }
  ans[0] = 0;
  for (int i = 1; i <= n; i++) {
    ans[i] = ans[i - 1];
    for (int len = 1; len <= i; len++)
      ans[i] = max(ans[i], ans[i - len] + dp[i - len][i]);
  }
  cout << ans[n] << endl;
}
