#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long k = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    k = k * 10 + ch - '0';
    ch = getchar();
  }
  return k * f;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(long long x) {
  write(x);
  puts("");
}
const long long N = 5010, inf = 1e12;
long long n, K, a[N], b[N], e[N][2];
const long long M = 4e5 + 10;
long long nedge = 0, p[2 * M], nex[2 * M], head[2 * M], c[2 * M], cc[2 * M];
inline void addedge(long long a, long long b, long long v, long long vv) {
  p[++nedge] = b;
  nex[nedge] = head[a];
  head[a] = nedge;
  c[nedge] = v;
  cc[nedge] = vv;
  p[++nedge] = a;
  nex[nedge] = head[b];
  head[b] = nedge;
  c[nedge] = 0;
  cc[nedge] = -vv;
}
long long s, t, ans = 0;
long long dist[2 * N], vis[2 * N], cur[2 * N], distt[2 * N];
inline long long fan(long long x) { return (x & 1) ? x + 1 : x - 1; }
inline bool spfa(long long s, long long t) {
  memset(vis, 0, sizeof vis);
  for (long long i = s; i <= t; i++) dist[i] = 1e9;
  dist[t] = 0;
  vis[t] = 1;
  deque<long long> q;
  q.push_back(t);
  while (!q.empty()) {
    long long now = q.front();
    q.pop_front();
    for (long long k = head[now]; k; k = nex[k])
      if (c[fan(k)] && dist[p[k]] > dist[now] - cc[k]) {
        dist[p[k]] = dist[now] - cc[k];
        if (!vis[p[k]]) {
          vis[p[k]] = 1;
          if (!q.empty() && dist[p[k]] < dist[q.front()])
            q.push_front(p[k]);
          else
            q.push_back(p[k]);
        }
      }
    vis[now] = 0;
  }
  return dist[s] < 1e9;
}
inline long long dfs(long long x, long long low) {
  if (x == t) {
    vis[t] = 1;
    return low;
  }
  long long used = 0, a;
  vis[x] = 1;
  for (long long k = head[x]; k; k = nex[k])
    if (!vis[p[k]] && c[k] && dist[x] - cc[k] == dist[p[k]]) {
      a = dfs(p[k], min(c[k], low - used));
      if (a) ans += a * cc[k], c[k] -= a, c[fan(k)] += a, used += a;
      if (used == low) break;
    }
  return used;
}
inline long long costflow() {
  long long flow = 0;
  while (spfa(s, t)) {
    vis[t] = 1;
    while (vis[t]) {
      memset(vis, 0, sizeof vis);
      flow += dfs(s, 1e9);
    }
  }
  return flow;
}
long long cntp = 0, cntk, cp[N];
map<long long, bool> mp;
signed main() {
  n = read();
  K = read();
  s = 0;
  for (long long i = 1; i <= n; i++) {
    b[i] = read();
    if (!mp[b[i]]) {
      mp[b[i]] = 1;
      cntk++;
    }
  }
  K = min(K, cntk);
  for (long long i = 1; i <= n; i++) a[i] = read();
  for (long long i = 1; i <= n; i++) {
    e[i][0] = ++cntp;
    e[i][1] = ++cntp;
    addedge(e[i][0], e[i][1], 1, -inf);
  }
  for (long long i = 1; i <= n; i++) cp[i] = ++cntp;
  t = ++cntp;
  cp[n + 1] = t;
  addedge(s, cp[1], K, 0);
  for (long long i = 1; i <= n; i++) {
    addedge(cp[i], cp[i + 1], K, 0);
    for (long long j = i + 1; j <= n; j++)
      if (b[j] == b[i]) {
        addedge(e[i][1], e[j][0], 1, 0);
        break;
      }
    addedge(cp[i], e[i][0], 1, 0);
    addedge(e[i][1], cp[i + 1], 1, a[b[i]]);
  }
  costflow();
  writeln(ans + n * inf);
  return 0;
}
