Under construction

Still being built. Things may change

Bounty Hunter

Time limit4sMemory limit1024 MB

Summary
Muji starts at the origin and moves along the axes while thieves travel in fixed directions. Find the maximum total bounty Muji can collect within T hours.
Level

Medium7 of 10

Topics
Dynamic programming, Sorting, Geometry
Solved
No attempts yet

Problem

N thieves escaped into an infinitely large two-dimensional plane.

Bounty hunter Muji has T time left and wants to catch as many thieves as possible to collect the bounties on them.

Each thief starts at an integer coordinate point and moves at speed 1 for one hour in only one initial direction, chosen from east, south, west, or north.

Muji moves at the same speed as a thief, but can change to any of the four directions at any time. Muji may also stay in place.

The X coordinate increases toward the east, and the Y coordinate increases toward the north.

If Muji and a thief are at the same position at the same time, Muji spends 1 hour to catch that thief at that position and earns the bounty CiC_i. Muji cannot catch two or more thieves at once. The position may have real-valued coordinates.

Muji starts at the origin (0,0). Find the maximum total bounty Muji can earn within the remaining time T.

Input

The first line contains the number of thieves NN and the remaining time TT, separated by a space. (1≤N≤5001 \le N \le 500, 1≤T≤1 0001 \le T \le 1\,000)

From the second line through line N+1N+1, each line contains the thief's initial position XiX_i, YiY_i, bounty CiC_i, and direction DiD_i, separated by spaces. (−2 000≤Xi≤2 000-2\,000 \le X_i \le 2\,000, −2 000≤Yi≤2 000-2\,000 \le Y_i \le 2\,000, 1≤Ci≤500 0001 \le C_i \le 500\,000, 0≤Di≤30 \le D_i \le 3)

The thief moves east if DiD_i is 0, south if 1, west if 2, and north if 3.

Output

Print the maximum total bounty Muji can collect by catching thieves within the remaining time TT.

Hint

Muji always moves parallel to the XX axis or the YY axis.

Examples2

  1. Example 1

    Input
    3 20
    5 3 24 2
    -6 5 20 0
    -3 16 14 1
    
    Expected output
    44
    
  2. Example 2

    Input
    3 13
    5 3 24 2
    -6 5 20 0
    -3 17 14 1
    
    Expected output
    58