#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } // https://yukicoder.me/submissions/644671 // https://ei1333.github.io/library/structure/others/slope-queue.cpp // https://maspypy.com/slope-trick-1-%e8%a7%a3%e8%aa%ac%e7%b7%a8 // ex) // f(x) <- min_{y<=x+k} f(y) // 1. f.shift(k) // 2. f.apply_cumulative_min() template struct Slope{ // private: using P=pair; priority_queue,less

> L; priority_queue,greater

> R; T add_L,add_R,min_f; void push_R(T a,T cnt){if(cnt>0) R.emplace(a-add_R,cnt);} void push_L(T a,T cnt){if(cnt>0) L.emplace(a-add_L,cnt);} P top_R(){return P(R.top().first+add_R,R.top().second);} P top_L(){return P(L.top().first+add_L,L.top().second);} P pop_R(){ P ret=top_R(); R.pop(); return ret; } P pop_L(){ P ret=top_L(); L.pop(); return ret; } public: Slope():add_L(0),add_R(0),min_f(0){} void add_all(T a){min_f+=a;} void add_x_minus_a(T a,T cnt=1){ while(cnt>0 and !L.empty() and a0 and !R.empty() and a>top_R().first){ auto [pos,num]=pop_R(); T mv=std::min(cnt,num); push_L(pos,mv); push_R(pos,num-mv); push_R(a,mv); min_f+=(a-pos)*mv; cnt-=mv; } push_L(a,cnt); } void add_abs(T a,T cnt=1){ add_a_minus_x(a,cnt); add_x_minus_a(a,cnt); } void clear_right(){while(!R.empty()) R.pop();} void clear_left(){ while(!L.empty()) L.pop();} // f <- min{x-b<=y<=x-a} f(y) void shift(T a,T b){ assert(a<=b); add_L+=a,add_R+=b; } // f(x) <- f(x-a) void shift(T a){shift(a,a);} // get value O(N) T operator()(T x){ T ret=min_f; auto vec=[](auto pq,T shift){ vector

v; while(!pq.empty()){ v.emplace_back(pq.top().first+shift,pq.top().second); pq.pop(); } return v; }; for(auto [pos,num]:vec(L,add_L)) ret+=max(pos-x,T(0))*num; for(auto [pos,num]:vec(R,add_R)) ret+=max(x-pos,T(0))*num; return ret; } T min(){return min_f;} pair argmin(){return make_pair(P(top_L().first,top_R().first));} }; signed main(){ int m,n;cin>>m>>n; vector a(m),b(n); cin>>a>>b; struct E{ ll val; bool a; E(ll val,bool a):val(val),a(a){} }; vector v; rep(i,m) v.emplace_back(a[i],true); rep(i,n) v.emplace_back(b[i],false); sort(ALL(v),[](E l,E r){return l.val f; f.add_x_minus_a(0,1000000000); f.add_a_minus_x(0,1000000000); ll pre=-100000; for(int i=0;i<(int)v.size();i++){ ll nxt=(i+1==(int)v.size()?INF:v[i+1].val); ll d=nxt-v[i].val; bool is_a=v[i].a; if(is_a){ f.shift(1); f.clear_right(); f.add_x_minus_a(0,d); f.add_a_minus_x(0,d); }else{ f.shift(-k); f.clear_right(); f.add_x_minus_a(0,d); f.add_a_minus_x(0,d); } } cout<