#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[200], c[200], last[200];
struct edge {
  int to, nxt, f, c;
} e[(200 * 5)];
int edgenum = 0;
int lin[400];
void add(int a, int b, int f, int c) {
  e[edgenum] = (edge){b, lin[a], f, c};
  lin[a] = edgenum++;
  e[edgenum] = (edge){a, lin[b], 0, -c};
  lin[b] = edgenum++;
  return;
}
int pre[400], d[400], rate[400];
bool inq[400];
int s, t;
bool SPFA() {
  queue<int> q;
  q.push(s);
  memset(d, 0x3f, sizeof(d));
  d[s] = 0;
  rate[s] = 0x3f3f3f3f;
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    inq[k] = false;
    for (int i = lin[k]; i != -1; i = e[i].nxt) {
      if (d[e[i].to] > d[k] + e[i].c && e[i].f) {
        d[e[i].to] = d[k] + e[i].c;
        pre[e[i].to] = i;
        rate[e[i].to] = min(rate[k], e[i].f);
        if (!inq[e[i].to]) {
          inq[e[i].to] = true;
          q.push(e[i].to);
        }
      }
    }
  }
  return d[t] != 0x3f3f3f3f;
}
int flow() {
  for (int i = t; i != s; i = e[pre[i] ^ 1].to) {
    e[pre[i]].f -= rate[t];
    e[pre[i] ^ 1].f += rate[t];
  }
  return rate[t] * d[t];
}
int EK() {
  int ans = 0;
  while (SPFA()) ans += flow();
  return ans;
}
int main() {
  memset(lin, -1, sizeof(lin));
  scanf("%d%d", &n, &m);
  s = 2 * n + 1;
  t = s + 1;
  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) add(s, 2 * i - 1, 1, c[a[i]]);
  for (int i = 1; i <= n; ++i) {
    if (last[a[i]] != 0) add((i - 1) * 2 - 1, last[a[i]] * 2, 1, -c[a[i]]);
    last[a[i]] = i;
    add(i * 2 - 1, i * 2, 1, 0);
    add(i * 2, t, 1, 0);
    if (i != n) add(i * 2 - 1, i * 2 + 1, m - 1, 0);
  }
  cout << EK() << endl;
  return 0;
}
