#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <assert.h>
using namespace std;
#define sz(a) (int)(a.size())
#define len(a) (int)(a.length())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
inline long long ab(long long x) {return x<0?-x:x;}
long long f(long long k,vector<int>&x)
{
long long steps=0;
for(int i=0;i<sz(x);++i)
steps+=ab(x[i]-k);
return steps;
}
class FoxConnection3 {
public:
long long minimalSteps(vector <int> x, vector <int> y)
{
//Y
long long lo=-20000000000LL,hi=20000000000LL,res=-1;
while(lo<=hi)
{
long long mid=(lo+hi)>>1;
if(f(mid,y)<=f(mid+1,y)) res=mid,hi=mid-1;
else lo=mid+1;
}
//X
lo=-20000000000LL,hi=20000000000LL;
long long res2=-1;
while(lo<=hi)
{
long long mid=(lo+hi)>>1;
if(f(mid,x)<=f(mid+1,x)) res2=mid,hi=mid-1;
else lo=mid+1;
}
return f(res,y)+f(res2,x);
}
};
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, vector <int> p0, vector <int> p1, bool hasAnswer, long long p2) {
cout << "Test " << testNum << ": [" << "{";
for (int i = 0; int(p0.size()) > i; ++i) {
if (i > 0) {
cout << ",";
}
cout << p0[i];
}
cout << "}" << "," << "{";
for (int i = 0; int(p1.size()) > i; ++i) {
if (i > 0) {
cout << ",";
}
cout << p1[i];
}
cout << "}";
cout << "]" << endl;
FoxConnection3 *obj;
long long answer;
obj = new FoxConnection3();
clock_t startTime = clock();
answer = obj->minimalSteps(p0, p1);
clock_t endTime = clock();
delete obj;
bool res;
res = true;
cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
if (hasAnswer) {
cout << "Desired answer:" << endl;
cout << "\t" << p2 << endl;
}
cout << "Your answer:" << endl;
cout << "\t" << answer << endl;
if (hasAnswer) {
res = answer == p2;
}
if (!res) {
cout << "DOESN'T MATCH!!!!" << endl;
} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
cout << "FAIL the timeout" << endl;
res = false;
} else if (hasAnswer) {
cout << "Match :-)" << endl;
} else {
cout << "OK, but is it right?" << endl;
}
cout << "" << endl;
return res;
}
int main() {
bool all_right;
all_right = true;
vector <int> p0;
vector <int> p1;
long long p2;
{
// ----- test 0 -----
int t0[] = {0,0,1,-2};
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
int t1[] = {1,-1,0,0};
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
p2 = 2ll;
all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
// ------------------
}
{
// ----- test 1 -----
int t0[] = {0,0,0,0,0,0};
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
int t1[] = {1,2,3,4,5,6};
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
p2 = 0ll;
all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
// ------------------
}
{
// ----- test 2 -----
int t0[] = {-123456789,-58585858,-47474747,123,456,789012345};
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
int t1[] = {0,0,0,0,0,0};
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
p2 = 1018530309ll;
all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
// ------------------
}
{
// ----- test 3 -----
int t0[] = {1,7,3,5,2};
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
int t1[] = {2,7,5,3,7};
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
p2 = 12ll;
all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
// ------------------
}
{
// ----- test 4 -----
int t0[] = {-3,0,1,-2,3,2};
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
int t1[] = {2,-3,0,1,-1,-1};
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
p2 = 10ll;
all_right = KawigiEdit_RunTest(4, p0, p1, true, p2) && all_right;
// ------------------
}
{
// ----- test 5 -----
int t0[] = {-96277832,507856257,-86306299,-806700273,-775932643,-273209838};
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
int t1[] = {-955536464,-599634138,399850429,-165706338,-537800480,738983556};
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
p2 = 5247213600ll;
all_right = KawigiEdit_RunTest(5, p0, p1, true, p2) && all_right;
// ------------------
}
{
// ----- test 6 -----
int t0[] = {0};
p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
int t1[] = {0};
p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
p2 = 0ll;
all_right = KawigiEdit_RunTest(6, p0, p1, true, p2) && all_right;
// ------------------
}
if (all_right) {
cout << "You're a stud (at least on the example cases)!" << endl;
} else {
cout << "Some of the test cases had errors." << endl;
}
return 0;
}
// END KAWIGIEDIT TESTING
//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPGRlcXVlPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxzdGFjaz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPG51bWVyaWM+CiNpbmNsdWRlIDx1dGlsaXR5PgojaW5jbHVkZSA8c3N0cmVhbT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPGN0aW1lPgojaW5jbHVkZSA8Y3N0cmluZz4KI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