#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int nxt[100005], pre[100005], flow[100005], to[100005], head[100005],
    tot = -1, w[100005], f[100005], dis[100005];
int s, t, n, m, ans = 0, cost, k;
bool vis[100005];
void add(int u, int v, int w1, int f1) {
  nxt[++tot] = head[u];
  head[u] = tot;
  to[tot] = v;
  w[tot] = w1;
  f[tot] = f1;
  nxt[++tot] = head[v];
  head[v] = tot;
  to[tot] = u;
  w[tot] = 0;
  f[tot] = -f1;
}
queue<int> q;
bool spfa() {
  memset(dis, inf, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  dis[s] = 0;
  q.push(s);
  flow[s] = inf;
  vis[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int e = head[u]; e != -1; e = nxt[e]) {
      int v = to[e];
      if (dis[v] > dis[u] + f[e] && w[e]) {
        dis[v] = dis[u] + f[e];
        flow[v] = min(flow[u], w[e]);
        pre[v] = e;
        if (!vis[v]) {
          vis[v] = 1;
          q.push(v);
        }
      }
    }
  }
  if (dis[t] != inf) return 1;
  return 0;
}
void update() {
  int x = t;
  while (x != s) {
    int e = pre[x];
    w[e] -= flow[t];
    w[e ^ 1] += flow[t];
    x = to[e ^ 1];
  }
  ans += flow[t];
  cost += flow[t] * dis[t];
}
void hg() {
  while (spfa()) {
    update();
  }
}
struct asd {
  int l, r;
} a[100005];
int p[1111], b[1111], lt[105], id[105];
int main() {
  memset(head, -1, sizeof(head));
  int l, r, tt = 0, m;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    lt[i] = id[p[i]];
    id[p[i]] = i;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++) ans += b[p[i]];
  s = 0, t = n + 1;
  for (int i = 1; i <= n; i++) {
    add(i - 1, i, k - 1, 0);
    if (i == 1) continue;
    if (lt[i] + 1 == i)
      ans -= b[p[i]];
    else if (lt[i] != 0)
      add(lt[i] + 1, i, 1, -b[p[i]]);
  }
  add(n, n + 1, k - 1, 0);
  if (k != 1) {
    hg();
    cout << ans + cost << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
