#include <bits/stdc++.h>
using namespace std;
struct Line {
  int nxt, t, c, l;
} g[820];
int fir[165], tl = 1;
void adl(int f, int t, int c, int cost) {
  g[++tl] = (Line){fir[f], t, c, cost};
  fir[f] = tl;
  g[++tl] = (Line){fir[t], f, 0, -cost};
  fir[t] = tl;
}
int S, T, pre[165], lp[165], flow[165], dis[165];
bool in[165];
queue<int> q;
int spfa() {
  for (int i = 1; i <= T; i++) {
    dis[i] = 1000000000;
    flow[i] = 0;
  }
  flow[S] = 1000000000;
  dis[S] = 0;
  q.push(S);
  in[S] = 1;
  while (!q.empty()) {
    int u = q.front();
    in[u] = 0;
    q.pop();
    for (int i = fir[u], v; i; i = g[i].nxt)
      if (g[i].c && dis[v = g[i].t] > dis[u] + g[i].l) {
        dis[v] = dis[u] + g[i].l;
        flow[v] = min(flow[u], g[i].c);
        lp[v] = i;
        pre[v] = u;
        if (!in[v]) {
          in[v] = 1;
          q.push(v);
        }
      }
  }
  return flow[T];
}
int n, k, a[85], c[85], las[85];
int main() {
  scanf("%d%d", &n, &k);
  S = n + 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++) {
    adl(S, i, 1, c[a[i]]);
    adl(i, i + n, 1, 0);
    adl(i + n, T, 1, 0);
    if (i < n) adl(i, i + 1, k - 1, 0);
    if (las[a[i]]) adl(i - 1, las[a[i]] + n, 1, -c[a[i]]);
    las[a[i]] = i;
  }
  int sav, cans = 0;
  while (sav = spfa()) {
    int u = T;
    while (u != S) {
      int las = pre[u];
      g[lp[u]].c -= sav;
      g[lp[u] ^ 1].c += sav;
      u = las;
    }
    cans += dis[T] * sav;
  }
  printf("%d\n", cans);
  return 0;
}
