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

#QHD0000055. Ngân sách và khoản hoàn tiền (Trouble of 13-Dots)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ngân sách và khoản hoàn tiền (Trouble of 13-Dots)

Trouble of 13-Dots

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

You have a shopping budget of mm dollars and nn distinct items. Item ii has listed price pip_i and favour score fif_i. Each item may be bought at most once and buying nothing is allowed.

A credit-card promotion gives a refund of 200 dollars only when the total listed expense strictly exceeds 20002000. No refund is given for a total at most 20002000. Your final amount paid after any refund must not exceed mm. Maximize the sum of favour scores. In particular, a listed expense exactly equal to 20002000 receives no refund.

Input

The first line contains m,nm,n. Each of the next nn lines contains the item price pip_i and favour score fif_i.

Output

Print the greatest achievable total favour within the effective budget.

Subtasks

Scoring groups are independent. All constraints listed for a group apply simultaneously.

  • Subtask 1 (20%): 0≤m≤7000\le m\le700, 0≤n≤100\le n\le10, 1≤pi≤5001\le p_i\le500, 1≤fi≤51\le f_i\le5.
  • Subtask 2 (30%): 0≤m≤30000\le m\le3000, 0≤n≤500\le n\le50, 1≤pi≤20001\le p_i\le2000, 1≤fi≤51\le f_i\le5.
  • Subtask 3 (50%): 0≤m≤100000\le m\le10000, 0≤n≤1000\le n\le100, 1≤pi≤40001\le p_i\le4000, 1≤fi≤51\le f_i\le5.

Examples

Input

500 4
100 2
100 3
200 3
400 4

Output

8

Explanation

With budget 500500, no refunded purchase is affordable. Items priced 100,100,200100,100,200 cost 400400 and yield 2+3+3=82+3+3=8 favour; no better feasible selection exists.