/*Code by Leo2011*/
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define FOR(i, l, r) for (ll(i) = (l); (i) <= (r); ++(i))
#define log printf
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
using namespace std;
typedef long long ll;
const ll N = 510;
ll m, n, a[N], b[N], l[N], r[N], dp[N][N], ans = -1;
template <typename T>
inline T read() {
T sum = 0, fl = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0';
return sum * fl;
}
template <typename T>
inline void write(T x) {
static T sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
}
ll dfs(ll k, ll c) {
if (c == m) return 0LL;
if (dp[k][c] != -1) return dp[k][c];
ll p = -INF;
FOR(i, a[k] + 1LL, n) if(l[i] > r[a[k]]) p = max(p, dfs(r[i], c + 1LL) + b[i]);
return dp[k][c] = p;
}
int main() {
memset(dp, -1LL, sizeof(dp));
n = read<ll>(), m = read<ll>();
FOR(i, 1LL, n)
a[i] = read<ll>();
FOR(i, 1LL, n)
b[i] = read<ll>();
FOR (i, 1LL, n) {
if (!l[a[i]]) l[a[i]] = i;
r[a[i]] = max(r[a[i]], i);
}
ans = dfs(0, 0);
if (ans < 0) {write<ll>(-1);putchar('\n');}
else write<ll>(ans);
return 0;
}
WA 在#10、#12、#19,都是输出-1的点,咋整啊