#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  x *= f;
}
template <typename T>
void chkmax(T &x, T y) {
  x = max(x, y);
}
template <typename T>
void chkmin(T &x, T y) {
  x = min(x, y);
}
int n, m, S, T;
struct Ans {
  int flow, cost;
} ans;
const int N = 5010, M = 100010, inf = 2e9;
struct Dinic {
  int cnt;
  int pre[N], lst[N], dp[N], fir[N], flow[N], dis[N];
  bool vis[N];
  struct edge {
    int to, w, c, nxt;
  } e[M];
  queue<int> q;
  void Prepare() {
    cnt = -1;
    memset(fir, -1, sizeof fir);
  }
  void Ade(int u, int v, int w, int c) {
    e[++cnt] = (edge){v, w, c, fir[u]}, fir[u] = cnt;
    e[++cnt] = (edge){u, 0, -c, fir[v]}, fir[v] = cnt;
  }
  bool Spfa() {
    memset(dis, 63, sizeof dis);
    memset(flow, 63, sizeof flow);
    memset(vis, 0, sizeof vis);
    while (!q.empty()) q.pop();
    q.push(S), dis[S] = 0, vis[S] = 1, pre[T] = -1;
    int u, v;
    while (!q.empty()) {
      u = q.front();
      q.pop();
      vis[u] = 0;
      for (int i = fir[u]; ~i; i = e[i].nxt)
        if (e[i].w && dis[v = e[i].to] > dis[u] + e[i].c) {
          dis[v] = dis[u] + e[i].c;
          pre[v] = u, lst[v] = i;
          flow[v] = min(flow[u], e[i].w);
          if (!vis[v]) vis[v] = 1, q.push(v);
        }
    }
    return pre[T] != -1;
  }
  Ans MincostMaxflow() {
    int Flow = 0, Cost = 0;
    while (Spfa()) {
      int now = T;
      Flow += flow[T], Cost += flow[T] * dis[T];
      while (now != S)
        e[lst[now]].w -= flow[T], e[lst[now] ^ 1].w += flow[T], now = pre[now];
    }
    return (Ans){Flow, Cost};
  }
} D;
int x, y, z, w, k;
int a[100], c[100], lst[100];
int main() {
  read(n), read(k);
  S = 0, T = 2 * n + 1;
  D.Prepare();
  for (int i = 1; i <= n; ++i) read(a[i]);
  for (int i = 1; i <= n; ++i) read(c[i]);
  for (int i = 1; i <= n; ++i) {
    if (lst[a[i]]) D.Ade(i - 1, lst[a[i]] + n, 1, -c[a[i]]);
    lst[a[i]] = i;
    D.Ade(S, i, 1, c[a[i]]);
    D.Ade(i + n, T, 1, 0);
    D.Ade(i, i + n, 1, 0);
    if (i < n) D.Ade(i, i + 1, k - 1, 0);
  }
  ans = D.MincostMaxflow();
  cout << ans.cost << '\n';
  return 0;
}
