#include<iostream>
#include<unordered_map>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1000200;
unordered_map<int, vector<int>>behind;
struct Edge {
long long a;
long long b;
long long c;
};
bool cmp(Edge x, Edge y)
{
return x.a < y.a;
}
long long v[maxn], w[maxn];
vector<long long >nums;
vector<Edge>edges;
unordered_map<long long, long long> st;
unordered_map<long long,long long> dist, backup;
unordered_map<long long, long long>backst;
void bellman_ford(int m, int k)
{
dist[0] = 0;
for (int i = 0; i < k; i++)
{
backup = dist;
backst = st;
for (int j = 0; j < m; j++)
{
long long a = edges[j].a, b = edges[j].b, c = edges[j].c;
if (dist[b] >backup[a] + c)
{
dist[b] = backup[a] + c;
st[b] = backst[a] + 1;
}
}
}
}
int main()
{
int n, k;
cin >> n >> k;
unordered_map<int, int>mp;
for (int i = 1; i <= n; i++)
{
cin >> v[i];
if (mp.count(v[i]) == 0)
{
nums.push_back(v[i]);
mp[v[i]]++;
}
}
for (int i = 1; i <= n; i++)
{
cin >> w[i];
}
sort(nums.begin(), nums.end());
if (nums.size() <= k)
{
cout << -1 << endl;
return 0;
}
unordered_map<int, int>mpp;
for (int i = 1; i <= n; i++)
{
if (mpp.count(v[i]) != 0)
{
continue;
}
else
{
mpp[v[i]]++;
}
unordered_map<int, int>mp1;
for (int j = i + 1; j <= n; j++)
{
if (mp1.count(v[j]) == 0 && v[j] != v[i])
{
behind[v[i]].push_back(v[j]);
mp1[v[j]]++;
}
}
}
for (int i = 0; i < nums.size(); i++)
{
for (int j = i + 1; j < nums.size(); j++)
{
auto it1 = std::find(behind[nums[i]].begin(), behind[nums[i]].end(), nums[j]);
auto it2 = std::find(behind[nums[j]].begin(), behind[nums[j]].end(), nums[i]);
if (it1 != behind[nums[i]].end() && it2 == behind[nums[j]].end())
{
edges.push_back({ nums[i],nums[j],-w[nums[j]] });
}
}
}
for (int i = 0; i < nums.size(); i++)
{
edges.push_back({ 0,nums[i],-w[nums[i]] });
}
sort(edges.begin(), edges.end(),cmp);
bellman_ford(edges.size(), k);
long long res = 1e11;
for (int i = 0; i < nums.size(); i++)
{
if (st[nums[i]] <k)
{
continue;
}
else
{
res = min(res, (long long)dist[nums[i]]);
}
}
if (res >= 1e11)
{
cout << -1 << endl;
}
else
{
cout << -res << endl;
}
return 0;
}