#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class FastPostman { public:
struct vert
{
int Ldone, Rdone;
bool nowAtL;
bool operator<( const vert& rhs ) const {
if( Ldone != rhs.Ldone ) return Ldone < rhs.Ldone;
if( Rdone != rhs.Rdone ) return Rdone < rhs.Rdone;
return nowAtL < rhs.nowAtL;
}
vert(int l, int r, bool b) : Ldone(l), Rdone(r), nowAtL(b) {}
};
typedef vert edge;
typedef pair<int,edge> cedge;
int minTime(vector <int> address, vector <int> maxTime, int initialPos)
{
const int N = address.size();
vector< pair<int,int> > L, R;
R.push_back( make_pair(initialPos,0) );
for(int i=0; i<N; ++i)
if( initialPos <= address[i] )
R.push_back( make_pair(address[i], maxTime[i]) );
else
L.push_back( make_pair(address[i], maxTime[i]) );
stable_sort( R.begin(), R.end() );
stable_sort( L.rbegin(), L.rend() );
vert S(0, 1, false);
priority_queue< cedge, vector<cedge>, greater<cedge> > Q;
set<vert> V;
Q.push( cedge(0,S) );
while( !Q.empty() )
{
int c = Q.top().first;
vert v = Q.top().second;
Q.pop();
if( V.count(v) )
continue;
if( v.Ldone == L.size() && v.Rdone == R.size() )
return c;
V.insert(v);
if( v.Ldone < L.size() ) {
vert u(v.Ldone+1, v.Rdone, true);
int cu = c + abs( (v.nowAtL ? L[v.Ldone-1] : R[v.Rdone-1]).first - L[v.Ldone].first );
if( !V.count(u) && cu <= L[v.Ldone].second )
Q.push( cedge(cu,u) );
}
if( v.Rdone < R.size() ) {
vert u(v.Ldone, v.Rdone+1, false);
int cu = c + abs( (v.nowAtL ? L[v.Ldone-1] : R[v.Rdone-1]).first - R[v.Rdone].first );
if( !V.count(u) && cu <= R[v.Rdone].second )
Q.push( cedge(cu,u) );
}
}
return -1;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, FastPostman().minTime(address, maxTime, initialPos));}
int main(){
CASE(0)
int address_[] = {1,3,5,7};
vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
int maxTime_[] = {9,2,5,100};
vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
int initialPos = 4;
int _ = 13;
END
CASE(1)
int address_[] = {1,5};
vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
int maxTime_[] = {6,2};
vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
int initialPos = 3;
int _ = 6;
END
CASE(2)
int address_[] = {1000000};
vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
int maxTime_[] = {45634};
vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
int initialPos = 876;
int _ = -1;
END
CASE(3)
int address_[] = {1,7,10,4};
vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
int maxTime_[] = {15,6,28,39};
vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
int initialPos = 2;
int _ = 20;
END
CASE(4)
int address_[] = {1000000,1000000,1000000,1000000};
vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
int maxTime_[] = {563,23452,32426,1};
vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
int initialPos = 1000000;
int _ = 0;
END
CASE(5)
int address_[] = {1};
vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
int maxTime_[] = {2};
vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
int initialPos = {3};
int _ = 2;
END
CASE(6)
int address_[] = {0, 1000000};
vector <int> address(address_, address_+sizeof(address_)/sizeof(*address_));
int maxTime_[] = {500000, 1500000};
vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_));
int initialPos = 500000;
int _ = 1500000;
END
}
// END CUT HERE