問題一覧 > 通常問題

No.545 Mom's Two Precious Children

レベル : / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 153
作問者 : horiesiniti / テスター : 37zigen
お気に入りにしたユーザー ProblemId : 1203 / 自分の提出
問題文最終更新日: 2026-06-25 18:10:08
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 169 (順位表) の他の問題:

Problem Statement

Mom has two precious children, A and B. Mom loves them both equally and raises them without favoring either one. Mom now has n items, and she intends to distribute all n items between the two children. However, the two children get different levels of satisfaction when they receive an item. For example, if A receives a sweet bean bun, A's satisfaction is 15, and if B receives it, B's satisfaction is 30. Since Mom loves them both, she has decided to distribute the items so that the difference between A's total satisfaction and B's total satisfaction is minimized. Write a program that, from the data, finds the minimum difference (that is, the absolute value of the difference). The data is as follows.

The number of items n.
The following n lines give the two children's satisfaction values Ai and Bi for the items.
A1 is the satisfaction when A receives item 1. B1 is the satisfaction when B receives item 1.
In general, Ai is the satisfaction when A receives item i, and Bi is the satisfaction when B receives item i.

Input

$n$
$A_1$ $B_1$
$A_2$ $B_2$
$\dots$
$A_{n}$ $B_{n}$

$1 \leq n \leq 32$
$0 \leq Ai \leq5000000000$
$0 \leq Bi \leq5000000000$
Ai is A's satisfaction when A receives item i, and Bi is B's satisfaction when B receives item i. For both children, the satisfaction values of the items received are simply summed. Compute the difference in total satisfaction when the items are distributed so that the difference in satisfaction is minimized.


Output

Output the minimum absolute value of the difference in satisfaction between the two children.

Sample

Sample 1
Input
1
10 15
Output
10

There is only one item, and giving it to A results in a smaller difference in satisfaction than giving it to B. Mom is not aiming for the highest satisfaction; she wants to minimize the difference in the distribution. Since the condition is that all items are distributed to A and B, not distributing an item is not an option. It is also possible for all items to be distributed to only one of A or B.

Sample 2
Input
3
25 14
25 23
32 10
Output
1

Distributing items 1 and 3 to B and item 2 to A minimizes the difference.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。