#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int MAXN = 100'001;
ll a[MAXN], b[MAXN], nxt[MAXN];
pair<ll, int> dp[MAXN][2]; // any - with team
vector<int> roots;
vector<int> g[MAXN];
void clear() {
for (int i = 0; i < MAXN; i++) {
dp[i][0] = {0, 0};
for (int j : {0, 1}) {
dp[i][j] = {0, 0};
}
}
}
void dfs(int u, ll lambda) {
for (int v : g[u]) {
dfs(v, lambda);
dp[u][0].first += dp[v][0].first;
dp[u][0].second += dp[v][0].second;
}
dp[u][1] = {dp[u][0].first + b[u] - lambda, dp[u][0].second - 1};
for (int v : g[u]) {
pair<ll, int> corrected = {dp[u][0].first - dp[v][0].first, dp[u][0].second - dp[v][0].second};
pair<ll, int> opt = {corrected.first + dp[v][1].first + b[u], corrected.second + dp[v][1].second};
dp[u][1] = max(dp[u][1], opt);
}
dp[u][0] = max(dp[u][0], dp[u][1]);
}
pair<ll, int> calc(ll lambda) {
pair<ll, int> res = {0, 0};
clear();
for (int root : roots) {
dfs(root, lambda);
auto best = max(dp[root][0], dp[root][1]);
res.first += best.first;
res.second += best.second;
}
res.second *= -1;
return res;
}
void solve() {
int n, k; cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
vector<ll> vals(b+1, b+n+1);
sort(vals.begin(), vals.end());
int cnt = count_if(vals.begin(), vals.end(), [](ll x){return x >= 0;});
if (cnt <= k) {
ll ans = 0;
for (int i = n-1; i >= n-k; i--) {
ans += vals[i];
}
cout << ans << "\n";
return;
}
stack<int> st;
int maxe = 0;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty()) {
g[st.top()].emplace_back(i);
}
st.push(i);
if (maxe <= a[i]) {
roots.emplace_back(i);
maxe = a[i];
}
}
ll lo = 0, hi = 1e16;
while (lo < hi) {
ll mid = (lo + hi) / 2;
if (calc(mid).second > k) {
lo = mid + 1;
} else {
hi = mid;
}
}
auto ans = calc(lo);
ll val = ans.first + ans.second * lo;
if (ans.second < k) {
int need = k - ans.second;
val += need * lo;
}
cout << val << "\n";
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}