#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
const int inf = 1e7;
int a[maxn], c[maxn];
struct edge {
  int to, cap, cost, flow, nxt;
} E[maxn * maxn];
int head[maxn], tot, N;
int pre[maxn], dis[maxn];
bool vis[maxn];
void init(int n) {
  N = n;
  tot = 0;
  memset(head, -1, sizeof(head));
}
void add(int u, int v, int cap, int cost) {
  E[tot].to = v, E[tot].cap = cap, E[tot].cost = cost, E[tot].flow = 0,
  E[tot].nxt = head[u], head[u] = tot++;
  E[tot].to = u, E[tot].cap = 0, E[tot].cost = -cost, E[tot].flow = 0,
  E[tot].nxt = head[v], head[v] = tot++;
}
bool spfa(int s, int t) {
  queue<int> q;
  for (int i = 0; i < N; i++) dis[i] = inf, vis[i] = false, pre[i] = -1;
  dis[s] = 0, vis[s] = true;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = head[u]; ~i; i = E[i].nxt) {
      int v = E[i].to;
      if (E[i].cap > E[i].flow && dis[v] > dis[u] + E[i].cost) {
        dis[v] = dis[u] + E[i].cost;
        pre[v] = i;
        if (!vis[v]) {
          vis[v] = true;
          q.push(v);
        }
      }
    }
  }
  return dis[t] <= 0;
}
int mcmf(int s, int t, int& cost) {
  int flow = 0;
  cost = 0;
  while (spfa(s, t)) {
    int Min = inf;
    for (int i = pre[t]; ~i; i = pre[E[i ^ 1].to]) {
      if (Min > E[i].cap - E[i].flow) Min = E[i].cap - E[i].flow;
    }
    for (int i = pre[t]; ~i; i = pre[E[i ^ 1].to]) {
      E[i].flow += Min;
      E[i ^ 1].flow -= Min;
      cost += E[i].cost * Min;
    }
    flow += Min;
  }
  return flow;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  init(2 * n + 4);
  int s = 0, t = 2 * n + 1, s1 = 2 * n + 2, cost = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  add(s1, s, k, 0);
  for (int i = 1; i <= n; i++) {
    add(s, 2 * i - 1, 1, c[a[i]]);
    add(2 * i - 1, 2 * i, 1, -inf);
    add(2 * i, t, 1, 0);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (a[i] == a[j]) {
        add(2 * i, 2 * j - 1, 1, 0);
      } else
        add(2 * i, 2 * j - 1, 1, c[a[j]]);
    }
  }
  int flow = mcmf(s1, t, cost);
  cost += n * inf;
  printf("%d\n", cost);
}
