#include <bits/stdc++.h>
using namespace std;
const int N = 165, M = N * 6, inf = 0x3f3f3f3f;
int n, K, tot, S, T, ans;
int head[N], dis[N], inq[N], a[N], w[N], pre[N], ins[N];
queue<int> q;
struct Tway {
  int v, nex, w, c;
} e[M];
void add(int u, int v, int w, int c) {
  e[++tot] = (Tway){v, head[u], w, c};
  head[u] = tot;
  e[++tot] = (Tway){u, head[v], 0, -c};
  head[v] = tot;
}
int read() {
  int ret = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar();
  return ret;
}
bool spfa() {
  memset(dis, 0x3f, (T + 2) << 2);
  q.push(S);
  dis[S] = 0;
  inq[S] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    inq[x] = 0;
    for (int i = head[x]; i; i = e[i].nex) {
      int v = e[i].v;
      if (e[i].w && dis[v] > dis[x] + e[i].c) {
        dis[v] = dis[x] + e[i].c;
        if (!inq[v]) q.push(v), inq[v] = 1;
      }
    }
  }
  return dis[T] < (inf / 2);
}
int dfs(int x, int flow) {
  if (x == T || !flow) return flow;
  int u, used = 0;
  ins[x] = 1;
  for (int i = head[x]; i; i = e[i].nex) {
    int v = e[i].v;
    if (ins[v] || !e[i].w || (dis[v] != dis[x] + e[i].c)) continue;
    u = dfs(v, min(e[i].w, flow - used));
    e[i].w -= u;
    e[i ^ 1].w += u;
    used += u;
    ans += e[i].c * u;
  }
  ins[x] = 0;
  return used;
}
int main() {
  n = read();
  K = read();
  S = 2 * n + 1;
  T = S + 1;
  tot = 1;
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 1; i <= n; ++i) w[i] = read();
  for (int i = 1; i <= n; ++i) {
    add(S, i, 1, w[a[i]]);
    add(i + n, T, 1, 0);
    add(i, i + n, 1, 0);
    if (i < n) add(i, i + 1, K - 1, 0);
    if (pre[a[i]]) add(i - 1, pre[a[i]] + n, 1, -w[a[i]]);
    pre[a[i]] = i;
  }
  while (spfa()) dfs(S, inf);
  printf("%d\n", ans);
  return 0;
}
