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

#FT0000158. Bắt Snuke trên mặt phẳng (Snuke Panic (2D))

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Bắt Snuke trên mặt phẳng (Snuke Panic (2D))

Snuke Panic (2D)

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Takahashi starts at (0,0)(0,0) at time 00. He can move in either xx-direction with speed at most 11, but can move in the positive yy-direction only, at speed at most 11; moving downwards is forbidden. Snuke ii appears at time TiT_i at (Xi,Yi)(X_i,Y_i) and is worth AiA_i. It can be captured only by being at its exact position at that instant, with negligible capture time. Moving from (x1,y1,t1)(x_1,y_1,t_1) to (x2,y2,t2)(x_2,y_2,t_2) is feasible exactly when y2≥y1y_2\ge y_1 and ∣x2−x1∣+(y2−y1)≤t2−t1|x_2-x_1|+(y_2-y_1)\le t_2-t_1. Maximize total reward, possibly catching none. The result may exceed 32 bits.

Input

The first line contains nn. Each of the next nn lines gives Ti,Xi,Yi,AiT_i,X_i,Y_i,A_i.

Output

Print the maximum total reward as a 64-bit integer.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le 10; 1≤Ti≤1091\le T_i\le10^9, 0≤Xi,Yi≤1090\le X_i,Y_i\le10^9, 1≤Ai≤1091\le A_i\le10^9; all triples (Ti,Xi,Yi)(T_i,X_i,Y_i) are distinct.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤Ti≤1091\le T_i\le10^9, 0≤Xi,Yi≤1090\le X_i,Y_i\le10^9, 1≤Ai≤1091\le A_i\le10^9; all triples (Ti,Xi,Yi)(T_i,X_i,Y_i) are distinct.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤Ti≤1091\le T_i\le10^9, 0≤Xi,Yi≤1090\le X_i,Y_i\le10^9, 1≤Ai≤1091\le A_i\le10^9; all triples (Ti,Xi,Yi)(T_i,X_i,Y_i) are distinct.

Examples

Example 1

Input

3
1 0 0 100
3 2 1 10
5 3 1 1

Output

101

Explanation

Wait at (0,0)(0,0) at time 1 to collect reward 100. The second event at (2,1)(2,1) at time 3 needs three movement units within two time units and is unreachable. The third event at (3,1)(3,1) at time 5 is reachable in four units, giving reward 100+1=101100+1=101.

Example 2

Input

2
100 0 1 1
200 1 0 10

Output

10

Explanation

Both cannot be caught: going from (0,1)(0,1) to (1,0)(1,0) would decrease the y-coordinate, which is forbidden. Skipping the first allows catching the second for reward 10.