The Lazy Cow

No attempts yetTime limit1sMemory limit128 MB

Problem

On a hot summer day, Bessie the cow feels lazy. She wants to pick a starting point in her field so that she can reach as much grass as possible within a short walk.

The field has NN grass patches (1N1000001 \le N \le 100\,000). Patch ii holds gig_i units of grass (1gi100001 \le g_i \le 10\,000) at a distinct point (xi,yi)(x_i, y_i) (0xi,yi10000000 \le x_i, y_i \le 1\,000\,000). Bessie chooses one point in the field as her start (it may coincide with a patch or use non-integer coordinates). She wants the maximum total grass within Manhattan distance KK of that point (1K20000001 \le K \le 2\,000\,000).

Each step moves 1 unit north, south, east, or west. For example, (0,0)(0,0) to (3,2)(3,2) needs 5 steps. A step may be split: half a unit north plus half a unit east still counts as one step.

Input

  • Line 1: integers NN and KK
  • Next NN lines: gig_i, xix_i, yiy_i for each patch

Output

Print one integer: the maximum grass Bessie can reach within KK steps when she picks the best start.

Hint

Rotate coordinates to (u,v)=(x+y,xy)(u,v)=(x+y,x-y). A Manhattan ball becomes a box with uu0K|u-u_0|\le K and vv0K|v-v_0|\le K. Sort by uu, slide a window of width at most 2K2K, and within each window slide on vv the same way.