#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class MayTheBestPetWin { public:
int calc(vector <int> A, vector <int> B)
{
const int N = A.size();
vector<int> C(N);
for(int i=0; i<N; ++i)
C[i] = A[i] + B[i];
const int allmin = accumulate(A.begin(), A.end(), 0);
const int allmax = accumulate(B.begin(), B.end(), 0);
const int allsum = accumulate(C.begin(), C.end(), 0);
vector<bool> can(allsum+1);
can[0] = true;
for(int i=0; i<N; ++i)
for(int k=allsum; k>=C[i]; --k)
if(can[k-C[i]])
can[k] = true;
int best = allsum;
for(int i=0; i<=allsum; ++i)
if(can[i])
best = min(best, max(abs(i-allmin), abs(allmax-i)));
return 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(_, MayTheBestPetWin().calc(A, B));}
int main(){
CASE(0)
int A_[] = {3,4,4,7};
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = {3,4,4,7};
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = 2;
END
CASE(1)
int A_[] = {1,3,5,4,5};
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = {2,5,6,8,7};
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = 5;
END
CASE(2)
int A_[] = {2907,949,1674,6092,8608,5186,2630,970,1050,2415,1923,2697,5571,6941,8065,4710,716,756,5185,1341,993,5092,248,1895,4223,1783,3844,3531,2431,1755,2837,4015};
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = {7296,6954,4407,9724,8645,8065,9323,8433,1352,9618,6487,7309,9297,8999,9960,5653,4721,7623,6017,7320,3513,6642,6359,3145,7233,5077,6457,3605,2911,4679,5381,6574};
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = 52873;
END
/*
CASE(3)
int A_[] = ;
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = ;
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = ;
END
CASE(4)
int A_[] = ;
vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_));
int B_[] = ;
vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_));
int _ = ;
END
*/
}
// END CUT HERE