#define PROBLEM "https://yukicoder.me/problems/5061" #include using namespace std; #define call_from_test template vector read(size_t n){ vector ts(n); for(size_t i=0;i>ts[i]; return ts; } template vector fusion(vector bs,Ts... ts){ auto append=[&](auto vs){for(auto v:vs) bs.emplace_back(v);}; initializer_list{(void(append(ts)),0)...}; return bs; } template V compress(V vs){ sort(vs.begin(),vs.end()); vs.erase(unique(vs.begin(),vs.end()),vs.end()); return vs; } template map dict(const vector &vs){ map res; for(int i=0;i<(int)vs.size();i++) res[vs[i]]=i; return res; } map dict(const string &s){ return dict(vector(s.begin(),s.end())); } template vector compressed(vector vs){ auto dc=dict(compress(vs)); for(auto &v:vs) v=dc[v]; return vs; } // https://maspypy.com/slope-trick-1-%e8%a7%a3%e8%aa%ac%e7%b7%a8 template struct Slope{ template typename Comp_> struct PQ{ template using Comp = Comp_; inline static constexpr Comp comp{}; using P = pair; priority_queue, Comp

> pq; bool empty()const{return pq.empty();} T offset; PQ():offset(0){} bool compare(T a){return comp(a,pq.top().first+offset);} // f_{new}(x) = f_{old}(x - diff) void shift(T diff){offset+=diff;} void push(T pos,T num){ if(num!=T(0)) pq.emplace(pos-offset,num); } P pop(){ auto[pos,num]=pq.top();pq.pop(); return P(pos+offset,num); } }; PQ L; PQ R; T entire; Slope():entire(0){} inline T relu(T x){return max(0,x);} template void fix(T a,T cnt,From &from,To &to){ T use(0); while(use> vp; vp.reserve(pq.pq.size()); while(!pq.empty()) vp.emplace_back(pq.pop()); return vp; }; for(auto[pos,num]:vectorize(L)) res+=relu(pos-x)*num; for(auto[pos,num]:vectorize(R)) res+=relu(x-pos)*num; return res; } }; #undef call_from_test #ifdef SANITIZE #define IGNORE #endif signed main(){ cin.tie(0); ios::sync_with_stdio(0); int m,n; cin>>m>>n; auto as=read(m); auto bs=read(n); auto cs=compress(fusion(as,bs)); auto dc=dict(cs); for(int &a:as) a=dc[a]; for(int &b:bs) b=dc[b]; const int sz = cs.size(); vector num(sz,0); for(int a:as) num[a]--; using ll = long long; for(int k=1;k<=m;k++){ for(int b:bs) num[b]++; int pos=0; Slope S; S.add_a_minus_x(pos,1e9); for(int i=0;i