#include<bits/stdc++.h>
#define GO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef pair<int, int> pi;
const ll Mod = 998244353;
const ll INF = (ll)(1e18) + 5;
const ll N = 1e6 + 5, M = 18;
int n, m;
int a[N], cost[N], b[N];
map<int, int> idx;
int main() {
GO;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> cost[i], cost[i] *= -1;
for (int i = 1; i <= m; i++)
cin >> b[i], idx[b[i]] = i;
ll sum = 0;
vec dp(m + 1, INF);
dp[0] = 0;
ll ALL = 0;
for (int i = 1; i <= n; i++) {
ALL += cost[i];
int x = a[i];
if (idx.count(x) == 0 || dp[idx[x] - 1] == INF) {
if (cost[i] < 0)
sum += cost[i];
continue;
}
ll cur = dp[idx[x] - 1];
cur += cost[i];
if (dp[idx[x]] < INF && cost[i] < 0)
dp[idx[x]] += cost[i];
dp[idx[x]] = min(dp[idx[x]], cur);
}
cout << sum + dp[m] - ALL << '\n';
return 0;
}
Copy