#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
 
#define MAX_N 120
#define MAX_D 8
#define MAX_C 40320
 
int dp[MAX_N][MAX_C];
vector<int> fact1[MAX_C];
int fact2[8][8][8][8][8][8][8];
int D[MAX_N], X[MAX_N], n, cnt, maxn;
 
void fact() {
    cnt = 0;
    vector<int> p = { 0,1,2,3,4,5,6,7 };
    vector<int> a;
 
    //fact1
    do {
        for (int i = 0; i < MAX_D; i++) {
            fact1[cnt].push_back(p[i]);
        }
        cnt++;
    } while (next_permutation(p.begin(), p.end()));
 
    //fact2
    for (int i = 0; i < MAX_C; i++) {
        a = fact1[i];
        fact2[a[0]][a[1]][a[2]][a[3]][a[4]][a[5]][a[6]] = i;
    }
}
 
int Non_dp() {
    int G[8], H, maxx = 0;
    for (int i = 0; i < n; i++){
        G[i] = i;
    }
    do {
        H = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (abs(i - j) <= D[i] && G[i] > G[j]) {
                    H += X[i];
                }
            }
        }
        maxx = max(maxx, H);
    } while (next_permutation(G, G + n));
    return maxx;
}
 
void First_dp() {
    cnt = 0;
    vector<int>q = { 0,1,2,3,4,5,6,7 };
    int A[MAX_D], C[MAX_D];
    pair<int, int>B[MAX_D];
    //shori.
    do {
        memset(A, 0, sizeof(A));
        memset(C, 0, sizeof(C));
 
        for (int i = 0; i < MAX_D; i++){
            B[i].first = q[i];
            B[i].second = i;
        }
        sort(B, B + MAX_D);
 
        for (int i = 0; i < MAX_D; i++) {
            A[B[i].second] = 1;
            for (int j = 0; j < MAX_D; j++) {
                if (A[j] == 0 && abs(B[i].second - j) <= D[B[i].second]) { C[j] += X[B[i].second]; }
            }
        }
        for (int i = 0; i < MAX_D; i++) {
            dp[MAX_D - 1][cnt] += C[i];
        }
        cnt++;
    }while (next_permutation(q.begin(), q.end()));
}
 
void Second_dp() {
    vector<int>r;
    pair<int, int>S[7];
    int s[8];
    int E;
    int F;
    for (int i = 8; i < n; i++) {
        for (int j = 0; j < MAX_C; j++) {
 
            r = fact1[j];
            memset(s, 0, sizeof(s));
 
            //seiri.
            for (int k = 1; k < MAX_D; k++) {
                S[k - 1].first = r[k];
                S[k - 1].second = k - 1;
            }
            sort(S, S + MAX_D - 1);
            for (int k = 0; k < MAX_D - 1; k++) {
                s[S[k].second] = k + 1;
            }
 
            //dp.
            for (int k = 0; k < MAX_D; k++) {
                //i-komeno xiao wo itsu ireruka.
                for (int l = 0; l < MAX_D - 1; l++) {
                    if (s[l] == k) {
                        s[l]--;
                    }
                }
                s[MAX_D - 1] = k;
                F = 0;
                for (int l = 0; l < MAX_D - 1; l++) {
                    if (abs(l - 7) <= D[i] && s[l] >= k) {
                        F += X[i];
                    }
                }
                for (int l = 0; l < MAX_D - 1; l++) {
                    if (abs(l - 7) <= D[i + l - 7] && s[l] <= k) {
                        F += X[i + l - 7];
                    }
                }
                E = fact2[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]][s[6]];
                dp[i][E] = max(dp[i][E], dp[i - 1][j] + F);
            }
 
        }
    }
}
 
int main() {
    fact();
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> D[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> X[i];
    }
    if (n < MAX_D) {
        cout << Non_dp() << endl;
    }
    else {
        First_dp();
        Second_dp();
        for (int i = 0; i < MAX_C; i++) {
            maxn = max(maxn, dp[n - 1][i]);
        }
        cout << maxn << endl;
    }
    return 0;
}

Measured memory_bytes: 25764