This page is still under construction.

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

Catching Fish

Time limit1sMemory limit256 MB

Summary
Given up to 100 fish on a large grid and a fixed net perimeter, find the placement of the net that covers the most fish.
Level

Medium5 of 10

Topics
Brute force, Prefix sum, Array
Solved
No attempts yet

Problem

Fish are an important source of protein in the Korean diet. However, rising sea temperatures and years of overfishing have steadily reduced the number of fish in the nearby seas, so the government has restricted both where fishing is allowed and the size of net a fishing boat may use.

Because fish live near the surface of the sea, the height of the net does not matter. The net can therefore be thought of as a single string of length ll that is stretched into the outline of a rectangle to catch fish. Each of the rectangle's two side lengths is an integer that is at least 11, and since the perimeter equals ll, the two side lengths sum to l/2l/2. For example, if l=10l = 10, the possible nets are the four rectangles 1×41 \times 4, 2×32 \times 3, 3×23 \times 2, and 4×14 \times 1.

The fishing area is an N×NN \times N grid of cells. Each cell has a coordinate: the top-left cell is (1,1)(1, 1) and the bottom-right cell is (N,N)(N, N). There are MM fish, each living on a distinct cell, and the fish do not move.

The boat picks one cell, treats it as the top-left corner of the net, and casts the net to the right and downward. That is, starting from the boat's cell the net covers a rectangular region of aa rows and bb columns (with a+b=l/2a + b = l/2), and it catches every fish inside that region, including those on its border. The net may only be cast so that it stays completely inside the grid.

The figure below shows an example with N=7N = 7, l=10l = 10, and M=6M = 6 fish at (2,2)(2, 2), (2,4)(2, 4), (3,3)(3, 3), (5,6)(5, 6), (6,2)(6, 2), and (7,4)(7, 4), where the boat on cell (2,2)(2, 2) casts a 2×32 \times 3 net. In this case it catches 33 fish.

Given the size of the grid, the positions of the fish, and the length of the net, write a program that finds the maximum number of fish that can be caught with a single cast.

Input

The first line contains three integers: the grid size NN, the net length ll, and the number of fish MM, separated by single spaces (2≤N≤10,0002 \le N \le 10{,}000, 4≤l≤1004 \le l \le 100, 1≤M≤1001 \le M \le 100). ll is even and satisfies l≤4N−4l \le 4N - 4.

Each of the next MM lines contains the coordinates of one fish, given as its row and column separated by a single space. The fish are listed in an arbitrary order.

Output

Print, as a single integer, the maximum number of fish that can be caught with one cast of the net.

Examples1

  1. Example 1

    Input
    7 10 6
    2 2
    2 4
    6 2
    7 4
    3 3
    5 6
    
    Expected output
    3