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

#QHD0000081. Mời bạn cùng đi xem phim (Bribing Friends)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Mời bạn cùng đi xem phim (Bribing Friends)

Bribing Friends

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

Bessie wants some friends to accompany her to a movie. Friend ii contributes popularity PiP_i if persuaded. The aim is to maximize total popularity of all selected friends.

Normally, persuading friend ii costs CiC_i moonies. However, this friend offers a discount of one mooney for exactly XiX_i ice cream cones. Bessie may purchase any integer number of discounts, but cannot make the mooney payment negative. Precisely, if she buys did_i discounts for friend ii, then did_i must be an integer satisfying 0≤di≤Ci0\le d_i\le C_i; she spends diXid_iX_i cones and Ci−diC_i-d_i moonies.

Bessie has a budget of AA moonies and BB cones. Each friend can be selected at most once. The combined money and cone expenditures must not exceed the available resources.

Find the maximum popularity she can achieve. Choosing no friends is allowed and yields 00.

Input

The first line contains n,A,Bn,A,B. Each of the next nn lines contains Pi,Ci,XiP_i,C_i,X_i.

Output

Print one integer: the maximum total popularity.

Subtasks

  • Subtask 1 (20 points): 1≤n≤71\le n\le7; 0≤A,B≤120\le A,B\le12; 1≤Pi,Ci,Xi≤121\le P_i,C_i,X_i\le12.

  • Subtask 2 (30 points): 1≤n≤2001\le n\le200; 0≤A,B≤2000\le A,B\le200; 1≤Pi,Ci,Xi≤2001\le P_i,C_i,X_i\le200.

  • Subtask 3 (50 points): 1≤n≤20001\le n\le2000; 0≤A,B≤20000\le A,B\le2000; 1≤Pi,Ci,Xi≤20001\le P_i,C_i,X_i\le2000.

Examples

Example 1

Input

3 10 8
5 5 4
6 7 3
10 6 3

Output

15

Explanation

Invite friend 1 for 4 moonies and 4 cones, and friend 3 for 6 moonies and no cones. Their combined popularity is 5+10=155+10=15.

Example 2

Input

2 0 3
7 2 2
9 1 3

Output

9

Explanation

There are no moonies. Three cones can fully discount friend 2, yielding popularity 99.