시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 512 MB359926.471%

문제

You are working hard to prepare a fun party for your fun friends. Fortunately, you have just located the perfect venue for the fun party: a fun palace. The fun palace has N fun rooms connected in a linear structure. The fun rooms are numbered from 1 to N, and for 1 ≤ i ≤ N − 1, fun rooms i and i + 1 are connected by a fun tunnel. We say that such a fun tunnel is incident to fun rooms i and i + 1. In addition, fun room 1 is incident to an exit tunnel leaving the fun palace.

Fun tunnels can be in one of two states: open or closed. When the fun tunnel between fun rooms i and i + 1 is opened, fun friends may travel freely between the two rooms, in either direction.

By default, the fun tunnels will all be closed. However, they may temporarily be opened by a group of fun friends pressing down a required number of fun buttons. For each fun tunnel, there is a set of fun buttons present in the fun rooms connected to the fun tunnel. If all of the buttons in one of the rooms connected to a tunnel are pressed down by distinct fun friends, then the fun tunnel will open. Otherwise, the fun tunnel will immediately close. The fun tunnel between rooms i and i + 1 is connected to a set of ai buttons in room i and a set of bi buttons in room i + 1. To put this another way, if there are at least ai friends in room i or if there are bi friends in room i + 1, then tunnel between room i and i + 1 may be opened.

The exit tunnel operates under similar rules, but it is only connected to a single set of e buttons present in room 1.

You want to ensure your friends have maximum fun, and that obviously means keeping your fun friends trapped in the fun palace forever. What is the maximum number of fun friends that you can distribute to particular fun rooms such that the exit fun tunnel is never opened?

입력

The first line will contain a single integer N (1 ≤ N ≤ 1000), the number of fun rooms. The next line contains a single integer e (1 ≤ e ≤ 10 000). The next N −1 lines contain two space-separated integers each, with the ith of these lines containing ai and bi (1 ≤ ai, bi ≤ 10 000).

출력

Output a single integer, the maximum number of fun friends over all possible distributions of fun friends to fun rooms such that there is no way for the fun friends to open the exit tunnel.

예제 입력 1

2
20
5 5

예제 출력 1

19

If we had any more than 19 fun friends, then they would be able to move to fun room 1 and press all 20 fun buttons required in order to open the exit tunnel.

예제 입력 2

2
20
5 20

예제 출력 2

24

Suppose we place 24 fun friends in fun room 2. In order to open the fun tunnel between fun rooms 1 and 2, 20 fun friends must stay in fun room 2 to press the fun buttons. This allows only 4 fun friends into fun room 1, which is not enough to press every fun button in the set of 5.

예제 입력 3

7
7
2 8
6 6
1 1
2 4
2 8
7 8

예제 출력 3

23

One optimum distribution is to place 9 fun friends in fun room 2 and 14 fun friends in fun room 7.