#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int maxn = 510;
const int maxm = 2e6 + 1E2;
const int inf = 1e9;
int point[maxn], nxt[maxm], to[maxm];
int flow[maxm], pre[maxm], cost[maxm];
int from[maxn], vis[maxn], dis[maxn];
int cnt = 1, n, m;
int pos[2000100];
int a[maxn], w[maxn];
int ans;
int s, t;
void addedge(int x, int y, int w, int f) {
  nxt[++cnt] = point[x];
  to[cnt] = y;
  pre[cnt] = x;
  cost[cnt] = w;
  flow[cnt] = f;
  point[x] = cnt;
}
void insert(int x, int y, int w, int f) {
  addedge(x, y, w, f);
  addedge(y, x, -w, 0);
}
queue<int> q;
bool spfa(int s) {
  memset(vis, 0, sizeof(vis));
  memset(dis, 127 / 3, sizeof(dis));
  dis[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    vis[x] = 0;
    for (int i = point[x]; i; i = nxt[i]) {
      int p = to[i];
      if (flow[i] > 0 && dis[p] > dis[x] + cost[i]) {
        dis[p] = dis[x] + cost[i];
        from[p] = i;
        if (!vis[p]) {
          q.push(p);
          vis[p] = 1;
        }
      }
    }
  }
  if (dis[t] >= dis[maxn - 3])
    return 0;
  else
    return 1;
}
void mcf() {
  int x = inf;
  for (int i = from[t]; i; i = from[pre[i]]) {
    x = min(x, flow[i]);
  }
  for (int i = from[t]; i; i = from[pre[i]]) {
    flow[i] -= x;
    flow[i ^ 1] += x;
    ans += x * cost[i];
  }
}
void solve() {
  while (spfa(s)) mcf();
}
int k;
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) w[i] = read();
  s = maxn - 10;
  t = s + 1;
  for (int i = 1; i <= n; i++) {
    insert(s, i, w[a[i]], 1);
    insert(i, i + n, 0, 1);
    insert(i + n, t, 0, 1);
    if (pos[a[i]]) insert(i - 1, pos[a[i]] + n, -w[a[i]], 1);
    pos[a[i]] = i;
  }
  for (int i = 1; i < n; i++) insert(i, i + 1, 0, k - 1);
  solve();
  cout << ans;
  return 0;
}
