Galactic Container Ship

No attempts yetTime limit1sMemory limit128 MB

Problem

A clay mine on Mars digs up red carbon-silicon clay and presses it into plates that are easy to transport. Every plate has the same standard width and thickness, but plates can differ in height and in material quality. There are 1000 quality classes, and plates are produced in heights of 1, 2, ..., 500000mm. The price of a plate depends only on its material quality, not on its height: a plate made of class qq material costs qq galactars.

A galactic container ship carries these plates. Its cargo hold is a hall with MM rails mounted in parallel in the floor; each rail can hold exactly one plate. The hold has a slanted ceiling: at one end it is 11mm high and at the other end it is MMmm high. That is, the ceiling above the rail numbered nn is exactly nnmm high, so that rail can hold only a plate whose height is at most nn.

A pile of plates waiting to be shipped sits at the dock. The captain wants to load cargo of the greatest possible total value, but is limited by the size of the hold (the plates cannot be trimmed). The experienced crew always chooses the load optimally, following the captain's wishes. Determine how much the captain must pay for the optimal load.

Write a program that:

  • reads the size of the hold and the sizes of the plates on the pile,
  • computes the maximum total value of cargo that can be loaded,
  • prints that maximum value.

Input

The first line contains two natural numbers MM and NN separated by a space (1M5000001 \le M \le 500000, 0N10000000 \le N \le 1000000). MM is the length and the maximum height of the hold, and at the same time the number of rails; NN is the number of plates on the pile. Each of the next NN lines describes one plate as two natural numbers ww and hh separated by a space (1w10001 \le w \le 1000, 1h5000001 \le h \le 500000): ww is the quality class of the plate's material and hh is the plate's height in millimeters. Note that the pile may contain plates taller than the maximum height allowed in the hold.

Output

Print a single integer: the value of the optimal load.