#include <bits/stdc++.h>
using namespace std;
const int N = 5001, M = 500001, P = 100;
int m, n, s, t;
int head[N], to[M], nxt[M], dis[N], flow[N], cst[M], val[M], pre[N], last[N],
    cnt = 1;
bool vis[N];
void add_edge(int u, int v, int ncst, int nval) {
  nxt[++cnt] = head[u], head[u] = cnt, val[cnt] = nval, cst[cnt] = ncst,
  to[cnt] = v;
  nxt[++cnt] = head[v], head[v] = cnt, val[cnt] = 0, cst[cnt] = -ncst,
  to[cnt] = u;
}
int maxflow = 0;
bool find() {
  queue<int> q;
  memset(vis, 0, sizeof(vis));
  memset(dis, 0x3f3f3f3f, sizeof(dis));
  memset(flow, 0x7f, sizeof(flow));
  int h;
  q.push(s);
  vis[s] = 1;
  pre[s] = 0;
  dis[s] = 0;
  while (!q.empty()) {
    h = q.front();
    q.pop();
    vis[h] = 0;
    for (int i = head[h]; i; i = nxt[i]) {
      if (val[i] > 0 && dis[to[i]] > dis[h] + cst[i]) {
        dis[to[i]] = dis[h] + cst[i];
        flow[to[i]] = min(val[i], flow[h]);
        last[to[i]] = i;
        pre[to[i]] = h;
        if (!vis[to[i]]) {
          vis[to[i]] = true;
          q.push(to[i]);
        }
      }
    }
  }
  return dis[t] != 0x3f3f3f3f;
}
int mincost;
int EK() {
  int ans = 0;
  mincost = 0;
  while (find()) {
    maxflow = flow[t];
    ans += maxflow;
    mincost += maxflow * dis[t];
    for (int i = t; i; i = pre[i]) {
      val[last[i]] -= maxflow;
      val[last[i] ^ 1] += maxflow;
    }
  }
  return ans;
}
int P1[P], P2[P], nd, k, a[P], c[P];
vector<int> pos[P];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    P1[i] = ++nd, P2[i] = ++nd, cin >> a[i], pos[a[i]].push_back(i);
  for (int i = 1; i <= n; ++i) cin >> c[i];
  s = ++nd, t = ++nd;
  for (int i = 1; i <= n; ++i)
    add_edge(s, P1[i], c[a[i]], 1), add_edge(P1[i], P2[i], 0, 1),
        add_edge(P2[i], t, 0, 1);
  for (int i = 1; i < n; ++i) add_edge(P1[i], P1[i + 1], 0, k - 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < pos[i].size(); ++j)
      add_edge(P1[pos[i][j] - 1], P2[pos[i][j - 1]], -c[i], 1);
  EK();
  cout << mincost << endl;
}
