#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6+5;
const ll OO = 1e15;
ll a[N];
ll cost[N];
ll b[N];
ll dp[N];
int first_idx[N], ridx[N];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
ll sum_neg = 0;
for(int i = 0 ; i < n ; i++) scanf("%lld",&a[i]);
for(int i = 0 ; i < n ; i++) scanf("%lld",&cost[i]), sum_neg += (cost[i] < 0 ? cost[i] : 0);
for(int i = 0 ; i < m ; i++) scanf("%lld",&b[i]) , ridx[b[i]] = i;
memset(first_idx, -1, sizeof(first_idx));
for(int i = 0 ; i <= n ; i++) dp[i] = -OO;
b[m] = N-1;
dp[b[m]] = 0;
int j = m-1;
for(int i = n-1 ; i >=0 ; i--)
if(a[i] == b[j])
{
first_idx[b[j]] = i;
j--;
}
for(int i = n-1 ; i >= 0 ; i--) if(i <= first_idx[a[i]])
dp[a[i]] = max(dp[a[i]], (cost[i] < 0 ? cost[i] : 0) + dp[b[ridx[a[i]]+1]]);
printf("%lld\n", sum_neg - dp[b[0]]);
}
Copy