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

#QHD0000093. Lịch dự tiệc (PARTY)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Lịch dự tiệc (PARTY)

PARTY

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are planning which of nn parties to attend. Party ii has entrance fee cic_i and provides fun fif_i. Your budget is BB, and each party may be attended at most once. Your first objective is to maximize total fun within the budget. Among all selections with maximum fun, minimize total spending. You may choose no party.

Input

The first line contains BB and nn, followed by nn lines containing ci,fic_i,f_i.

Output

Print two integers: minimum spending among optimal selections, and maximum fun.

Subtasks

The independent scoring groups have the following complete constraints:

  • Subtask 1 (20%): 0≤B≤500\le B\le50, 0≤n≤120\le n\le12, 5≤ci≤255\le c_i\le25, 0≤fi≤100\le f_i\le10.
  • Subtask 2 (30%): 0≤B≤2500\le B\le250, 0≤n≤500\le n\le50, 5≤ci≤255\le c_i\le25, 0≤fi≤100\le f_i\le10.
  • Subtask 3 (50%): 0≤B≤5000\le B\le500, 0≤n≤1000\le n\le100, 5≤ci≤255\le c_i\le25, 0≤fi≤100\le f_i\le10.

Examples

Input

50 10
12 3
15 8
16 9
16 6
10 2
21 9
18 4
12 4
17 8
18 9

Output

49 26

Explanation

The maximum achievable fun is 2626 at a total fee of 4949. No cheaper selection reaches fun 2626, so output 49 2649\ 26.