#include #include #include #include int main() { int n; std::cin >> n; const int n1 = n / 2; const int n2 = (n + 1) / 2; std::vector a(n), b(n); for (int i = 0; i < n; i++) { std::cin >> a[i] >> b[i]; } std::vector d1, d2; for (int i = 0; i < 1 << n1; i++) { long long diff = 0; for (int j = 0; j < n1; j++) { if (i >> j & 1) { diff += a[j]; } else { diff -= b[j]; } } d1.push_back(diff); } for (int i = 0; i < 1 << n2; i++) { long long diff = 0; for (int j = 0; j < n2; j++) { if (i >> j & 1) { diff += a[n1 + j]; } else { diff -= b[n1 + j]; } } d2.push_back(diff); } std::sort(d1.begin(), d1.end()); std::sort(d2.begin(), d2.end()); long long ans = 1e18; for (int i = 0; i < d1.size(); i++) { int j = std::lower_bound(d2.begin(), d2.end(), -d1[i]) - d2.begin(); for (int d = -1; d <= 1; d++) { if (0 <= j + d && j + d < d2.size()) { ans = std::min(ans, std::abs(d1[i] + d2[j + d])); } } } std::cout << ans << std::endl; }