using
System;
using
System.Collections.Generic;
using
System.Collections;
class
Line : IComparable<Line> {
public
int
m, c;
public
Line(
int
m,
int
c)
{
this
.m = m;
this
.c = c;
}
public
int
CompareTo(Line l)
{
if
(m != l.m)
return
l.m -
this
.m;
else
return
l.c -
this
.c;
}
public
bool
check(Line L1, Line L2, Line L3)
{
return
(L3.c - L1.c) * (L1.m - L2.m)
< (L2.c - L1.c) * (L1.m - L3.m);
}
};
class
Convex_HULL_Trick {
public
List<Line> l =
new
List<Line>();
public
void
add(Line newLine)
{
int
n = l.Count;
while
(
n >= 2
&& newLine.check(l[n - 2], l[n - 1], newLine)) {
n--;
}
l.Add(newLine);
}
public
int
value(
int
ind,
int
x)
{
return
l[ind].m * x + l[ind].c;
}
public
int
minQuery(
int
x)
{
if
(l.Count == 0)
return
Int32.MaxValue;
int
low = 0, high = l.Count - 2;
while
(low <= high) {
int
mid = (low + high) / 2;
if
(value(mid, x) > value(mid + 1, x))
low = mid + 1;
else
high = mid - 1;
}
return
value(low, x);
}
};
class
GFG {
public
static
void
Main(
string
[] args)
{
Line[] lines = {
new
Line(1, 1),
new
Line(0, 0),
new
Line(-3, 3) };
int
[] Q = { -2, 2, 0 };
int
n = 3, q = 3;
Convex_HULL_Trick cht =
new
Convex_HULL_Trick();
Array.Sort(lines);
for
(
int
i = 0; i < n; i++)
cht.add(lines[i]);
for
(
int
i = 0; i < q; i++) {
int
x = Q[i];
Console.WriteLine(cht.minQuery(x));
}
}
}