#P6715. [CCO 2018] Fun Palace

[CCO 2018] Fun Palace

Description

There is a chain with NN nodes, and each node has a weight.

For every 1iN11 \le i \le N - 1, there is an edge connecting (i,i+1)(i, i + 1).

You may place some creatures on any nodes of the chain.

For the ii-th edge, if node ii has at least aia_i creatures or node i+1i + 1 has at least bib_i creatures, then this edge can be opened.

The chain has an exit. If there are ee creatures at node 11, then the exit will be opened.

You need to determine the maximum number of creatures you can place, such that the creatures will not open the exit.

Opening does not necessarily mean they can pass through.

Input Format

The first line contains an integer NN.

The second line contains an integer ee.

The next N1N - 1 lines each contain two numbers ai,bia_i, b_i.

Output Format

Output a single number, the maximum number of creatures that can be placed such that the creatures will not escape through the exit.

2
20
5 5
19
2
20
5 20
24

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

23

Hint

Sample Explanation

Sample 1 Explanation

If there are more than 1919 creatures, the exit will be opened.

Sample 2 Explanation

Suppose we place 2424 creatures at node 22. Then 44 creatures will go to node 11, but the exit still cannot be opened.

Sample 3 Explanation

Place 99 creatures at node 22, and place 1414 creatures at node 77.

Constraints

Subtask ID Points Special Constraints
1 1212 1e2001 \le e \le 200, ai=bi=1a_i = b_i = 1
2 2020 1e,ai,bi21 \le e, a_i, b_i \le 2
3 4848 1N,e,ai,bi2001 \le N, e, a_i, b_i \le 200
4 2020 None

For 100%100\% of the testdata, it is guaranteed that 1N1031 \le N \le 10^3, 1e,ai,bi1041 \le e, a_i, b_i \le 10^4.

Notes

This problem is translated from Canadian Computing Olympiad 2018 Day 1 T3 Fun Palace.

Translated by ChatGPT 5