#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<double> CMP;
static const int INF = 999, UNK = -1;
int bitcnt(int x)
{
int c = 0;
for(; x; x>>=1)
c += x&1;
return c;
}
class AntlerSwapping { public:
int getmin(vector <int> a, vector <int> b, int capacity)
{
int N = a.size();
vector<bool> tbl(1<<N);
for(int mask=1; mask<(1<<N); ++mask)
{
vector<int> c;
for(int i=0; (1<<i)<=mask; ++i)
if((1<<i) & mask) {
c.push_back(a[i]);
c.push_back(b[i]);
}
sort(c.begin(), c.end());
bool can = true;
for(int i=0; i+1<c.size(); i+=2)
if(abs(c[i]-c[i+1]) > capacity)
can = false;
tbl[mask] = can;
}
vector<int> memo(1<<N, UNK);
int best = rec((1<<N)-1, tbl, memo);
return best>=INF ? -1 : best;
}
int rec(int mask, const vector<bool>& tbl, vector<int>& memo)
{
if(memo[mask] != UNK)
return memo[mask];
if(mask == 0)
return 0;
int best = INF;
for(int m2=mask; m2; m2=(m2-1)&mask)
{
int bc = bitcnt(m2);
if(tbl[m2])
best = min(best, (bc-1) + rec(mask&~m2, tbl, memo));
}
return memo[mask] = best;
}
};
// 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(_, AntlerSwapping().getmin(antler1, antler2, capacity));}
int main(){
CASE(0)
int antler1_[] = {3, 2, 2};
vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_));
int antler2_[] = {3, 5, 5};
vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_));
int capacity = 0;
int _ = 1;
END
CASE(1)
int antler1_[] = {4, 2, 6, 4, 8, 5, 2, 3};
vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_));
int antler2_[] = {3, 4, 5, 2, 8, 5, 7, 6};
vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_));
int capacity = 1;
int _ = 2;
END
CASE(2)
int antler1_[] = {12, 34, 56, 78};
vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_));
int antler2_[] = {1234, 2345, 3456, 4567};
vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_));
int capacity = 100;
int _ = -1;
END
CASE(3)
int antler1_[] = {47, 58, 2013};
vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_));
int antler2_[] = {49, 55, 2013};
vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_));
int capacity = 3;
int _ = 0;
END
CASE(4)
int antler1_[] = {4, 1, 7, 5, 7, 8, 2, 1, 3, 1, 7, 5, 9, 4, 9, 1};
vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_));
int antler2_[] = {10, 6, 5, 3, 1, 8, 4, 4, 4, 7, 1, 4, 6, 5, 10, 10};
vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_));
int capacity = 1;
int _ = 7;
END
/*
CASE(5)
int antler1_[] = ;
vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_));
int antler2_[] = ;
vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_));
int capacity = ;
int _ = ;
END
CASE(6)
int antler1_[] = ;
vector <int> antler1(antler1_, antler1_+sizeof(antler1_)/sizeof(*antler1_));
int antler2_[] = ;
vector <int> antler2(antler2_, antler2_+sizeof(antler2_)/sizeof(*antler2_));
int capacity = ;
int _ = ;
END
*/
}
// END CUT HERE