#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> v(n + 1), a(n);
  for (int i = 1; i <= n; ++i) cin >> v[i];
  for (int i = 0; i < n; ++i) cin >> a[i];
  vector<vector<int> > dpMon(n, vector<int>(n, -INF));
  vector<vector<int> > dpWsz(n, vector<int>(n, -INF));
  vector<vector<int> > dp(n, vector<int>(n, -INF));
  for (int d = 1; d <= n; ++d) {
    for (int l = 0; l + d <= n; ++l) {
      int p = l + d - 1;
      if (d == 1) {
        dpMon[l][p] = 0;
        dpWsz[l][p] = v[1];
        dp[l][p] = max(0, v[1]);
        continue;
      }
      if (d == 2) {
        if (abs(a[l] - a[p]) == 1) dpMon[l][p] = 0;
        dpWsz[l][p] = 2 * v[1];
        if (abs(a[l] - a[p]) == 1) dpWsz[l][p] = max(dpWsz[l][p], v[2]);
        dp[l][p] = max(dpWsz[l][p], max(v[1], 0));
        continue;
      }
      if (abs(a[l] - a[p]) == 1) dpMon[l][p] = dpWsz[l + 1][p - 1];
      for (int i = l + 1; i < p; ++i)
        if (((a[i] > a[l] && a[i] < a[p]) || (a[i] < a[l] && a[i] > a[p])) &&
            dpMon[l][i] != -INF && dpMon[i][p] != -INF)
          dpMon[l][p] = max(dpMon[l][p], dpMon[l][i] + dpMon[i][p]);
      for (int i = l; i < p; ++i)
        dpWsz[l][p] = max(dpWsz[l][p], dpWsz[l][i] + dpWsz[i + 1][p]);
      for (int i = l + 1; i < p; ++i) {
        if (dpMon[l][i] == -INF || dpMon[i][p] == -INF) continue;
        if (a[l] > a[i] && a[i] < a[p]) continue;
        int dl = abs(a[l] - a[i]) + abs(a[i] - a[p]) + 1;
        dpWsz[l][p] = max(dpWsz[l][p], dpMon[l][i] + dpMon[i][p] + v[dl]);
      }
      if (abs(a[l] - a[p]) == 1)
        dpWsz[l][p] = max(dpWsz[l][p], dpWsz[l + 1][p - 1] + v[2]);
      dp[l][p] = dpWsz[l][p];
      for (int i = l; i < p; ++i) {
        dp[l][p] = max(dp[l][p], dp[l][i] + dpWsz[i + 1][p]);
        dp[l][p] = max(dp[l][p], dpWsz[l][i] + dp[i + 1][p]);
      }
      dp[l][p] = max(dp[l][p], dp[l + 1][p - 1]);
    }
  }
  cout << dp[0][n - 1];
  return 0;
}
