#include <bits/stdc++.h>
using namespace std;
const int N = 85;
struct edge {
  int from, to, next, f, v;
} e[N * N * 2];
int head[N * 2], tot = 1;
int q[N * N], from[N * 2], vis[N * 2];
int n, k, S, T;
long long dis[N * 2], ans;
void add(int x, int y, int v, int f) {
  e[++tot] = (edge){x, y, head[x], f, v};
  head[x] = tot;
  e[++tot] = (edge){y, x, head[y], 0, -v};
  head[y] = tot;
}
void bfs() {
  for (int i = (int)(0); i <= (int)(2 * N - 1); i++)
    dis[i] = (1ll << 30) * (1ll << 30), vis[i] = 0;
  int h = 0, t = 1;
  q[1] = S;
  vis[S] = 1;
  dis[S] = 0;
  while (h != t) {
    int x = q[++h];
    vis[x] = 0;
    for (int i = head[x]; i; i = e[i].next)
      if (dis[e[i].to] > dis[x] + e[i].v && e[i].f) {
        dis[e[i].to] = dis[x] + e[i].v;
        from[e[i].to] = i;
        if (!vis[e[i].to]) {
          vis[e[i].to] = 1;
          q[++t] = e[i].to;
        }
      }
  }
}
int v[N], w[N];
int main() {
  scanf("%d%d", &n, &k);
  S = 0;
  T = 1;
  k = min(n, k);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &v[i]);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &w[i]);
  for (int i = (int)(1); i <= (int)(n); i++) {
    add(S, i * 2, w[v[i]], 1);
    add(i * 2, i * 2 + 1, -(1ll << 30), 1);
    add(i * 2 + 1, T, 0, 1);
  }
  for (int i = (int)(1); i <= (int)(n); i++)
    for (int j = (int)(i + 1); j <= (int)(n); j++)
      add(i * 2 + 1, j * 2, w[v[j]], 1);
  for (int i = (int)(1); i <= (int)(n); i++)
    for (int j = (int)(i + 1); j <= (int)(n); j++)
      if (v[i] == v[j]) add(i * 2 + 1, j * 2, 0, 1);
  for (int i = (int)(1); i <= (int)(k); i++) {
    bfs();
    if (dis[T] >= 0) break;
    ans += dis[T];
    for (int j = from[T]; j; j = from[e[j].from]) e[j].f--, e[j ^ 1].f++;
  }
  printf("%lld\n", ans + 1ll * (1ll << 30) * n);
}
