P27231 [AtCoder] Treasures
省选
题目描述

Problem Description

Thieves Anna and Bruno have sneaked into a wealthy mansion and found $N$ treasures numbered from $1$ to $N$. They decided to distribute these treasures between them. Some of the treasures will be taken by Anna, and some of the remaining treasures will be taken by Bruno. The same treasure cannot be taken by both of them. Anna or Bruno may choose not to take any treasures, and any remaining treasures will be left in the mansion, so there may be treasures that neither of them takes.

Each treasure has two values: "market value" and "preciousness". If the absolute difference between the total market value of the treasures taken by Anna and the total market value of the treasures taken by Bruno is less than or equal to $D$, Anna will think it's fair and be satisfied. On the other hand, Bruno wants treasures with higher preciousness than Anna's.

Find the maximum value of the difference between the total preciousness of the treasures taken by Bruno and the total preciousness of the treasures taken by Anna when the treasures are distributed such that Anna is satisfied.


Input Format

The input consists of $1 + N$ lines.

The first line contains two integers $N, D$ ($1 \leq N \leq 30$, $0 \leq D \leq 10^{15}$). This indicates that there are $N$ treasures, and Anna will be satisfied if the absolute difference between the total market value of the treasures taken by Anna and Bruno is less than or equal to $D$.

The following $N$ lines contain two integers each, $X_i, Y_i$ ($0 \leq X_i \leq 10^{15}$, $0 \leq Y_i \leq 10^{15}$), separated by a space. This indicates that the market value of treasure $i$ is $X_i$ and its preciousness is $Y_i$.

Among the five input data sets, input $1$ satisfies $N \leq 10$, and input $2$ satisfies $D = 0$.

Output Format

Output the maximum value of the difference between the total preciousness of the treasures taken by Bruno and the total preciousness of the treasures taken by Anna when the treasures are distributed such that Anna is satisfied.


Sample Input and Output

Sample Input #1

6 15
50 900
30 200
40 100
80 600
60 100
70 700

Sample Output #1

1200

Sample Input #2

5 0
0 1000000000000000
0 1000000000000000
1 1
1000000000000000 0
1000000000000000 0

Sample Output #2

2000000000000000

Notes/Hints

Sample Explanation 1

In sample input/output $1$, if Anna takes treasures $2$, $3$, and $5$, and Bruno takes treasures $1$ and $6$, the total market value of the treasures taken by Anna is $130$, and by Bruno is $120$. The absolute difference $10$ is less than or equal to $D = 15$, so Anna is satisfied. In this case, the total preciousness of the treasures taken by Anna is $400$, and by Bruno is $1600$. The difference between the total preciousness of the treasures taken by Bruno and Anna is $1200$, which is the maximum.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
6 15 50 900 30 200 40 100 80 600 60 100 70 700
样例输出
1200