#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.first << "," << p.second << ")";
  return o;
}
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}
template <typename T>
class MinCostFlow {
 private:
  struct edge {
    int to;
    T cap, cost;
    int rev;
  };
  using P = pair<int, int>;
  vector<vector<edge>> Graph;
  vector<int> prevv, preve;
  vector<T> h, d;

 public:
  MinCostFlow(int v) {
    Graph.resize(v);
    prevv.resize(v);
    preve.resize(v);
    h.resize(v);
    d.resize(v);
  }
  T min_cost_flow(int s, int t, T f) {
    T res = 0;
    fill((h).begin(), (h).end(), 0);
    for (int v = (int)(0); v < (int)(Graph.size()); v++) {
      for (int j = (int)(0); j < (int)(Graph[v].size()); j++) {
        edge &e = Graph[v][j];
        if (e.cap == 0) continue;
        int u = e.to;
        h[u] = min(h[u], h[v] + e.cost);
      }
    }
    while (f > 0) {
      priority_queue<P, vector<P>, greater<P>> pq;
      fill((d).begin(), (d).end(), 214748360012345678);
      d[s] = 0;
      pq.push(make_pair((0), (s)));
      while (!pq.empty()) {
        auto p = pq.top();
        pq.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = (int)(0); i < (int)(Graph[v].size()); i++) {
          edge &e = Graph[v][i];
          if (e.cap > 0 && d[e.to] > d[v] + e.cost + h[v] - h[e.to]) {
            d[e.to] = d[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
            pq.push(make_pair((d[e.to]), (e.to)));
          }
        }
      }
      if (d[t] == 214748360012345678) return -1;
      for (int i = (int)(0); i < (int)(Graph.size()); i++) h[i] += d[i];
      T nf = f;
      for (int v = t; v != s; v = prevv[v]) {
        nf = min(nf, Graph[prevv[v]][preve[v]].cap);
      }
      f -= nf;
      res += nf * h[t];
      for (int v = t; v != s; v = prevv[v]) {
        edge &e = Graph[prevv[v]][preve[v]];
        e.cap -= nf;
        Graph[v][e.rev].cap += nf;
      }
    }
    return res;
  }
  void add_edge(int from, int to, T cap, T cost) {
    Graph[from].push_back((((edge){to, cap, cost, (int)Graph[to].size()})));
    Graph[to].push_back(
        (((edge){from, 0, -cost, (int)Graph[from].size() - 1})));
  }
};
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> q(n), c(n);
  for (int i = (int)(0); i < (int)(n); i++) scanf("%d", &q[i]);
  for (int i = (int)(0); i < (int)(n); i++) scanf("%d", &c[i]);
  for (int i = (int)(0); i < (int)(n); i++) q[i]--;
  MinCostFlow<long long> flow(2 * n + 2);
  const long long XXX = 1e+8;
  for (int i = (int)(0); i < (int)(n); i++)
    flow.add_edge(2 * n, 2 * i, 1, c[q[i]]);
  for (int i = (int)(0); i < (int)(n); i++)
    flow.add_edge(2 * i, 2 * i + 1, 1, -XXX);
  for (int i = (int)(0); i < (int)(n); i++)
    flow.add_edge(2 * i + 1, 2 * n + 1, 1, 0);
  for (int i = (int)(0); i < (int)(n); i++)
    for (int j = (int)(i + 1); j < (int)(n); j++) {
      if (q[i] == q[j])
        flow.add_edge(2 * i + 1, 2 * j, 1, 0);
      else
        flow.add_edge(2 * i + 1, 2 * j, 1, c[q[j]]);
    }
  flow.add_edge(2 * n, 2 * n + 1, k - 1, 0);
  long long res = flow.min_cost_flow(2 * n, 2 * n + 1, k);
  cout << res + XXX * n << endl;
  return 0;
}
