#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N,K;
cin>>N>>K;
vector<int>v(N+1),w(N+1);
for(int i=1;i<=N;i++){
cin>>v[i];
}
for(int i=1;i<=N;i++){
cin>>w[i];
}
vector<vector<int>>g(N+1);
vector<int>s;
for(int i=N;i>=1;i--){
while(!s.empty()&&v[s.back()]<=v[i]){
s.pop_back();
}
g[s.empty()?0:s.back()].push_back(i);
s.push_back(i);
}
if(*min_element(w.begin(),w.end())>=0){
auto dfs=[&](auto self,int i)->vector<int>{
vector<int>s;
for(int j:g[i]){
auto z=self(self,j);
if(s.size()<z.size()){
swap(s,z);
}
s.insert(s.end(),z.begin(),z.end());
}
sort(s.begin(),s.end());
if(s.empty()){
s.push_back(w[i]);
}
else{
s.back()+=w[i];
}
return s;
};
auto ans=dfs(dfs,0);
cout<<accumulate(max(ans.begin(),ans.end()-K),ans.end(),0ll)<<'\n';
return 0;
}
auto solveMin=[&](int y)->pii{
auto dfs=[&](auto self,int i)->pair<pii,pii>{
pii a{},b{};
for(int j:g[i]){
auto [p,q]=self(self,j);
a.first+=p.first;
a.second+=p.second;
if(q.first-p.first>b.first){
b.first=q.first-p.first;
b.second=q.second-p.second;
}
}
b.first+=a.first+w[i];
b.second+=a.second;
if(b.first-y>a.first){
a.first=b.first-y;
a.second=b.second+1;
}
return{a,b};
};
return dfs(dfs,0).first;
};
auto solveMax=[&](int y)->pii{
auto dfs=[&](auto self,int i)->pair<pii,pii>{
pii a{},b{};
for(int j:g[i]){
auto [p,q]=self(self,j);
a.first+=p.first;
a.second+=p.second;
if(q.first-p.first>b.first||q.first-p.first==b.first&&q.second-p.second>b.second){
b.first=q.first-p.first;
b.second=q.second-p.second;
}
}
b.first+=a.first+w[i];
b.second+=a.second;
if(b.first-y>=a.first){
a.first=b.first-y;
a.second=b.second+1;
}
return{a,b};
};
return dfs(dfs,0).first;
};
int l=0,h=1e18;
while(l<h){
int m=(l+h)/2;
auto[x,c]=solveMin(m);
if(c<=K){
h=m;
}
else{
l=m+1;
}
}
auto[x,c]=solveMax(h);
sort(w.begin()+1,w.end());
if(c<K&&w[N-K+1]<0){
int ans=0;
for(int i=N;i>N-K;i--){
ans+=w[i];
}
cout<<ans<<'\n';
return 0;
}
cout<<x+min(c,K)*h<<'\n';
return 0;
}