#include #include #include using namespace std; void halr_sum(vector a,vector b,long long int temp_sum,int i,int end, vector &sum){ if(i==end){ sum.push_back(temp_sum); return; } halr_sum(a,b,temp_sum + a[i],i+1,end,sum); halr_sum(a,b,temp_sum - b[i],i+1,end,sum); } int main(){ int n; cin >> n; vector a(n),b(n); for(int i=0; i> a[i]>>b[i]; } int lc=n/2,rc=n-lc; vector l(lc),r(rc); vector lsum,rsum; halr_sum(a,b,0,0,lc,lsum); halr_sum(a,b,0,lc,n,rsum); sort(lsum.begin(),lsum.end()); sort(rsum.begin(),rsum.end()); long long ans = 5000000000; int rnum = rsum.size()-1; int lnum = 0; while(lnum < lsum.size() && rnum > 0){ if(llabs(lsum[lnum] + rsum[rnum]) >= llabs(lsum[lnum] + rsum[rnum - 1])){ rnum--; }else{ ans = min(ans,llabs(lsum[lnum] + rsum[rnum])); lnum++; } } if(lnum < lsum.size()){ ans = min(ans,llabs(lsum[lnum] + rsum[rnum])); } cout << ans << endl; return 0; }