Under construction

Still being built. Things may change

Dotted Strips

Interview

Time limit3sMemory limit1024 MB

Summary
Cut a strip of L squares into as many length-M pieces with exactly N dots each as possible, where only some squares have dots.
Level

Medium5 of 10

Topics
Greedy, Prefix sum
Solved
No attempts yet

Problem

Juku has a paper strip made of LL squares, and some of the squares have dots. Juku wants to cut the strip into pieces so that as many pieces as possible have length exactly MM squares and exactly NN dots each. The strip may only be cut at the boundaries between squares.

Input

The first line contains four integers separated by spaces: LL (1≤L≤10151 \le L \le 10^{15}), TT (0≤T≤1050 \le T \le 10^5), MM (1≤M≤1061 \le M \le 10^6), and NN (0≤N≤1090 \le N \le 10^9). The squares are numbered 1…L1 \ldots L starting from one end of the strip.

Each of the next TT lines contains two integers: the number of a square with dots, and the number of dots on that square. Each such square has between 1 and 1,000 dots. The squares with dots are given in increasing order of their numbers.

Output

Print one integer: the maximum number of pieces with the required properties that Juku can cut from the strip.

Examples1

  1. Example 1

    Input
    12 7 4 3
    2 1
    3 2
    5 2
    6 2
    7 1
    10 2
    11 1
    
    Expected output
    2