This page is still under construction.

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

The Lazy Cow

Time limit1sMemory limit128 MB

Summary
Choose a start point to maximize the total grass of patches within Manhattan distance K of it.
Level

Medium7 of 10

Topics
Sliding window, Sorting, Segment tree, Geometry
Solved
No attempts yet

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 (1≤N≤100 0001 \le N \le 100\,000). Patch ii holds gig_i units of grass (1≤gi≤10 0001 \le g_i \le 10\,000) at a distinct point (xi,yi)(x_i, y_i) (0≤xi,yi≤1 000 0000 \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 (1≤K≤2 000 0001 \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,x−y)(u,v)=(x+y,x-y). A Manhattan ball becomes a box with ∣u−u0∣≤K|u-u_0|\le K and ∣v−v0∣≤K|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.

Examples1

  1. Example 1

    Input
    4 3
    7 8 6
    3 0 0
    4 6 0
    1 4 2
    
    Expected output
    8