#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
using namespace std;
const int N = 100010;
const long long INF = 1e18;
namespace network {
int ans, n, m, k, head[N], w[N], las[N], c[N], vis[N], flow[N], pre[N], s, t,
    tt, tot;
long long dis[N], mc, mf;
struct pr {
  int next, to, w, c;
} a[N];
void add(int u, int v, int w, int c) {
  a[++tt] = (pr){head[u], v, w, c};
  head[u] = tt;
  a[++tt] = (pr){head[v], u, 0, -c};
  head[v] = tt;
}
int spfa() {
  queue<int> q;
  while (!q.empty()) q.pop();
  for (int i = 1; i <= tot; i++) dis[i] = INF;
  vis[s] = 1;
  dis[s] = 0;
  pre[t] = -1;
  flow[s] = 1e9;
  q.push(s);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    vis[x] = 0;
    for (int i = head[x]; i; i = a[i].next) {
      int y = a[i].to;
      if (a[i].w && dis[x] + a[i].c < dis[y]) {
        dis[y] = dis[x] + a[i].c;
        flow[y] = min(flow[x], a[i].w);
        pre[y] = i;
        if (!vis[y]) q.push(y), vis[y] = 1;
      }
    }
  }
  return pre[t] == -1 ? pre[t] : flow[t];
}
void build() {
  tt = 1;
  n = read();
  k = read();
  for (int i = 2; i <= n + 1; i++) w[i] = read();
  for (int i = 1; i <= n; i++) c[i] = read();
  tot = 2 * (n + 2);
  s = 1;
  t = 2 * (n + 2);
  for (int i = 1; i <= n + 2; i++) {
    int first = i * 2 - 1, la = i * 2;
    add(la - 2, la, k - 1, 0);
    add(first, la, 1, -1e9);
    add(first, la, 1e9, 0);
    if (i != 1) add(first - 1, first, 1, c[w[i]]);
    if (w[i]) {
      if (las[w[i]]) add(2 * las[w[i]], first, 1, 0);
      las[w[i]] = i;
    }
  }
}
void MCMF() {
  int in, x;
  while ((in = spfa()) != -1) {
    x = t;
    while (x != s) {
      a[pre[x]].w -= in;
      a[pre[x] ^ 1].w += in;
      x = a[pre[x] ^ 1].to;
    }
    mf += 1ll * in;
    mc += 1ll * in * dis[t];
  }
}
void work() {
  build();
  MCMF();
  mc += 1ll * (n + 2) * (1e9);
  cout << mc << '\n';
}
}  // namespace network
int main() { network::work(); }
