#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[500], c[500];
struct graph {
  vector<int> to, w, c, ed[500];
  long long dis[500];
  int cur[500];
  bool vis[500];
  void add_edge(int a, int b, int aw, int ac) {
    to.push_back(b);
    w.push_back(aw);
    c.push_back(ac);
    ed[a].push_back(to.size() - 1);
    to.push_back(a);
    w.push_back(0);
    c.push_back(-ac);
    ed[b].push_back(to.size() - 1);
  }
  bool spfa() {
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    queue<int> q;
    dis[0] = 0;
    q.push(0);
    vis[0] = 1;
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      for (register int i = 0; i < ed[x].size(); i++) {
        int e = ed[x][i];
        if (dis[to[e]] > dis[x] + c[e] && w[e]) {
          dis[to[e]] = dis[x] + c[e];
          if (!vis[to[e]]) vis[to[e]] = 1, q.push(to[e]);
        }
      }
      vis[x] = 0;
    }
    return dis[(2 * n + 1)] < 1000000007;
  }
  int dfs(int x, int lim) {
    if (x == (2 * n + 1) || !lim) return lim;
    long long sum = 0;
    vis[x] = 1;
    for (register int &i = cur[x]; i < ed[x].size(); i++) {
      int e = ed[x][i];
      if (dis[x] + c[e] == dis[to[e]] && w[e] && !vis[to[e]]) {
        int f = dfs(to[e], min(lim, w[e]));
        w[e] -= f;
        w[1 ^ e] += f;
        lim -= f;
        sum += f;
        if (!lim) return sum;
      }
    }
    return sum;
  }
  long long zkw() {
    long long ret = 0;
    while (spfa()) {
      memset(vis, 0, sizeof(vis));
      memset(cur, 0, sizeof(cur));
      int f = dfs(0, n);
      k -= f;
      if (k < 0) {
        if ((k + f) * dis[(2 * n + 1)] < 0) ret += (k + f) * dis[(2 * n + 1)];
        return ret;
      }
      if (f * dis[(2 * n + 1)] < 0) ret += f * dis[(2 * n + 1)];
    }
    return ret;
  }
} G;
int main() {
  scanf("%d %d", &n, &k);
  for (register int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (register int i = 1; i <= n; i++) scanf("%d", &c[i]);
  for (register int i = 1; i <= n; i++) G.add_edge(0, i, 1, c[a[i]]);
  for (register int i = 1; i <= n; i++) G.add_edge(i, n + i, 1, -1000000007);
  for (register int i = 1; i <= n; i++) G.add_edge(n + i, (2 * n + 1), 1, 0);
  for (register int i = 1; i < n; i++) {
    for (register int j = i + 1; j <= n; j++)
      if (a[i] != a[j])
        G.add_edge(n + i, j, 1, c[a[j]]);
      else
        G.add_edge(n + i, j, 1, 0);
  }
  cout << G.zkw() + n * 1LL * 1000000007 << endl;
  return 0;
}
