#include <bits/stdc++.h>
template <int xn, int xe>
struct mcf {
  int head[xn], next[xe], to[xe], fl[xe], N, E, q[xn], fa[xn], ent[xn], in[xn];
  long long co[xe], dis[xn];
  inline void add(int u, int v, int f, long long c) {
    next[E] = head[u], to[E] = v, fl[E] = f, co[E] = c, head[u] = E++;
    next[E] = head[v], to[E] = u, fl[E] = 0, co[E] = -c, head[v] = E++;
  }
  void clear(int n) {
    N = n, E = 0;
    for (int i = 0; i < n; i++) head[i] = -1;
  }
  bool spfa() {
    for (int i = 0; i < N; i++) {
      dis[i] = 1LL << 60;
      in[i] = 0;
    }
    dis[0] = 0;
    in[0] = 1;
    q[0] = 0;
    int H = -1, T = 0;
    while (H != T) {
      if (++H == xn) H = 0;
      int u = q[H];
      in[u] = 0;
      for (int e = head[u]; ~e; e = next[e])
        if (fl[e] && dis[to[e]] > dis[u] + co[e]) {
          dis[to[e]] = dis[u] + co[e];
          fa[to[e]] = u;
          ent[to[e]] = e;
          if (!in[to[e]]) {
            if (++T == xn) T = 0;
            in[q[T] = to[e]] = 1;
          }
        }
    }
    return dis[N - 1] != 1LL << 60;
  }
  long long operator()() {
    long long r = 0;
    while (spfa()) {
      int f = 1 << 30;
      for (int i = N - 1; i; i = fa[i]) f = std::min(f, fl[ent[i]]);
      r += f * dis[N - 1];
      for (int i = N - 1; i; i = fa[i]) {
        fl[ent[i]] -= f;
        fl[ent[i] ^ 1] += f;
      }
    }
    return r;
  }
};
mcf<10000, 10000> G;
int N, K, a[81], b[81], O;
std::map<int, std::vector<int> > V;
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%d", a + i);
    V[a[i]].push_back(i);
  }
  for (int i = 1; i <= N; i++) scanf("%d", b + i);
  G.clear(N + 2);
  G.add(0, 1, V.size(), 0);
  for (int i = 1; i <= N; i++) G.add(i, i + 1, K, 0);
  for (auto &i : V) {
    O += b[i.first];
    if (i.second.front() > 1) G.add(1, i.second.front(), 1, 0);
    if (i.second.back() < N) G.add(i.second.back() + 1, N + 1, 1, 0);
    for (size_t j = 1; j < i.second.size(); j++)
      if (i.second[j] - i.second[j - 1] > 1)
        G.add(i.second[j - 1] + 1, i.second[j], 1, b[i.first]);
  }
  printf("%lld\n", O + G());
  return 0;
}
