using
System;
using
System.Collections.Generic;
public
struct
pr
{
public
int
first;
public
int
second;
}
public
class
Sortbyfirst : IComparer<pr>
{
public
int
Compare(pr a, pr b)
{
if
(a.first == b.first)
return
a.second > b.second ? 1 : -1;
return
a.first < b.first ? -1 : 1;
}
}
public
static
SortedSet<pr> s1 =
new
SortedSet<pr>(
new
Sortbyfirst());
public
static
SortedSet<pr> s2 =
new
SortedSet<pr>(
new
Sortbyfirst());
public
static
Dictionary<
int
,
int
> m =
new
Dictionary<
int
,
int
>();
public
static
void
type1(
int
num)
{
if
(m.ContainsKey(num))
{
int
cnt = m[num];
pr target1 =
new
pr { first = num, second = cnt };
pr target2 =
new
pr { first = cnt, second = num };
s1.Remove(target1);
s2.Remove(target2);
s1.Add(
new
pr { first = num, second = m[num] + 1 });
s2.Add(
new
pr { first = m[num] + 1, second = num });
}
else
{
s1.Add(
new
pr { first = num, second = 1 });
s2.Add(
new
pr { first = 1, second = num });
}
m[num] += 1;
}
public
static
void
type2(
int
num)
{
if
(m.ContainsKey(num))
{
int
cnt = m[num];
pr target1 =
new
pr { first = num, second = cnt };
pr target2 =
new
pr { first = cnt, second = num };
s1.Remove(target1);
s2.Remove(target2);
s1.Add(
new
pr { first = num, second = m[num] - 1 });
s2.Add(
new
pr { first = m[num] - 1, second = num });
m[num] -= 1;
}
}
public
static
int
type3()
{
if
(s1.Count != 0)
{
return
s2.Min.second;
}
else
return
-1;
}
public
static
int
type4()
{
if
(s1.Count != 0)
{
return
s2.Max.second;
}
else
return
-1;
}
public
static
void
Main()
{
type1(6);
type1(6);
type1(7);
Console.WriteLine(type3());
type1(7);
type2(7);
type1(7);
Console.WriteLine(type3());
Console.WriteLine(type4());
}