Under construction

Still being built. Things may change

Multiple sequence

Time limit0.5sMemory limit1024 MB

Summary
Choose N integers from M kinds, each kind limited to c_i copies, so every term is a multiple of the previous one, and find the maximum possible sum.
Level

Medium6 of 10

Topics
Dynamic programming, Math
Solved
No attempts yet

Problem

There are MM kinds of positive integers. The ii-th integer x_ix\_i has c_ic\_i copies. Choose NN of these integers and build a sequence AA of length NN that satisfies the condition below. Find the maximum possible sum of AA.

  • A_j+1A\_{j + 1} is a multiple of A_jA\_j. (1≤j<N)(1 \le j \lt N)

Input

The first line contains the length NN of sequence AA and the number of kinds MM, separated by a space. (1≤N,M≤500)(1 \le N, M \le 500) From the second line onward, each of the next MM lines gives one integer's information. The ii-th line contains a positive integer x_ix\_i and c_ic\_i, separated by a space. All x_ix\_i are distinct. (1≤x_i,c_i≤109)(1 \le x\_i, c\_i \le 10^9)

Output

Print the maximum sum of sequence AA. If no such sequence AA exists, print -1.

Examples3

  1. Example 1

    Input
    4 5
    12 1
    8 2
    1 4
    6 3
    2 4
    
    Expected output
    30
    
  2. Example 2

    Input
    3 3
    81 81
    9 9
    3 3
    
    Expected output
    243
    
  3. Example 3

    Input
    9 5
    2 1
    3 6
    5 3
    7 8
    11 3
    
    Expected output
    -1