#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
namespace MCMF {
struct e {
  int to, nxt, val, cst;
} ed[400000];
int head[400000], cur[400000], top = 1, n, s, t;
void add_edge(int u, int v, int val, int cst) {
  top++;
  ed[top].to = v, ed[top].val = val, ed[top].cst = cst;
  ed[top].nxt = head[u];
  head[u] = top;
}
void add_pair(int u, int v, int val, int cst) {
  add_edge(u, v, val, cst);
  add_edge(v, u, 0, -cst);
}
int dis[400000];
bool spfa() {
  for (int i = 0; i <= n; i++) dis[i] = INF;
  dis[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = ed[i].nxt) {
      int v = ed[i].to;
      if (ed[i].val > 0 && dis[v] > dis[u] + ed[i].cst) {
        dis[v] = dis[u] + ed[i].cst;
        q.push(v);
      }
    }
  }
  return dis[t] < INF;
}
bool vis[20000];
int dfs(int u, int flow) {
  if (u == t) return flow;
  int rlow = 0;
  vis[u] = 1;
  for (int &i = cur[u]; i; i = ed[i].nxt) {
    int v = ed[i].to;
    if (!vis[v] && dis[v] == dis[u] + ed[i].cst && ed[i].val > 0) {
      int tmp = dfs(v, min(ed[i].val, flow - rlow));
      rlow += tmp, ed[i].val -= tmp, ed[i ^ 1].val += tmp;
    }
  }
  vis[u] = 0;
  return rlow;
}
int Dinic() {
  int sum_flow = 0, sum_cst = 0, tmp = 0;
  while (spfa()) {
    for (int i = 0; i <= n; i++) cur[i] = head[i];
    while (tmp = dfs(s, INF)) {
      sum_flow += tmp, sum_cst += dis[t] * tmp;
    }
  }
  return sum_cst;
}
void init(int nn, int ss, int tt) {
  n = nn, s = ss, t = tt, top = 1;
  memset(head, 0, sizeof(head));
}
};  // namespace MCMF
int a[200], c[200];
int n = 0, k = 0;
inline int in(int x) { return x; }
inline int out(int x) { return x + n; }
int last[200];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  MCMF::init(2 * n + 1, 0, 2 * n + 1);
  for (int i = 1; i <= n; i++) {
    MCMF::add_pair(0, in(i), 1, c[a[i]]);
    MCMF::add_pair(in(i), out(i), 1, 0);
  }
  for (int i = 1; i < n; i++) MCMF::add_pair(in(i), in(i + 1), k - 1, 0);
  for (int i = 1; i <= n; i++) {
    if (last[a[i]]) {
      MCMF::add_pair(in(i - 1), out(last[a[i]]), 1, -c[a[i]]);
    }
    last[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) MCMF::add_pair(out(i), 2 * n + 1, 1, 0);
  printf("%d", MCMF::Dinic());
  return 0;
}
