PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000022. Đám mây từ tối ưu (Word Clouds Revisited)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đám mây từ tối ưu (Word Clouds Revisited)

Word Clouds Revisited

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

A page has width CC. You must place NN word blocks in the given order. Block ii has width wiw_i and height hih_i.

Each line contains a consecutive group of blocks whose total width is at most CC. The height of a line is the maximum height of a block on that line. Each block appears on exactly one line, and the block order must remain unchanged.

Minimize the sum of the line heights.

Input

The first line contains NN and CC, the number of blocks and page width.

Each of the next NN lines contains wiw_i and hih_i, the width and height of block ii.

Output

Print the minimum possible sum of line heights.

Subtasks

  • Subtask 1 (20 points): 2≤N≤302\le N\le 30; 150≤C≤1000150\le C\le 1000; 10≤wi≤15010\le w_i\le 150; 10≤hi≤15010\le h_i\le 150; wi≤Cw_i\le C for all 1≤i≤N1\le i\le N.
  • Subtask 2 (30 points): 2≤N≤4002\le N\le 400; 150≤C≤1000150\le C\le 1000; 10≤wi≤15010\le w_i\le 150; 10≤hi≤15010\le h_i\le 150; wi≤Cw_i\le C for all 1≤i≤N1\le i\le N.
  • Subtask 3 (50 points): 2≤N≤50002\le N\le 5000; 150≤C≤1000150\le C\le 1000; 10≤wi≤15010\le w_i\le 150; 10≤hi≤15010\le h_i\le 150; wi≤Cw_i\le C for all 1≤i≤N1\le i\le N.

Examples

Input

6 260
65 23
38 11
135 48
97 43
95 28
130 23

Output

99

Explanation

Place blocks (1,2)(1,2), (3,4)(3,4), and (5,6)(5,6) on three consecutive lines. Each line fits the width limit 260260. Their heights are 2323, 4848, and 2828, giving 23+48+28=9923+48+28=99, which is minimal.