SmallR likes a game called "Deleting Substrings". In the game you are given a sequence of integers w, you can modify the sequence and get points. The only type of modification you can perform is (unexpected, right?) deleting substrings. More formally, you can choose several contiguous elements of w and delete them from the sequence. Let's denote the sequence of chosen elements as wl, wl + 1, ..., wr. They must meet the conditions:

  * the equality |wi - wi + 1| = 1 must hold for all i (l ≤ i < r); 
  * the inequality 2·wi - wi + 1 - wi - 1 ≥ 0 must hold for all i (l < i < r). 



After deleting the chosen substring of w, you gain vr - l + 1 points. You can perform the described operation again and again while proper substrings exist. Also you can end the game at any time. Your task is to calculate the maximum total score you can get in the game.

Input

The first line contains a single integer n (1 ≤ n ≤ 400) — the initial length of w. The second line contains n integers v1, v2, ..., vn (0 ≤ |vi| ≤ 2000) — the costs of operations. The next line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 109) — the initial w.

Output

Print a single integer — the maximum total score you can get.

Examples

Input

3
0 0 3
1 2 1


Output

3

Input

6
1 4 5 6 7 1000
2 1 1 2 2 3


Output

12
#include <bits/stdc++.h>
const int N = 405, inf = 1000000007;
using namespace std;
int g[N], f[N][N][2], n, a[N], v[N];
void dp() {
  for (int i = 1; i <= n; ++i) {
    f[i][i][0] = 0;
    f[i][i][1] = v[1];
  }
  for (int L = 2; L <= n; ++L)
    for (int l = 1; l <= n - L + 1; ++l) {
      int r = l + L - 1;
      f[l][r][0] = -inf;
      if (a[l] != a[r]) {
        int st = (a[l] < a[r] ? 1 : -1);
        for (int k = l + 1; k <= r; ++k)
          if (a[k] == a[l] + st)
            f[l][r][0] = max(f[l][r][0], f[l + 1][k - 1][1] + f[k][r][0]);
      }
      f[l][r][1] = -inf;
      for (int k = l; k <= r - 1; ++k)
        f[l][r][1] = max(f[l][r][1], f[l][k][1] + f[k + 1][r][1]);
      for (int k = l; k <= r; ++k)
        if (a[l] <= a[k] && a[r] <= a[k] && a[k] - a[l] + a[k] - a[r] + 1 <= n)
          f[l][r][1] = max(f[l][r][1], f[l][k][0] + f[k][r][0] +
                                           v[a[k] - a[l] + a[k] - a[r] + 1]);
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &v[i]);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
  }
  dp();
  g[0] = 0;
  for (int i = 1; i <= n; ++i) {
    g[i] = g[i - 1];
    for (int j = 0; j <= i - 1; ++j) g[i] = max(g[i], g[j] + f[j + 1][i][1]);
  }
  printf("%d\n", g[n]);
  return 0;
}
