#include <bits/stdc++.h>
using namespace std;
struct edge {
  int u, v, cost;
  edge(int u, int v, int cost) : u(u), v(v), cost(cost) {}
  edge() { u = v = cost = 0; }
};
int t, source = 0, sink = 201, a[110], b[110], vis[210];
vector<int> near[210];
edge e[7000];
long long dist[210];
int prv[210];
int n, k;
bool spfa(int src = source) {
  bool ok = false;
  queue<int> q;
  q.push(src);
  vis[src] = 1;
  fill(dist, dist + 210, (1LL << 60));
  dist[src] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (auto x : near[u]) {
      int v = e[x].v;
      if (v == u) continue;
      if (dist[v] > dist[u] + e[x].cost) {
        dist[v] = dist[u] + e[x].cost;
        prv[v] = x;
        if (v == sink) ok = true;
        if (!vis[v]) {
          vis[v] = 1;
          q.push(v);
        }
      }
    }
  }
  return ok;
}
void reset() {
  t = 0;
  for (int i = 0; i < 300; i++) near[i].clear();
}
void addedge(int u, int v, int w) {
  near[u].push_back((t));
  near[v].push_back((t));
  e[t++] = edge(u, v, w);
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = 1; i <= n; i++) {
    addedge(source, 2 * i - 1, b[a[i]]);
    addedge(2 * i - 1, 2 * i, -1000000007);
    addedge(2 * i, sink, 0);
  }
  for (int i = 1; i < n; i++)
    for (int j = i + 1; j <= n; j++)
      addedge(2 * i, 2 * j - 1, b[a[j]] * (a[j] != a[i]));
  long long res = 1LL * n * 1000000007;
  for (int i = 0; i < k; i++) {
    if (not spfa()) break;
    if (dist[sink] >= 0) break;
    res += dist[sink];
    for (int pos = sink; pos != source;) {
      int x = prv[pos];
      e[x].cost *= -1;
      swap(e[x].u, e[x].v);
      pos = e[x].v;
    }
  }
  cout << res << endl;
  return 0;
}
