#include # define endl '\n' # define clr(x,a) memset(x,a,sizeof(x)) # define vi vector # define all(a) a.begin(), a.end() # define deb(x) cout<<#x<<"=="<>t; while(t--) # define rev(s) reverse(s.begin(),s.end()) # define linija cout<<"___________\n"; # define sza(x) (int)x.size() using namespace std; typedef long long ll; const int mxN=100005, mxM=100005, LOG=18, koren=317, inf=1e8; const ll mod=4294967295; template T nzd(T a, T b){if(b==0) return a;else return nzd(b, a%b);} template T nzs(T a, T b){return(a*(b/nzd(a,b)));} template T stepenuj(T e, T n){T x=1,p=e;while(n){if(n&1)x=(x*p)&mod;p=(p*p)&mod;n>>=1;}return x;} ll years[mxN], planets[mxN]; ll res, godine[mxN]; ll n , m, sr; ll stek[mxN], kkr[mxN]; int main(){ freopen("planets.in", "r", stdin); freopen("planets.out", "w", stdout); sc("%d %d", &n, &m); for(int i = 1; i <= n; i++) sc("%lld", &planets[i]); for(int i = 1; i <= m; i++) sc("%lld", &years[i]); sort(planets+1, planets+1+n); sort(years+1, years+1+m); sr = 0; ll idp, idy; idp = idy = 1; while(idp <= n and idy <= m){ if(planets[idp] <= years[idy]){ //godina veca stek[++sr] = planets[idp]; idp++; } else{ kkr[idy] = (years[idy]-stek[sr])*(years[idy]-stek[sr]); sr--; idy++; } } // cout << "Stek : "; // for(int i = 1; i <= sr; i++) // cout << stek[i] << " "; // cout << endl; int poc = 1; for(int i = m; i >= 1; i--){ if(poc > sr) break; if(stek[poc] > years[i]) break; // cout << years[i] << " - " << stek[poc] << endl; if((years[i]-stek[poc])*(years[i]-stek[poc]) >= kkr[i]){ kkr[i] = (years[i]-stek[poc])*(years[i]-stek[poc]); poc++; } } for(int i = 1; i <= m; i++) res += kkr[i]; cout << res << endl; return 0; }