#include #include #include #include #include struct Item { int64_t a; int64_t b; }; struct Sums { int64_t sum_a; int64_t sum_b; }; int64_t get_diff(Sums& s) { return std::abs(s.sum_a - s.sum_b); } int main() { int n; std::cin >> n; std::vector items; items.reserve(n); for (int i = 0; i < n; i++) { int64_t a, b; std::cin >> a >> b; items.push_back({ a,b }); } std::unordered_map dp; dp.insert({ 0,{0,0} }); int64_t min_diff; for (int i = 0; i < n; i++) { min_diff = INT64_MAX; std::unordered_map new_map; new_map.reserve(dp.size() * 2); for (std::pair& si : dp) { { Sums s = si.second; s.sum_a += items[i].a; int64_t diff = get_diff(s); new_map[diff] = s; min_diff = std::min(min_diff, diff); } { Sums s = si.second; s.sum_b += items[i].b; int64_t diff = get_diff(s); new_map[diff] = s; min_diff = std::min(min_diff, diff); } } dp = std::move(new_map); } std::cout << min_diff << std::endl; }