#[allow(unused_imports)]
use std::io::{stdin, Read, StdinLock};
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::str::FromStr;
#[allow(unused_imports)]
use std::collections::{HashSet, HashMap, BinaryHeap, VecDeque};
#[allow(unused_imports)]
use std::vec::Vec;

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner {cin: cin}
    }
    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
                    .skip_while(|c| c.is_whitespace())
                    .take_while(|c| !c.is_whitespace())
                    .collect::<String>();
        token.parse::<T>().ok()
    }
    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }
}

#[allow(dead_code)]
fn next_permutation<T: Ord>(v: &mut Vec<T>) -> bool {
    if v.len() == 0 || v.len() == 1 {
        return false
    }
    let mut i: usize = v.len() - 1;
    let n = v.len();
    loop {
        let ii = i;
        i -= 1;
        if v[i].cmp(&v[ii]) == Ordering::Less {
            let mut j = n - 1;
            while v[i].cmp(&v[j]) != Ordering::Less {
                j -= 1;
            }
            v.swap(i, j);
            for k in 0..(n - ii) / 2 { // reverse
                v.swap(ii + k, n - k - 1);
            }
            return true
        }
        if i == 0 { // i == first
            v.reverse();
            return false
        }
    }
}

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin);

    let n: usize = sc.read();
    let mut d: Vec<usize> = vec![0; n];
    let mut a: Vec<usize> = vec![0; n];
    for i in 0..n {
        d[i] = sc.read();
    }
    for i in 0..n {
        a[i] = sc.read();
    }
    let d = d;
    let a = a;

    let mut dp: Vec<HashMap<Vec<usize>, usize>> = vec![HashMap::new(); n];
    let mut perm: Vec<usize> = (0..7).collect();
    while {
        dp[0].insert(perm.clone(), 0);
        next_permutation(&mut perm)
    } {}

    for i in 0..n-1 {
        let mut nxt: HashMap<Vec<usize>, usize> = HashMap::new();
        for (p, r) in &mut dp[i] {
            let mut np: Vec<usize> = vec![0; 7];
            for j in 0..6 {
                np[j] = p[j + 1];
                if p[0] > p[j + 1] {
                    np[j] += 1;
                }
            }
            for _j in 0..7 {
                let mut rr = *r;
                for kk in 0..6 {
                    if i >= kk {
                        if np[6 - kk - 1] < np[6] && d[i - kk] >= kk + 1 {
                            rr += a[i - kk];
                        }
                        if np[6 - kk - 1] > np[6] && d[i + 1] >= kk + 1 {
                            rr += a[i + 1];
                        }
                    }
                }
                if i >= 6 && p[0] == np[6] {
                    let t1 = if d[i - 6] == 7 { a[i - 6] } else { 0 };
                    let t2 = if d[i + 1] == 7 { a[i + 1] } else { 0 };
                    rr += max(t1, t2);
                } else if i >= 6 && p[0] > np[6] {
                    rr += if d[i + 1] == 7 { a[i + 1] } else { 0 };
                } else if i >= 6 && p[0] < np[6] {
                    rr += if d[i - 6] == 7 { a[i - 6] } else { 0 };
                }
                if nxt.contains_key(&np) {
                    let r2: usize = *nxt.get(&np).unwrap();
                    nxt.insert(np.clone(), max(rr, r2));
                } else {
                    nxt.insert(np.clone(), rr);
                }
                np[6] += 1;
                for k in 0..6 {
                    if np[k] == np[6] {
                        np[k] -= 1;
                    }
                }
            }
        }
        dp[i + 1] = nxt;
    }

    println!("{}", dp[n - 1].iter().fold(0, |ans, (_, a)| max(ans, *a)));
}


Measured memory_bytes: 70464