#include <bits/stdc++.h>
using namespace std;
inline int gi() {
  int data = 0, m = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') {
    m = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    data = (data << 1) + (data << 3) + (ch ^ 48);
    ch = getchar();
  }
  return (m) ? data : -data;
}
int SS, TT, P0, n, m, K, Init[405], Flow, Cost, A[405], C[405];
struct _Edge {
  int to, next, flow;
  int cost;
} Edge[3 * 405 * 405];
int head[405], CNT, S, T;
int dis[405], flw[405], frm[405];
bool vis[405];
queue<int> Q;
inline void AddEdge(int u, int v, int fl, int cs) {
  Edge[++CNT] = (_Edge){v, head[u], fl, cs};
  head[u] = CNT;
  Edge[++CNT] = (_Edge){u, head[v], 0, -cs};
  head[v] = CNT;
}
inline bool spfa() {
  for (int i = 1; i <= TT; ++i) dis[i] = 1e9 + 7;
  dis[SS] = 0;
  flw[SS] = 1e9;
  vis[SS] = true;
  Q.push(SS);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int e = head[u]; e; e = Edge[e].next) {
      int v = Edge[e].to;
      if (Edge[e].flow && dis[v] > dis[u] + Edge[e].cost) {
        dis[v] = dis[u] + Edge[e].cost;
        frm[v] = e;
        flw[v] = min(flw[u], Edge[e].flow);
        if (!vis[v]) vis[v] = true, Q.push(v);
      }
    }
    vis[u] = false;
  }
  if (dis[TT] == 1e9 + 7) return false;
  for (int x = TT; x ^ SS; x = Edge[frm[x] ^ 1].to) {
    Edge[frm[x]].flow -= flw[TT];
    Edge[frm[x] ^ 1].flow += flw[TT];
  }
  return true;
}
int main() {
  n = gi();
  K = gi();
  CNT = 1;
  P0 = 2 * n + 1;
  S = P0 + 1;
  T = S + 1;
  for (int i = 1; i <= n; i++) A[i] = gi();
  for (int i = 1; i <= n; i++) C[i] = gi();
  AddEdge(S, P0, K, 0);
  for (int i = 1; i <= n; ++i)
    AddEdge(P0, i, 1, C[A[i]]), AddEdge(i + n, T, 1e9 + 7, 0);
  for (int i = 1; i <= n; ++i)
    Init[i]--, Init[i + n]++, AddEdge(i, i + n, 1e9 + 7, 0);
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      AddEdge(i + n, j, 1e9 + 7, (A[i] ^ A[j]) ? C[A[j]] : 0);
  SS = T + 1;
  TT = SS + 1;
  int Delay = 0;
  for (int i = 1; i <= T; i++)
    if (Init[i] < 0)
      AddEdge(i, TT, -Init[i], 0);
    else if (Init[i] > 0)
      AddEdge(SS, i, Init[i], 0), Delay += Init[i];
  AddEdge(T, S, 1e9 + 7, 0);
  while (spfa()) Flow += flw[TT], Cost += flw[TT] * dis[TT];
  if (Flow ^ Delay)
    return puts("-1"), 0;
  else
    printf("%d\n", Cost);
  return 0;
}
