The good times at Heidi's library are over. Marmots finally got their internet connections and stopped coming to the library altogether. Not only that, but the bookstore has begun charging extortionate prices for some books. Namely, whereas in the previous versions each book could be bought for 1 CHF, now the price of book *i* is *c*<sub class="lower-index">*i*</sub> CHF.
The first line of input will contain two integers *n* and *k* (<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1fcb9de4700dce1c302f6e6f7941707f942bc3ff.png" style="max-width: 100.0%;max-height: 100.0%;"/>). The second line will contain *n* integers *a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*n*</sub> (1 ≤ *a*<sub class="lower-index">*i*</sub> ≤ *n*) – the sequence of book requests. The third line contains *n* integers *c*<sub class="lower-index">1</sub>, *c*<sub class="lower-index">2</sub>, ..., *c*<sub class="lower-index">*n*</sub> (0 ≤ *c*<sub class="lower-index">*i*</sub> ≤ 10<sup class="upper-index">6</sup>) – the costs of the books.
The first three sample cases are repeated, but the fourth one is new.

In the fourth test case, when buying book 3, Heidi should discard either book 1 or 2. Even though book 2 will be requested later than book 1, she should keep it, because it is so expensive to buy again.
4 80
1 2 2 1
1 1 1 1
2
4 1
1 2 2 1
1 1 1 1
3
4 2
1 2 3 1
1 1 1 1
3
7 2
1 2 3 1 1 1 2
1 10 1 0 0 0 0
13
11 1
1 2 3 5 1 10 10 1 1 3 5
1 1 2 3 4 5 6 7 8 9 10
25
5 2
1 2 3 1 2
1 1 1 1 1
4
4 2
1 2 3 2
1 1 1 1
3
80 4
2 6 10 4 7 8 2 8 9 10 6 2 5 3 2 10 5 9 1 3 4 8 6 1 9 3 9 4 7 7 4 6 4 8 1 2 5 9 7 7 8 7 10 8 3 3 1 6 6 4 5 4 6 3 10 8 1 9 5 2 7 9 1 10 2 9 4 7 10 2 2 8 5 10 4 3 10 7 1 8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
38
80 4
1 12 2 17 10 17 2 14 12 8 9 7 6 6 20 3 13 5 6 13 10 5 19 18 11 20 4 15 17 15 13 4 17 7 2 11 10 17 6 9 12 2 15 17 17 11 11 4 7 14 13 4 18 3 14 9 12 16 1 5 13 18 19 1 3 6 8 18 4 16 4 17 7 11 10 15 13 12 13 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
53
80 4
33 15 1 9 12 4 38 13 36 11 25 27 37 4 30 28 26 1 20 34 5 40 21 40 19 27 2 7 14 23 13 34 25 40 30 11 16 36 39 27 5 12 40 6 28 22 35 4 8 13 32 18 19 15 27 7 18 22 9 39 7 8 39 20 6 39 35 16 32 18 17 35 35 2 15 11 39 38 9 7
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
61
80 4
75 30 64 39 71 25 14 19 45 73 79 60 2 56 58 61 27 17 55 5 52 69 18 52 44 55 14 65 39 32 31 54 43 11 47 66 67 26 10 54 63 31 1 9 18 68 59 68 19 68 32 55 76 15 76 34 78 26 50 13 75 77 38 80 75 51 34 59 24 13 66 80 17 64 40 36 43 18 72 66
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
66
80 8
4 5 15 1 5 20 7 19 17 20 3 15 15 9 18 8 4 17 2 16 3 13 14 4 14 18 18 8 15 1 7 12 20 12 5 2 15 10 3 9 11 15 10 17 13 5 3 12 3 16 4 19 1 11 7 11 7 13 15 5 4 2 1 2 14 7 4 20 8 17 9 3 10 10 16 20 13 10 1 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
36
80 8
32 24 35 36 6 38 3 2 5 39 34 15 25 27 20 29 32 22 27 33 31 40 36 6 34 37 24 17 15 21 12 30 28 21 33 33 21 21 36 38 24 17 23 27 32 24 22 32 29 24 20 30 9 24 3 21 13 19 7 28 10 36 20 21 1 29 11 30 36 38 2 33 34 21 17 12 7 32 37 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
48
80 8
50 39 1 74 58 20 75 8 71 70 49 24 6 79 33 31 57 38 14 3 69 5 1 18 19 41 52 59 33 54 69 2 46 33 26 16 16 35 23 50 2 77 9 53 62 30 30 64 55 79 43 11 51 72 69 80 73 24 56 1 7 80 19 1 14 8 26 57 68 74 51 5 1 66 42 53 66 36 61 45
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
61
80 12
10 12 13 7 18 13 18 14 9 4 10 8 8 10 13 4 4 17 13 4 8 15 19 6 9 15 13 8 5 18 2 11 15 11 13 19 11 16 14 2 3 15 11 1 3 17 4 14 17 6 3 16 1 19 14 13 15 7 12 3 20 10 19 12 13 1 5 11 4 20 6 20 20 19 4 19 18 2 11 6
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
25
80 12
15 15 4 14 24 36 14 9 10 12 1 33 38 28 23 21 32 21 39 17 8 38 26 4 34 38 6 40 38 2 31 38 24 29 1 23 17 11 15 35 20 13 24 6 2 16 4 30 22 37 19 15 10 27 6 31 9 33 28 18 26 28 15 39 3 15 4 8 28 22 39 30 40 26 4 36 8 32 31 23
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
45
80 12
80 62 51 44 19 41 6 14 43 74 80 2 3 8 12 63 57 78 26 67 31 43 63 48 59 18 66 42 39 75 64 42 17 16 74 78 44 49 18 22 46 48 17 41 64 70 52 62 33 60 42 68 3 51 56 42 61 77 21 71 39 33 78 35 49 59 35 28 28 57 8 3 63 47 69 76 67 68 55 65
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
57
80 16
6 8 14 1 34 39 27 38 19 40 34 37 11 10 6 6 23 2 39 40 25 29 1 10 9 32 12 10 15 16 21 18 19 26 36 5 22 20 13 31 14 18 23 11 38 10 31 18 11 16 30 10 16 3 23 29 3 30 26 6 14 8 36 40 31 4 29 38 32 34 40 36 23 4 22 29 7 2 3 10
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
37
80 16
39 71 4 79 70 20 3 4 68 70 33 30 56 31 66 48 24 66 18 66 16 11 62 14 18 52 40 52 1 49 55 70 52 37 37 28 58 42 15 18 80 78 57 37 28 32 39 10 22 55 69 39 57 43 48 56 55 74 75 59 2 37 59 20 20 80 27 10 24 69 41 40 46 66 7 13 43 70 27 12
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
48
80 40
26 58 50 37 79 48 42 79 69 35 70 63 34 76 8 9 64 38 68 1 78 33 18 18 31 64 64 72 40 11 63 53 22 46 1 31 22 24 66 64 44 29 12 5 13 37 16 80 52 10 20 7 16 46 80 34 21 19 64 80 7 1 53 51 18 67 36 31 4 74 37 63 33 41 37 80 79 55 4 37
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
51
80 60
29 37 46 6 14 8 17 43 5 2 74 34 72 70 29 22 4 71 45 37 57 15 36 66 73 58 33 68 45 40 53 22 16 30 37 21 33 69 11 21 54 10 69 77 36 77 52 6 24 66 63 31 49 19 77 60 3 11 62 67 21 76 67 13 68 74 66 75 78 15 67 63 8 5 69 30 25 79 42 69
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
53
80 80
48 49 57 56 13 64 8 8 68 66 79 68 30 80 17 34 24 57 38 40 3 78 6 67 51 20 18 17 33 69 11 70 25 79 56 11 13 51 69 26 48 23 15 36 59 21 72 27 11 42 41 40 17 55 27 22 65 52 61 53 51 24 34 24 54 2 16 23 40 67 65 46 15 49 37 12 51 70 47 37
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
52
80 4
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
23
80 8
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
17
80 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
20
80 32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
34
80 4
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
7 101 0 641482 72595 818027 16 6639 409228 9213 76774 5822 98534 4 39 62347 0 941653 303 461146 354562 0 0 8735 11 592322 426170 379488 9 36378 637098 9 690665 4 40 52 8 29 506 55 37684 81 6144 49 0 0 0 69 52 6 13907 0 414367 606348 755654 53 9 769895 52 26487 379 14806 7 380 2 8231 79263 42 0 3731 8856 84 0 8 460 0 349 6 168676 6
714283
import sys
from heapq import heappop, heappush
 
class Edge:
    def __init__(self, u, v, cap, cost, rev):
        self.u = u
        self.v = v
        self.cap = cap
        self.flow = 0
        self.cost = cost
        self.rev = rev

def add_edge(adj, u, v, capv, costv):
    adj[u].append(Edge(u, v, capv, costv, len(adj[v])))
    adj[v].append(Edge(v, u, 0, -costv, len(adj[u])-1))

def bellman_ford(adj):
    potential = [0] * len(adj)

    for _ in range(len(adj)):
        for u in range(len(adj)):
            for e in adj[u]:
                reduced_cost = potential[e.u] + e.cost - potential[e.v]
                if e.cap > 0 and reduced_cost < 0:
                    potential[e.v] += reduced_cost
    
    return potential

def dijkstra(adj, potential, s, t):
    oo = float('inf')
    dist, pi = [+oo] * len(adj), [None] * len(adj)

    dist[s] = 0
    heap = [(0, s)]

    while heap:
        du, u = heappop(heap)

        if dist[u] < du: continue
        if u == t: break

        for e in adj[u]:
            reduced_cost = potential[e.u] + e.cost - potential[e.v]
            if e.cap - e.flow > 0 and dist[e.v] > dist[e.u] + reduced_cost:
                dist[e.v] = dist[e.u] + reduced_cost
                heappush(heap, (dist[e.v], e.v))
                pi[e.v] = e
    
    return dist, pi

def min_cost_max_flow(adj, s, t, flow_limit = float('inf')):
    min_cost, max_flow = 0, 0
    oo = float('inf')

    potential = bellman_ford(adj)

    while True:
        dist, pi = dijkstra(adj, potential, s, t)
        
        if dist[t] == +oo:
            break
            
        for u in range(len(adj)):
            if dist[u] < dist[t]:
                potential[u] += dist[u] - dist[t]
        
        limit, v = +oo, t
        while v:
            e = pi[v]
            limit = min(limit, e.cap - e.flow)
            v = e.u
        
        max_limit_reached = max_flow + limit >= flow_limit
        limit = max_limit_reached and flow_limit - max_flow or limit

        if max_limit_reached:
            min_cost += limit * (potential[t] - potential[s])
            max_flow += limit

            return min_cost, max_flow
        
        v = t
        while v:
            e = pi[v]
            e.flow += limit
            adj[v][e.rev].flow -= limit
            v = e.u
        
        min_cost += limit * (potential[t] - potential[s])
        max_flow += limit
    
    return min_cost, max_flow
		
n, k = map(int, sys.stdin.readline().split())
# n, k = map(int, "7 2".split())
# n, k = map(int, "4 80".split())
pre = [None] * (n + 1)
s, t = 0, 2 * n + 1
adj = [[] for  _ in range(t + 1)]
a = [None] + list(map(int, sys.stdin.readline().split()))
# a = [None] + list(map(int, "1 2 3 1 1 1 2".split()))
# a = [None] + list(map(int, "1 2 2 1".split()))
for i in range(1, n+1):
    add_edge(adj, i, i+n, 1, 0)
    add_edge(adj, i+n, t, 1, 0)

c = [None] + list(map(int, sys.stdin.readline().split()))
# c = [None] + list(map(int, "1 10 1 0 0 0 0".split()))
# c = [None] + list(map(int, "1 1 1 1".split()))

for i in range(1, n+1):
    add_edge(adj, s, i, 1, c[a[i]])

    if pre[a[i]]:
        add_edge(adj, i-1, pre[a[i]] + n, 1, -c[a[i]])
        # add_edge(adj, i, pre[a[i]] + n, 1, -c[a[i]])
    pre[a[i]] = i

for i in range(1, n):
    add_edge(adj, i, i+1, k-1, 0)

min_cost, _ = min_cost_max_flow(adj, s, t)
print(min_cost)