This page is still under construction.

Parts of this page are still being built. What you see may change.

Computer Purchase Return

Interview

Time limit2sMemory limit512 MB

Summary
Choose exactly one component of each of T types so the total cost stays within budget B and the total value is maximized.
Level

Medium6 of 10

Topics
Dynamic programming, Array, Implementation, Brute force
Solved
No attempts yet

Problem

You want to build your own computer to get the best value. The computer is made of TT (1≤T≤51 \le T \le 5) different types of components, and it must contain exactly one component of each type.

Each component has an integer cost cic_i (1≤ci≤30001 \le c_i \le 3000), an integer value viv_i (1≤vi≤30001 \le v_i \le 3000), and a type tit_i (1≤ti≤T1 \le t_i \le T).

An online parts store offers NN different components (1≤N≤10001 \le N \le 1000) to choose from.

For a given budget BB (1≤B≤30001 \le B \le 3000), maximize the total value of the components in your computer while keeping the total cost at most BB.

If you cannot build such a computer, print −1-1.

Input

The first line contains TT, the number of component types your computer requires.

The next line contains NN. Then NN lines follow, each containing three integers cic_i, viv_i, and tit_i separated by single spaces.

The last line contains the budget BB.

Output

Print the maximum total value of a computer whose total cost is at most BB. If no valid computer can be built, print −1-1.

Hint

For the sample, choosing the components with cost 1111 and cost 55 gives a computer with value 1818, and no other combination reaches a higher value.

Examples3

  1. Example 1

    Input
    2
    5
    10 6 1
    5 7 1
    6 10 2
    1 5 1
    11 11 2
    16
    
    Expected output
    18
    
  2. Example 2

    Input
    1
    3
    3 5 1
    2 4 1
    10 20 1
    5
    
    Expected output
    5
    
  3. Example 3

    Input
    2
    2
    10 5 1
    10 5 2
    15
    
    Expected output
    -1