
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define int long long 

const int MAXN = 1e6;
const int mod =  998244353;


int bin_pow_mod(int a,int n){
    if(n == 0)return 1;
    if(n % 2 == 0){
        int tmp = bin_pow_mod(a,n / 2);
        return (tmp * tmp) % mod;
    }else{
        return (a * bin_pow_mod(a,n - 1)) % mod;
    }
}
int sz[MAXN];
int p[MAXN];
int h[MAXN];

void dfs(int v,std::vector<std::vector<int>>& g,int pred = -1){
    sz[v] = 1;
    p[v] = pred;
    if(pred == -1){
        h[v] = 0;
    }else{
        h[v] = h[pred] + 1;
    }
    for(int u:g[v]){
        if(u != pred){
            dfs(u,g,v);
            sz[v] += sz[u];
        }
    }
}


int find(int a,int b){
    if(a == -1) return b;
    if(b == -1)return a;

    if(h[a] < h[b]){
        std::swap(a,b);
    }

    while(h[a] > h[b]){
        a = p[a];
    }
    while(a != b){
        a = p[a];
        b = p[b];
    }
    return a;
}



signed main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);


    int n;
    std::cin>>n;

    std::vector<std::pair<int,int>> all(n);
    for(int i =0 ;i < n;i++){
        std::cin>>all[i].first>>all[i].second;
    }

    std::vector<int> p(n + 1);
    p[0] = -1;
    for(int i = 1;i <= n;i++){
        std::cin>>p[i];
    }



    // int now = 0;
    // int l = 0;
    // int max = -1e18;
    // for(int i = 0;i < n;i++){
    //     while(l < p[i + 1] && )
    // }

    std::vector<std::vector<int>> dp(n + 1,std::vector<int>(n + 1,-1e18));
    std::vector<std::vector<int>> pref(n + 1,std::vector<int>(n + 1,-1e18));
    int ans = -1e18;

    dp[0][0] = 0;
    for(int i = 1;i <= n;i++){
        for(int j = 0;j < i;j++){
            dp[i][j] = dp[i - 1][j];
           for(int prev = j - 1;prev >= 0;prev--){
            if(p[prev + 1] != -1 && p[prev + 1] != i - 1)break;
            //std::cout<<p[prev]<<' '<<i<<' '<<j<<std::endl;
            dp[i][j] = std::max(dp[i][j],dp[i - 1][prev]);
           }
           dp[i][j] += (all[i - 1].second * j + all[i - 1].first);
           bool is = true;
           for(int k = j;k < n;k++){
            if(p[k + 1] != -1 && p[k + 1] != j){
                is = false;
            }
           }
           if(is){
            ans = std::max(ans,dp[i][j]);
           }
        }
    }

    
    std::cout<<ans;
}