#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int MAXM = 2e5 + 5;
const long long INF = 1e9;
struct Edge {
  int to, next, cap, flow, cost;
} edge[MAXM * 4];
int head[MAXN], tol;
int pre[MAXN];
long long dis[MAXN];
bool vis[MAXN];
int N;
void init(int n) {
  N = n;
  tol = 0;
  memset(head, -1, sizeof head);
  memset(pre, 0, sizeof pre);
  memset(dis, 0, sizeof dis);
  memset(vis, 0, sizeof vis);
  memset(edge, 0, sizeof edge);
}
void addedge(int u, int v, int cap, int cost) {
  edge[tol] = Edge{v, head[u], cap, 0, cost};
  head[u] = tol++;
  edge[tol] = Edge{u, head[v], 0, 0, -cost};
  head[v] = tol++;
}
bool spfa(int s, int t) {
  queue<int> q;
  for (int i = 0; i < N; i++) {
    dis[i] = INF * INF;
    vis[i] = false;
    pre[i] = -1;
  }
  dis[s] = 0;
  vis[s] = true;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) {
        dis[v] = dis[u] + edge[i].cost;
        pre[v] = i;
        if (!vis[v]) {
          vis[v] = true;
          q.push(v);
        }
      }
    }
  }
  return dis[t] < 0;
}
int a[MAXN], b[MAXN];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  init(MAXN);
  int s = 2 * n, t = 2 * n + 1;
  for (int i = 0; i < n; i++) {
    addedge(s, 2 * i, 1, b[a[i]]);
    addedge(2 * i + 1, t, 1, 0);
    addedge(2 * i, 2 * i + 1, 1, -INF);
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      addedge(2 * i + 1, 2 * j, 1, a[i] == a[j] ? 0 : b[a[j]]);
    }
  }
  long long cost = 0;
  for (int i = 0; i < k; i++) {
    if (!spfa(s, t)) break;
    for (int j = pre[t]; j != -1; j = pre[edge[j ^ 1].to]) {
      edge[j].flow++;
      edge[j ^ 1].flow--;
    }
    cost += dis[t];
  }
  cout << cost + 1LL * n * INF << endl;
  return 0;
}
