#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <map>
#include <math.h>
#include <cmath>
#include <string.h>
#include <numeric>
using namespace std;
typedef vector<int> vi ;
typedef long long ll;
#define all(x) (x).begin() , (x).end()
#define pb push_back
const int N = 1e6+5, M=1e6+1 , MX = 1e13+1;
const ll MXL=1e18 , MOD = 1e9 + 7 ;
ll a[N], b[N], cost[N], dp[N], indx[N], neg;
int main(){
// freopen("input.txt", "r", stdin);
int n, m;
cin >> n >> m;
for(int i=1 ; i<=n ; i++ ){
scanf("%lld" , a+i );
}
for(int i=1 ; i<=n ; i++ ){
scanf("%lld" , cost+i );
if( cost[i] <= 0 )
neg+=cost[i];
else
cost[i]=0;
}
for(int i=1 ; i<=m ; i++ ){
scanf("%lld" , b+i );
indx[b[i]]=i;
// cout << indx[b[i]] <<" "<<b[i]<<"\n";
dp[i]=-1*MX;
}
for(int i=1 ; i<=n ; i++ ){
if( indx[a[i]] ){
int in = indx[a[i]];
// cout<<"*"<< in <<" ";
dp[in] = max( dp[in] , cost[i]+ dp[in-1]);
// cout << dp[in] <<"\n";
}
}
// for(int i=1 ; i<=m ; i++ )
cout<< neg - dp[m]<<" ";
return 0 ;
}
Copy