PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000120. Chọn hoa (Choosing Flowers)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chọn hoa (Choosing Flowers)

Choosing Flowers

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Vladimir must choose exactly nn flowers among mm types, each available in unlimited quantities. The first flower of type ii brings aia_i happiness, and each additional flower of the same type brings bib_i. Choosing xi>0x_i>0 flowers of type ii contributes ai+(xi−1)bia_i+(x_i-1)b_i; choosing none contributes 00. The sum of all chosen quantities must be exactly nn. Find the maximum total happiness.

Input

The first line contains n,mn,m. Each of the next mm lines contains two integers ai,bia_i,b_i describing flower type ii (11-based).

Output

Print one integer: the maximum happiness attainable using exactly nn flowers.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le 10; 1≤m≤81\le m\le 8; 0≤ai,bi≤1090\le a_i,b_i\le 10^9 for all 1≤i≤m1\le i\le m.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤m≤15001\le m\le 1500; 0≤ai,bi≤1090\le a_i,b_i\le 10^9 for all 1≤i≤m1\le i\le m.
  • Subtask 3 (50 points): 1≤n≤10000000001\le n\le 1000000000; 1≤m≤1000001\le m\le 100000; 0≤ai,bi≤1090\le a_i,b_i\le 10^9 for all 1≤i≤m1\le i\le m.

Examples

Input

4 3
5 0
1 4
2 2

Output

14

Explanation

We need 44 flowers. One flower of type 11 yields 55. Three flowers of type 22 yield 1+2⋅4=91+2\cdot4=9. Together they yield 1414. No allocation of exactly four flowers yields more, so the answer is 14.