#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int maxe = maxn * 2;
const long long BIG = 1e9;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
struct edge {
  int to, v;
  long long f;
  int next;
} elist[maxe];
int head[maxn], etop = 1;
void adde(int from, int to, int v, long long f) {
  elist[++etop] = (edge){to, v, f, head[from]};
  head[from] = etop;
  elist[++etop] = (edge){from, 0, -f, head[to]};
  head[to] = etop;
}
int S, T, nlen;
long long dist[maxn];
int prt[maxn];
int prv[maxn];
int inQ[maxn];
bool spfa() {
  fill(dist + 1, dist + nlen + 1, INF);
  fill(inQ + 1, inQ + nlen + 1, 0);
  queue<int> Q;
  Q.push(S);
  dist[S] = 0;
  while (Q.size()) {
    int u = Q.front();
    Q.pop();
    inQ[u] = 0;
    for (int p = head[u]; p; p = elist[p].next) {
      int to = elist[p].to;
      long long v = elist[p].f;
      if (elist[p].v && dist[to] > dist[u] + v) {
        dist[to] = dist[u] + v;
        prt[to] = u;
        prv[to] = p;
        if (!inQ[to]) Q.push(to), inQ[to] = 1;
      }
    }
  }
  return dist[T] != INF;
}
int N, K;
int A[maxn];
int W[maxn];
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &A[i]);
  }
  for (int i = 1; i <= N; i++) {
    scanf("%d", &W[i]);
  }
  nlen = N * 2;
  S = ++nlen;
  T = ++nlen;
  for (int i = 1; i <= N; i++) {
    adde(S, i, 1, W[A[i]]);
    adde(i, i + N, 1, -BIG);
    adde(i + N, T, 1, 0);
  }
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      adde(i + N, j, 1, A[i] == A[j] ? 0 : W[A[j]]);
    }
  }
  long long ans = BIG * N;
  for (int i = 1; i <= K; i++) {
    spfa();
    if (dist[T] > 0) break;
    ans += dist[T];
    for (int u = T; prt[u]; u = prt[u]) {
      int p = prv[u];
      elist[p].v--;
      elist[p ^ 1].v++;
    }
  }
  cout << ans << '\n';
  return 0;
}
