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

#BS0000278. Nâng cấp lò bánh (Bakery)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nâng cấp lò bánh (Bakery)

Bakery

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

Bessie has one oven. A cookie takes tCt_C time units and a muffin takes tMt_M. The oven processes only one pastry at once, and starts an order only when it is placed. N friends arrive separately; friend i orders aia_i cookies and bib_i muffins and can wait at most cic_i time units. Before their visits, each coin reduces either production time by one; both times must remain positive integers. Minimize the coins required to satisfy every order.

Input

First line: N,tC,tMN,t_C,t_M. Each of the following N lines contains ai,bi,cia_i,b_i,c_i.

Output

Print the minimum number of coins.

Subtasks

  • Subtask 1 (20 points): 1≤N≤101\le N\le10; 1≤tC,tM≤201\le t_C,t_M\le20; 1≤ai,bi≤201\le a_i,b_i\le20; ai+bi≤ci≤1000a_i+b_i\le c_i\le1000 (1≤i≤N1\le i\le N).
  • Subtask 2 (30 points): 1≤N≤1001\le N\le100; 1≤tC,tM≤1091\le t_C,t_M\le10^9; 1≤ai=bi≤1091\le a_i=b_i\le10^9; ai+bi≤ci≤2⋅1018a_i+b_i\le c_i\le2\cdot10^{18} (1≤i≤N1\le i\le N).
  • Subtask 3 (50 points): 1≤N≤1001\le N\le100; 1≤tC,tM≤1091\le t_C,t_M\le10^9; 1≤ai,bi≤1091\le a_i,b_i\le10^9; ai+bi≤ci≤2⋅1018a_i+b_i\le c_i\le2\cdot10^{18} (1≤i≤N1\le i\le N).

Examples

Input 1

3 7 9
4 3 18
2 4 19
1 1 6

Output 1

11

Explanation

Reduce the times to 3 and 2 for 11 coins; orders take 18,14,5. With only ten reductions the times sum to six; the first two orders cannot both be satisfied by positive times.