#include <bits/stdc++.h>
using namespace std;
const int M = 40010;
const int N = 200;
const long long Inf = 1e9 + 7;
struct bian {
  int to, nxt, f, c;
} e[M];
int hea[N], en = 1;
void add(int u, int v, int f, int c) {
  e[++en] = (bian){v, hea[u], f, c};
  hea[u] = en;
  e[++en] = (bian){u, hea[v], 0, -c};
  hea[v] = en;
}
long long dis[N];
int src, sink, prevvv[N], vis[N], tot = 0;
queue<int> q;
bool spfa() {
  for (int i = 1; i <= tot; i++) dis[i] = 1e14;
  q.push(src);
  dis[src] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = hea[u]; i; i = e[i].nxt) {
      if (!e[i].f) continue;
      int v = e[i].to;
      if (dis[v] > dis[u] + e[i].c) {
        dis[v] = dis[u] + e[i].c;
        prevvv[v] = i;
        if (!vis[v]) {
          vis[v] = 1;
          q.push(v);
        }
      }
    }
    vis[u] = 0;
  }
  return dis[sink] < dis[src];
}
long long ans;
void arg(int k) {
  while (k--) {
    if (!spfa()) break;
    int u = sink, i;
    while (u != src) {
      i = prevvv[u];
      e[i].f--;
      e[i ^ 1].f++;
      u = e[i ^ 1].to;
    }
    ans = ans + dis[sink];
  }
}
int n, k, a[N], c[N], ru[N], cu[N];
int main() {
  scanf("%d%d", &n, &k);
  src = ++tot;
  sink = ++tot;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), ru[i] = ++tot;
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]), cu[i] = ++tot;
  for (int i = 1; i <= n; i++) {
    add(src, ru[i], 1, c[a[i]]);
    add(cu[i], sink, 1, 0);
    add(ru[i], cu[i], 1, -Inf);
    for (int j = 1; j < i; j++) {
      add(cu[j], ru[i], 1, (a[i] == a[j]) ? 0 : c[a[i]]);
    }
  }
  ans = Inf * n;
  if (k > n) k = n;
  arg(k);
  printf("%lld\n", ans);
  return 0;
}
