#include <bits/stdc++.h>
using namespace std;
const int maxn = 500, inf = 1e9;
int n, k, a[maxn], c[maxn], lst[maxn];
struct edge {
  int from, to, cap, flow, cost;
};
struct node {
  int u, dis;
};
inline bool operator<(node a, node b) { return a.dis > b.dis; }
struct PrimDual {
  vector<edge> e;
  vector<int> G[maxn];
  int cur[maxn], dis[maxn], n, m, s, t, min_cost = 0, max_flow = 0, delta;
  bool vis[maxn];
  inline void add_edge(int from, int to, int cap, int cost) {
    e.push_back((edge){from, to, cap, 0, cost});
    e.push_back((edge){to, from, 0, 0, -cost});
    int m = e.size();
    G[from].push_back(m - 2);
    G[to].push_back(m - 1);
  }
  bool spfa() {
    fill(dis, dis + n + 1, inf);
    fill(vis, vis + n + 1, false);
    queue<int> q;
    q.push(t);
    vis[t] = true;
    dis[t] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = (0); i <= ((int)G[u].size() - 1); ++i) {
        edge &E = e[G[u][i]], &W = e[G[u][i] ^ 1];
        if (W.cap > W.flow && dis[E.to] > dis[u] + W.cost) {
          dis[E.to] = dis[u] + W.cost;
          if (!vis[E.to]) {
            vis[E.to] = true;
            q.push(E.to);
          }
        }
      }
      vis[u] = false;
    }
    return dis[s] != inf;
  }
  bool dijkstra() {
    fill(dis, dis + n + 1, inf);
    fill(vis, vis + n + 1, false);
    priority_queue<node> pq;
    dis[t] = 0;
    pq.push((node){t, 0});
    while (!pq.empty()) {
      node x = pq.top();
      pq.pop();
      int u = x.u;
      if (vis[u]) continue;
      vis[u] = false;
      for (int i = (0); i <= ((int)G[u].size() - 1); ++i) {
        edge &E = e[G[u][i]], &W = e[G[u][i] ^ 1];
        if (W.cap > W.flow && dis[E.to] > dis[u] + W.cost) {
          dis[E.to] = dis[u] + W.cost;
          pq.push((node){E.to, dis[E.to]});
        }
      }
    }
    return dis[s] != inf;
  }
  int dfs(int u, int a) {
    if (u == t || a == 0) return a;
    int flow = 0, f;
    vis[u] = true;
    for (int &i = cur[u]; i < G[u].size(); ++i) {
      edge &E = e[G[u][i]];
      if (!vis[E.to] && E.flow < E.cap && E.cost == 0 &&
          (f = dfs(E.to, min(a, E.cap - E.flow)))) {
        E.flow += f;
        e[G[u][i] ^ 1].flow -= f;
        a -= f;
        flow += f;
        if (a == 0) break;
      }
    }
    vis[u] = false;
    return flow;
  }
  void reduce() {
    for (int i = (0); i <= ((int)e.size() - 1); ++i)
      e[i].cost += dis[e[i].to] - dis[e[i].from];
    delta += dis[s] - dis[t];
  }
  void augument() {
    int c = 0;
    memset(cur, 0, sizeof(cur));
    while (memset(vis, 0, sizeof(vis)), (c = dfs(s, inf))) {
      max_flow += c;
      min_cost += c * delta;
    }
  }
  void Min_cost_flow(int s, int t) {
    this->s = s;
    this->t = t;
    if (!spfa()) return;
    reduce();
    augument();
    while (dijkstra()) {
      reduce();
      augument();
    }
  }
} D;
int main() {
  scanf("%d%d", &n, &k);
  int S = n + n + 1, T = n + n + 2;
  D.n = T;
  for (int i = (1); i <= (n); ++i)
    D.add_edge(i, i + n, 1, 0), D.add_edge(i + n, T, 1, 0);
  for (int i = (1); i <= (n - 1); ++i) D.add_edge(i, i + 1, k - 1, 0);
  for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]);
  for (int i = (1); i <= (n); ++i) scanf("%d", &c[i]);
  for (int i = (1); i <= (n); ++i) {
    D.add_edge(S, i, 1, c[a[i]]);
    if (lst[a[i]]) D.add_edge(i - 1, lst[a[i]] + n, 1, -c[a[i]]);
    lst[a[i]] = i;
  }
  D.Min_cost_flow(S, T);
  printf("%d\n", D.min_cost);
  return 0;
}
