#include <bits/stdc++.h>
const int N = 2e3 + 5, INF = 1e9;
using namespace std;
int n, k, s, t, ans, num = 1;
int a[N], c[N], inq[N], dis[N], incf[N], used[N];
int nt[N], to[N], cap[N], val[N], pre[N], head[N];
bool BFS() {
  for (int i = s; i <= t; ++i) inq[i] = 0, dis[i] = INF;
  queue<int> q;
  q.push(s);
  inq[s] = 1;
  incf[s] = INF;
  dis[s] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = head[x], y = to[i]; i; i = nt[i], y = to[i]) {
      if (!cap[i]) continue;
      if (dis[y] > dis[x] + val[i]) {
        dis[y] = dis[x] + val[i];
        incf[y] = min(incf[x], cap[i]);
        pre[y] = i;
        if (!inq[y]) q.push(y), inq[y] = 1;
      }
    }
    inq[x] = 0;
  }
  return dis[t] != INF;
}
void update() {
  int x = t, y = pre[t];
  ans += dis[t] * incf[t];
  while (x != s)
    cap[y] -= incf[t], cap[y ^ 1] += incf[t], x = to[y ^ 1], y = pre[x];
}
inline void Add(int x, int y, int z, int l) {
  ++num;
  nt[num] = head[x];
  head[x] = num;
  to[num] = y;
  cap[num] = z;
  val[num] = l;
  ++num;
  nt[num] = head[y];
  head[y] = num;
  to[num] = x;
  cap[num] = 0;
  val[num] = -l;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i != n; ++i) Add(i, i + 1, k - 1, 0);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &c[i]);
  for (int i = 1; i <= n; ++i) {
    ans += c[a[i]];
    if (used[a[i]])
      if (used[a[i]] != i - 1)
        Add(used[a[i]], i - 1, 1, -c[a[i]]);
      else
        ans -= c[a[i]];
    used[a[i]] = i;
  }
  s = 0;
  t = n + 1;
  Add(s, 1, k - 1, 0);
  Add(n, t, k - 1, 0);
  while (BFS()) update();
  printf("%d\n", ans);
  return 0;
}
