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

#QHD0000094. Xây tháp (Tower)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Xây tháp (Tower)

Tower

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn distinct blocks. Block ii has weight wiw_i, strength sis_i, and value viv_i. Choose any subset and arrange it vertically in any order. A tower is valid when, for every selected block, the total weight of all blocks above it is at most its strength sis_i. Find the maximum total value of a valid tower. Choosing no blocks is allowed.

Input

The first line contains nn. Each of the next nn lines contains wi,si,viw_i,s_i,v_i.

Output

Print the maximum total value of a valid tower.

Subtasks

The independent scoring groups have the following complete constraints:

  • Subtask 1 (20%): 1≤n≤121\le n\le12, 1≤wi,si≤201\le w_i,s_i\le20, 1≤vi≤10001\le v_i\le1000.
  • Subtask 2 (30%): 1≤n≤1001\le n\le100, 1≤wi,si≤2001\le w_i,s_i\le200, 1≤vi≤10000001\le v_i\le1000000.
  • Subtask 3 (50%): 1≤n≤10001\le n\le1000, 1≤wi,si≤100001\le w_i,s_i\le10000, 1≤vi≤10000000001\le v_i\le1000000000.

Examples

Input

3
2 2 20
2 1 30
3 1 40

Output

50

Explanation

Place block 2 on top of block 1. Block 1 supports weight 22, equal to its strength. Their total value is 30+20=5030+20=50, which is optimal.