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

#GD0000109. Hộp kẹo – phiên bản khó (Candy Box (Hard Version))

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Hộp kẹo – phiên bản khó (Candy Box (Hard Version))

Candy Box (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You have nn candies; candy ii has type aia_i and preference flag fif_i, where 11 means willing to give it away. Choose a gift such that the positive counts of selected candies of different types are pairwise distinct. First maximize the total selected count; among maximum-size gifts maximize the number with flag 11. Print both numbers.

Input

Line 1 contains nn. Each of the next nn lines contains ai,fia_i,f_i.

Output

Print two integers: the maximum number of gifted candies and the maximum number with flag 11 among gifts of that size.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤ai≤n1\le a_i\le n; fi∈{0,1}f_i\in\{0,1\}.
  • Subtask 2 (30 points): 1≤n≤25001\le n\le 2500; 1≤ai≤n1\le a_i\le n; fi∈{0,1}f_i\in\{0,1\}.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤ai≤n1\le a_i\le n; fi∈{0,1}f_i\in\{0,1\}.

Examples

Input

5
1 1
1 0
2 1
2 1
3 0

Output

3 3

Explanation

There are two candies each of types 1,21,2 and one of type 33. Choose both type-22 candies and the preferred type-11 candy, giving distinct positive type counts 22 and 11 and three flagged-11 candies. Output 3 3.