#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define mod 1000000007
#define INF 1000000000
#define LLINF 2000000000000000000LL

#define SIZE 100
#define FACT7 5040 // 7!

//number of permutation
#define MAX_DATASIZE 7

int encode(int *data,int size){
    bool used[MAX_DATASIZE+1]={0};
    int num = 0;
    int t = 1;
    
    for(int i=1;i<=size;i++) t*=i;
    
    for(int i=size;i>=1;i--){
        t/=i;
        
        int s = 0;
        for(int j=1;j<data[size-i];j++){
            if(!used[j]) s++;
        }
        
        used[data[size-i]] = true;
        
        num += s*t;
    }
    
    for(int i=1;i<=7;i++) assert(used[i]);
    
    return num;
}

void decode(int num,int *data,int size){
    bool used[MAX_DATASIZE+1]={0};
    int t=1;
    
    for(int i=1;i<=size;i++) t*=i;
    
    for(int i=size;i>=1;i--){
        t/=i;
        
        int s = num/t;
        
        for(int j=1;;j++){
            if(used[j]==false){
                if(s==0){
                    
                    data[size-i] = j;
                    used[j] = true;
                    break;
                }
                s--;
            }
        }
        num %= t;
    }
}

int n;
int d[SIZE];
int a[SIZE];
int dp[SIZE+2][FACT7];


int main(){
    int P[7],P2[7];
    
    scanf("%d",&n);
    
    for(int i=0;i<n;i++) scanf("%d",&d[i]);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    
    for(int i=0;i<=n;i++){
        for(int j=0;j<FACT7;j++){
            dp[i][j] = -INF;
        }
    }
    
    dp[0][0] = 0;
    
    for(int i=0;i<n;i++){
        
        for(int j=0;j<FACT7;j++){
            if(dp[i][j]==-INF) continue;
            
            decode(j,P2,7);
            
            for(int k=0;k<=min(7,i);k++){
                
                for(int l=0;l<7;l++) P[l] = P2[l];
                
                int addscore = 0;
                
                int s = max(0,i-7);
                
                for(int l=max(0,7-i);l<7;l++){
                    
                    //cerr << s << ":" << i-s << " " << P[l] << endl;
                    
                    
                    if(P[l]>k){ //???????£???????
                        if(i-s <= d[i]) addscore += a[i];
                        
                    }else{ //???????£???????
                        if(i-s <= d[s]) addscore += a[s];
                    }
                    
                    s++;
                }
                
                //cerr << "addscore = " << addscore << endl;
                
                for(int l=0;l<7;l++){
                    if(P[l] > k) P[l]++;
                }
                
                int t = P[0];
                
                for(int l=0;l<6;l++){
                    P[l] = P[l+1];
                }
                P[6] = k+1;
                
                for(int l=0;l<7;l++){
                    if(P[l]>t) P[l]--;
                }
                
                int p = encode(P,7);
                
                dp[i+1][p] = max(dp[i+1][p],dp[i][j]+addscore);
                //cerr << "dp[" << i+1 << "][" << p << "] = " << dp[i+1][p] << endl;
            }
        }
    }
    
    int ans = 0;
    
    for(int i=0;i<FACT7;i++){
        ans = max(ans,dp[n][i]);
    }
    
    printf("%d\n",ans);
    
    return 0;
}

Measured memory_bytes: 3164