#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
template <class S>
inline void read(S& arg) {
  cin >> arg;
}
template <class S>
inline void readA(S Lptr, S Rptr) {
  while (Lptr != Rptr) {
    read(*Lptr);
    Lptr++;
  }
}
template <class S, class... T>
inline void read(S& arg, T&... rest) {
  read(arg);
  read(rest...);
}
char delimiter = ' ';
template <class S>
inline void write(S arg) {
  cout << arg << '\n';
}
template <class S, class... T>
inline void write(S arg, T... rest) {
  cout << arg << delimiter;
  write(rest...);
}
template <class S>
inline void writeA(S Lptr, S Rptr) {
  while (Lptr != Rptr) {
    cout << *Lptr;
    Lptr++;
    if (Lptr != Rptr) cout << delimiter;
  }
  cout << '\n';
}
template <class T, class S>
inline bool smin(T& a, S b) {
  return (T)b < a ? a = b, 1 : 0;
}
template <class T, class S>
inline bool smax(T& a, S b) {
  return a < (T)b ? a = b, 1 : 0;
}
constexpr long long MOD = 1e9 + 7;
constexpr long long N = 400 + 10;
template <typename T>
inline T mod(T& v) {
  return v = (v % MOD + MOD) % MOD;
}
template <typename S, typename T>
inline S add(S& l, T r) {
  return mod(l += r);
}
long long po(long long v, long long u) {
  return u ? po(v * v % MOD, u >> 1) * (u & 1 ? v : 1) % MOD : 1;
}
long long n, a[N], c[N], dp[N][N], pd[N][N], dp1[N][N];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  read(n);
  readA(c, c + n);
  readA(a, a + n);
  for (long long i = 0; i < (long long)n; i++) {
    dp[i][i] = c[0];
    smax(pd[i][i], c[0]);
  }
  for (long long d = 2; d <= n; d++)
    for (long long l = 0, r = d - 1; r < n; l++, r++) {
      dp1[l][r] = -1e18;
      if (a[l] != a[r]) {
        long long next = a[r] > a[l] ? a[l] + 1 : a[l] - 1;
        for (long long i = l; i < (long long)r + 1; i++)
          if (a[i] == next) smax(dp1[l][r], dp[l + 1][i - 1] + dp1[i][r]);
      }
      dp[l][r] = -1e18;
      for (long long i = l; i < (long long)r + 1; i++) {
        if (a[i] >= a[l] && a[i] >= a[r] && 2 * a[i] - a[l] - a[r] < n)
          smax(dp[l][r], dp1[l][i] + dp1[i][r] + c[2 * a[i] - a[l] - a[r]]);
        smax(dp[l][r], dp[l][i] + dp[i + 1][r]);
        smax(pd[l][r], pd[l][i] + pd[i + 1][r]);
      }
      smax(pd[l][r], dp[l][r]);
    }
  write(pd[0][n - 1]);
  return 0;
}
