#include #define DEBUG(C) cerr << #C << " = " << C << endl using namespace std; const int MAX = 44; int n; long long a[MAX], b[MAX]; const long long inf = 1e18; int main() { scanf("%d", &n); long long asum = 0, bsum = 0; for (int i = 0; i < n; i++) { scanf("%lld%lld", a + i, b + i); asum += a[i]; bsum += b[i]; } vector> vec; for (int i = 0; i < n; i++) { vec.push_back(make_pair(llabs(a[i] - b[i]), i)); } sort(vec.begin(), vec.end()); long long ans = min(asum, bsum); long long adeg = 0, bdeg = 0; for (int i = 0; i < n; i++) { adeg += a[vec[i].second]; bdeg += b[vec[i].second]; ans = min(ans, min(llabs(asum - adeg - bdeg), llabs(bsum - bdeg - adeg))); } printf("%lld\n", ans); return 0; }