#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  int f(1);
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
  x *= f;
}
const int maxn = 85;
const int inf = 0x3f3f3f3f;
int n, K, a[maxn], C[maxn];
int hd[maxn], cnt, S, T, lst[maxn];
int dis[maxn], vis[maxn], ANS;
struct edge {
  int ed, nxt, f, c;
};
edge e[maxn * maxn * maxn];
void star(int u, int v, int c, int f) {
  e[++cnt].ed = v;
  e[cnt].c = c;
  e[cnt].f = f;
  e[cnt].nxt = hd[u];
  hd[u] = cnt;
}
int dfs(int, int);
bool spfa(int);
int main() {
  read(n);
  read(K);
  cnt = 1;
  for (int i(1); i <= n; ++i) read(a[i]);
  for (int i(1); i <= n; ++i) read(C[i]);
  for (int i(1); i <= n; ++i) ANS += C[a[i]];
  S = n + 1;
  T = n + 2;
  for (int i(1); i < n; ++i) star(i, i + 1, K - 1, 0), star(i + 1, i, 0, 0);
  star(S, 1, K - 1, 0);
  star(1, S, 0, 0);
  star(n, T, K - 1, 0);
  star(T, n, 0, 0);
  for (int i(1); i <= n; ++i) {
    if (lst[a[i]] != 0) {
      if (lst[a[i]] == i - 1)
        ANS -= C[a[i]];
      else {
        star(lst[a[i]], i - 1, 1, -C[a[i]]);
        star(i - 1, lst[a[i]], 0, C[a[i]]);
      }
    }
    lst[a[i]] = i;
  }
  while (spfa(S)) {
    vis[T] = 1;
    while (vis[T]) {
      memset(vis, 0, sizeof vis);
      dfs(S, inf);
    }
  }
  printf("%d\n", ANS);
  return 0;
}
bool spfa(int s) {
  queue<int> q;
  q.push(s);
  memset(dis, 0x3f, sizeof dis);
  memset(vis, 0, sizeof vis);
  vis[s] = 1;
  dis[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int i = hd[u]; i; i = e[i].nxt) {
      int y = e[i].ed;
      if (dis[y] > dis[u] + e[i].f && e[i].c > 0) {
        dis[y] = dis[u] + e[i].f;
        if (!vis[y]) q.push(y), vis[y] = 1;
      }
    }
  }
  return (dis[T] <= 0);
}
int dfs(int x, int lowx) {
  if (x == T) {
    vis[x] = 1;
    return lowx;
  }
  int ret = 0;
  vis[x] = 1;
  for (int i = hd[x]; i; i = e[i].nxt) {
    int y = e[i].ed;
    if ((!vis[y] || y == T) && (dis[y] == dis[x] + e[i].f) && e[i].c > 0) {
      int tmp = dfs(y, min(lowx, e[i].c));
      if (tmp)
        ANS += e[i].f * tmp, ret += tmp, lowx -= tmp, e[i].c -= tmp,
            e[i ^ 1].c += tmp;
      if (!lowx) break;
    }
  }
  return ret;
}
