using
System;
public
class
Add_Elements
{
public
class
MinHeap
{
public
int
[] harr;
public
int
capacity;
public
int
heap_size;
public
MinHeap(
int
[]arr,
int
n)
{
heap_size = n;
capacity = n;
harr =
new
int
[n];
for
(
int
i = 0; i < n; i++)
harr[i] = arr[i];
for
(
int
i = n/2-1; i >= 0; i--)
heapify(i);
}
public
void
heapify(
int
i)
{
int
l = left(i);
int
r = right(i);
int
smallest = i;
if
(l < heap_size && harr[l] < harr[i])
smallest = l;
if
(r < heap_size && harr[r] < harr[smallest])
smallest = r;
if
(smallest != i)
{
int
temp = harr[i];
harr[i] = harr[smallest];
harr[smallest] = temp;
heapify(smallest);
}
}
public
static
int
parent(
int
i)
{
return
(i-1)/2;
}
static
int
left(
int
i)
{
return
(2*i + 1);
}
public
int
right(
int
i)
{
return
(2*i + 2);
}
public
int
extractMin()
{
if
(heap_size <= 0)
return
int
.MaxValue;
if
(heap_size == 1)
{
heap_size--;
return
harr[0];
}
int
root = harr[0];
harr[0] = harr[heap_size-1];
heap_size--;
heapify(0);
return
root;
}
public
int
getMin()
{
return
harr[0];
}
public
int
getSize()
{
return
heap_size;
}
public
void
insertKey(
int
k)
{
heap_size++;
int
i = heap_size - 1;
harr[i] = k;
while
(i != 0 && harr[parent(i)] > harr[i])
{
int
temp = harr[i];
harr[i] = harr[parent(i)];
harr[parent(i)] = temp;
i = parent(i);
}
}
}
static
int
countMinOps(
int
[]arr,
int
n,
int
k)
{
MinHeap h =
new
MinHeap(arr, n);
int
res = 0;
while
(h.getMin() < k)
{
if
(h.getSize() == 1)
return
-1;
int
first = h.extractMin();
int
second = h.extractMin();
h.insertKey(first + second);
res++;
}
return
res;
}
public
static
void
Main(String []args)
{
int
[]arr = {1, 10, 12, 9, 2, 3};
int
n = arr.Length;
int
k = 6;
Console.WriteLine(countMinOps(arr, n, k));
}
}