#include <bits/stdc++.h>
using namespace std;
int n, m, s, t;
int head[200016], to[200016 << 1], nex[200016 << 1], wto[200016 << 1],
    cto[200016 << 1], ecn = -1;
void ade(int u, int v, int w, int c) {
  to[++ecn] = v, nex[ecn] = head[u], wto[ecn] = w, head[u] = ecn, cto[ecn] = c;
  to[++ecn] = u, nex[ecn] = head[v], wto[ecn] = 0, head[v] = ecn, cto[ecn] = -c;
}
queue<int> Q;
int dis[200016], cur[200016], vis[200016];
const int inf = 1e9;
bool spfa(int s) {
  memset(vis, 0, sizeof vis);
  Q.push(s);
  memset(dis, 0x3f, sizeof dis);
  dis[s] = 0, vis[s] = 1;
  for (int i = (1), iend = (n); i <= iend; ++i) cur[i] = head[i];
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    vis[u] = 0;
    for (int i = head[u]; ~i; i = nex[i])
      if (wto[i] && dis[to[i]] > dis[u] + cto[i]) {
        int v = to[i];
        dis[v] = dis[u] + cto[i];
        if (!vis[v]) Q.push(v), vis[v] = 1;
      }
  }
  return dis[t] < inf;
}
int co;
int dfs(int u, int lim) {
  if (!lim || u == t) return lim;
  vis[u] = 1;
  int flow = 0, f;
  for (int &i = cur[u]; ~i; i = nex[i])
    if (!vis[to[i]] && wto[i] && dis[to[i]] == dis[u] + cto[i] &&
        (f = dfs(to[i], min(lim, wto[i])))) {
      lim -= f, flow += f, wto[i] -= f, wto[i ^ 1] += f;
      co += f * cto[i];
      if (!lim) break;
    }
  vis[u] = 0;
  return flow;
}
int mcmf() {
  int re = 0, x;
  while (spfa(s)) {
    while (x = dfs(s, 0x3f3f3f3f)) re += x;
  }
  return re;
}
int A[200016], lst[200016], c[200016];
void solve() {
  int n, k;
  memset(head, -1, sizeof head);
  cin >> n >> k;
  vector<int> a;
  a.push_back(0);
  for (int i = (1), iend = (n); i <= iend; ++i) {
    scanf("%d", A + i);
    if (A[i] != A[i - 1]) a.push_back(A[i]);
  }
  int res = 0;
  for (int i = (1), iend = (n); i <= iend; ++i) scanf("%d", c + i);
  n = a.size() - 1;
  ::n = n + 2, s = 1, t = n + 2;
  for (int i = (1), iend = (n); i <= iend; ++i) ade(i, i + 1, 0x3f3f3f3f, 0);
  ade(n + 1, n + 2, k - 1, 0);
  for (int i = (n), iend = (1); i >= iend; --i) {
    if (a[i] == a[i - 1]) continue;
    res += c[a[i]];
    if (lst[a[i]]) ade(i + 1, lst[a[i]], 1, -c[a[i]]);
    lst[a[i]] = i;
  }
  mcmf();
  cout << res + co << endl;
}
signed main() { solve(); }
