using
System;
using
System.Collections.Generic;
using
System.Linq;
class
GFG
{
static
long
MaximumMultiple(
int
N,
int
[] A)
{
PriorityQueue<
long
> pq =
new
PriorityQueue<
long
>();
Array.Sort(A);
long
i = 0;
long
neg = 0;
long
pos = 0;
List<
long
> first =
new
List<
long
>();
List<
long
> second =
new
List<
long
>();
while
(i < N)
{
if
(A[i] < 0)
{
second.Add(A[i]);
neg++;
}
else
{
first.Add(A[i]);
pos++;
}
i++;
}
if
(neg == pos)
{
i = 0;
while
(i < first.Count)
{
pq.Push(first[(
int
)i] * 1L * second[(
int
)i]);
i++;
}
return
pq.Top();
}
else
if
(pos > neg)
{
i = 0;
int
end = first.Count - 1;
while
(i < second.Count)
{
pq.Push(first[end] * 1L * second[(
int
)i]);
i++;
end--;
}
int
start = 0;
while
(start < end)
{
pq.Push(first[start] * 1L * first[end]);
start++;
end--;
}
return
pq.Top();
}
else
{
i = 0;
int
end = 0;
while
(i < first.Count)
{
pq.Push(first[(
int
)i] * 1L * second[(
int
)end]);
i++;
end++;
}
int
start = second.Count - 1;
while
(start > end)
{
pq.Push(second[start] * second[(
int
)end] * 1L);
start--;
end++;
}
return
pq.Top();
}
}
static
void
Main()
{
int
N = 4;
int
[] A = { -12, 17, -13, 17 };
Console.WriteLine(MaximumMultiple(N, A));
}
}
class
PriorityQueue<T>
where
T : IComparable<T>
{
private
List<T> heap;
public
PriorityQueue()
{
heap =
new
List<T>();
}
public
int
Count
{
get
{
return
heap.Count; }
}
public
void
Push(T value)
{
heap.Add(value);
int
currentIndex = heap.Count - 1;
int
parentIndex = (currentIndex - 1) / 2;
while
(parentIndex >= 0 && heap[parentIndex].CompareTo(heap[currentIndex]) < 0)
{
Swap(parentIndex, currentIndex);
currentIndex = parentIndex;
parentIndex = (currentIndex - 1) / 2;
}
}
public
T Pop()
{
if
(heap.Count == 0)
{
throw
new
InvalidOperationException(
"PriorityQueue is empty"
);
}
T root = heap[0];
int
lastIndex = heap.Count - 1;
heap[0] = heap[lastIndex];
heap.RemoveAt(lastIndex);
int
currentIndex = 0;
while
(
true
)
{
int
leftChildIndex = 2 * currentIndex + 1;
int
rightChildIndex = 2 * currentIndex + 2;
int
swapIndex = currentIndex;
if
(leftChildIndex < heap.Count && heap[leftChildIndex].CompareTo(heap[swapIndex]) > 0)
{
swapIndex = leftChildIndex;
}
if
(rightChildIndex < heap.Count && heap[rightChildIndex].CompareTo(heap[swapIndex]) > 0)
{
swapIndex = rightChildIndex;
}
if
(swapIndex == currentIndex)
{
break
;
}
Swap(currentIndex, swapIndex);
currentIndex = swapIndex;
}
return
root;
}
public
T Top()
{
if
(heap.Count == 0)
{
throw
new
InvalidOperationException(
"PriorityQueue is empty"
);
}
return
heap[0];
}
private
void
Swap(
int
index1,
int
index2)
{
T temp = heap[index1];
heap[index1] = heap[index2];
heap[index2] = temp;
}
}