package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
type Edge struct {
to int
cap int
cost int64
rev int
}
var graph [][]Edge
func addEdge(from, to, cap int, cost int64) {
graph[from] = append(graph[from], Edge{to, cap, cost, len(graph[to])})
graph[to] = append(graph[to], Edge{from, 0, -cost, len(graph[from]) - 1})
}
func spfa(S, T int) (bool, int64, []int, []int) {
dist := make([]int64, T+1)
for i := range dist {
dist[i] = 1e18
}
inq := make([]bool, T+1)
parentEdge := make([]int, T+1)
parentVertex := make([]int, T+1)
dist[S] = 0
q := []int{S}
inq[S] = true
for len(q) > 0 {
u := q[0]
q = q[1:]
inq[u] = false
for i, e := range graph[u] {
if e.cap > 0 && dist[e.to] > dist[u]+e.cost {
dist[e.to] = dist[u] + e.cost
parentVertex[e.to] = u
parentEdge[e.to] = i
if !inq[e.to] {
inq[e.to] = true
q = append(q, e.to)
}
}
}
}
return dist[T] != 1e18, dist[T], parentVertex, parentEdge
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
if !scanner.Scan() {
return
}
n, _ := strconv.Atoi(scanner.Text())
scanner.Scan()
k, _ := strconv.Atoi(scanner.Text())
a := make([]int, n+1)
for i := 1; i <= n; i++ {
scanner.Scan()
a[i], _ = strconv.Atoi(scanner.Text())
}
c := make([]int, n+1)
for i := 1; i <= n; i++ {
scanner.Scan()
c[i], _ = strconv.Atoi(scanner.Text())
}
graph = make([][]Edge, n+1)
staticSave := int64(0)
baseCost := int64(0)
pos := make(map[int]int)
for i := 1; i <= n; i++ {
book := a[i]
baseCost += int64(c[book])
if p, ok := pos[book]; ok {
if p == i-1 {
staticSave += int64(c[book])
} else {
addEdge(p+1, i, 1, int64(-c[book]))
}
}
pos[book] = i
}
S := 0
T := n
if k-1 > 0 {
addEdge(S, 1, k-1, 0)
for i := 1; i < n; i++ {
addEdge(i, i+1, k-1, 0)
}
minCost := int64(0)
for {
found, d, pV, pE := spfa(S, T)
if !found || d >= 0 {
break
}
push := int(1e9)
curr := T
for curr != S {
p := pV[curr]
idx := pE[curr]
if graph[p][idx].cap < push {
push = graph[p][idx].cap
}
curr = p
}
curr = T
for curr != S {
p := pV[curr]
idx := pE[curr]
revIdx := graph[p][idx].rev
graph[p][idx].cap -= push
graph[curr][revIdx].cap += push
curr = p
}
minCost += int64(d) * int64(push)
}
fmt.Println(baseCost - staticSave + minCost)
} else {
fmt.Println(baseCost - staticSave)
}
}