#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, maxn = 200007;
const long long MOD = 998244353;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const long long P = 19260817;
char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * f;
}
inline long long readll() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * f;
}
void write(int x) {
  if (!x) putchar('0');
  if (x < 0) x = -x, putchar('-');
  static int sta[20];
  register int tot = 0;
  while (x) sta[tot++] = x % 10, x /= 10;
  while (tot) putchar(sta[--tot] + 48);
}
int mod1(int x) {
  if (x >= MOD) x -= MOD;
  return x;
}
int n, k;
int a[maxn], c[maxn];
int lst[maxn];
struct mcmf {
  struct edge {
    int to, nxt, c, val;
  } e[maxn << 1];
  int tot = 1, head[maxn];
  void addedge(int u, int v, int val, int cost) {
    e[++tot] = (edge){v, head[u], cost, val};
    head[u] = tot;
    e[++tot] = (edge){u, head[v], -cost, 0};
    head[v] = tot;
  }
  int s, t;
  int dis[maxn], pre[maxn], vis[maxn];
  bool spfa() {
    queue<int> q;
    while (!q.empty()) q.pop();
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    memset(pre, 0, sizeof(pre));
    q.push(s);
    dis[s] = 0;
    vis[s] = 1;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      vis[u] = 0;
      for (int i = head[u]; i; i = e[i].nxt) {
        int v = e[i].to;
        if (e[i].val && dis[v] > dis[u] + e[i].c) {
          dis[v] = dis[u] + e[i].c, pre[v] = i;
          if (!vis[v]) {
            vis[v] = true;
            q.push(v);
          }
        }
      }
    }
    return (pre[t] != 0);
  }
  int query() {
    int mc = 0, mf = 0;
    while (spfa()) {
      int flow = INF;
      for (int i = pre[t]; i; i = pre[e[i ^ 1].to]) flow = min(flow, e[i].val);
      mf += flow;
      for (int i = pre[t]; i; i = pre[e[i ^ 1].to]) {
        e[i].val -= flow, e[i ^ 1].val += flow, mc += flow * e[i].c;
      }
    }
    return mc;
  }
} F;
vector<int> v;
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);
  int sum = 0;
  v.push_back(0);
  for (int i = 1; i <= n; i++)
    if (a[i] != a[i - 1]) v.push_back(a[i]);
  n = v.size() - 1;
  for (int i = 1; i <= n; i++) {
    sum += c[v[i]];
    if (lst[v[i]]) F.addedge(lst[v[i]] + 1, i, 1, -c[v[i]]);
    lst[v[i]] = i;
    if (i != n) F.addedge(i, i + 1, k - 1, 0);
  }
  F.s = n + 1, F.t = n + 2;
  F.addedge(F.s, 1, k - 1, 0);
  F.addedge(n, F.t, k - 1, 0);
  printf("%d", sum + F.query());
  return 0;
}
