#include <bits/stdc++.h>
using namespace std;
int IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}
const int N = 5000 + 19;
const int M = 200000;
const long long oo = 1ll << 60;
const int inf = (1 << 30) - 1;
struct Edge {
  int y, f, c, nxt;
} E[M];
long long dis[N], res;
int vis[N], pre[N], las[N];
int A[N], C[N];
int n, K, cnt, S, T, s1, s2, t, tot;
void Add_Edge(int x, int y, int f, int c) {
  E[cnt] = (Edge){y, f, c, las[x]};
  las[x] = cnt++;
  E[cnt] = (Edge){x, 0, -c, las[y]};
  las[y] = cnt++;
}
bool spfa() {
  static int Q[N * 100];
  int f = 1, w = 0;
  Q[1] = S;
  for (int i = 1; i < T + 1; i++) dis[i] = oo;
  dis[S] = 0;
  pre[S] = -1;
  while (f > w) {
    int x = Q[++w];
    vis[x] = 0;
    for (int i = las[x], y; ~i; i = E[i].nxt)
      if (E[i].f && dis[x] + E[i].c < dis[y = E[i].y]) {
        dis[y] = dis[x] + E[i].c;
        pre[y] = i;
        if (!vis[y]) {
          vis[y] = 1;
          Q[++f] = y;
        }
      }
  }
  return dis[T] < 0;
}
void Update() {
  for (int i = pre[T]; ~i; i = pre[E[i ^ 1].y]) E[i].f--, E[i ^ 1].f++;
  res += dis[T];
}
int main() {
  memset(las, -1, sizeof(las));
  n = IN(), K = IN();
  for (int i = 1; i < n + 1; i++) A[i] = IN();
  for (int i = 1; i < n + 1; i++) C[i] = IN();
  S = 2 * n + 1, T = S + 1;
  for (int i = 1; i < n + 1; i++) {
    Add_Edge(S, 2 * i - 1, 1, C[A[i]]);
    Add_Edge(2 * i - 1, 2 * i, 1, -inf);
    Add_Edge(2 * i, T, 1, 0);
  }
  for (int i = 1; i < n + 1; i++)
    for (int j = i + 1; j < n + 1; j++) {
      Add_Edge(2 * i, 2 * j - 1, 1, A[i] == A[j] ? 0 : C[A[j]]);
    }
  for (int i = 0; i < K; i++) {
    if (!spfa()) break;
    Update();
  }
  cout << res + 1ll * inf * n << endl;
}
