提交时间:2024-02-21 15:43:26
运行 ID: 26663
#include<bits/stdc++.h> #define int long long #define endl "\n" using namespace std; int a[100200],b[100300]; int n,k; int res,c; inline int calc(int lim){ priority_queue<int>q; //1: min(unused a_j) and b_i //2: min(used -b_j) and b_j priority_queue<int,vector<int>,greater<int>>QA,QB; QA.push(1e10); QB.push(1e10); res=0; int cnt=0; for(int i=1;i<=n;i++){ QA.push(a[i]-lim); if(min(QA.top(),QB.top())<0){ res+=b[i]; if(QA.top()<QB.top()) cnt++,res+=QA.top(),QA.pop(); else res+=QB.top(),QB.pop(); QB.push(-b[i]); } } return cnt; } signed main(){ //freopen("world.in","r",stdin); //freopen("reworldsist.out","w",stdout); ios::sync_with_stdio(0); cin>>n>>k; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)cin>>b[i]; int l=0,r=2e9; while(l<r){ int mid=l+r+1>>1; if(calc(mid)>k)r=mid-1; else l=mid; } calc(l); cout<<res+k*l<<endl; cout.flush(); return 0; } /* 21111 22331 12123 11333 21112 12132 */