class
GFG
{
static
int
maxSum(
int
arr[],
int
n,
int
x,
int
y,
int
z)
{
int
minNeg = Integer.MAX_VALUE,
maxNeg = Integer.MAX_VALUE;
int
minPos = Integer.MIN_VALUE,
maxPos = Integer.MIN_VALUE;
boolean
isZeroPresent =
false
;
for
(
int
i =
0
; i < n; i++)
{
if
(arr[i] ==
0
)
{
isZeroPresent =
true
;
}
else
if
(arr[i] <
0
)
{
minNeg = Math.min(minNeg, arr[i]);
maxNeg = Math.max(maxNeg, arr[i]);
}
else
{
minPos = Math.min(minPos, arr[i]);
maxPos = Math.max(maxPos, arr[i]);
}
}
int
sum =
0
;
if
(x !=
0
)
{
if
(x <
0
)
{
if
(minNeg != Integer.MAX_VALUE)
sum += (x * minNeg);
else
if
(!isZeroPresent)
sum += (x * minPos);
}
else
{
if
(maxPos != Integer.MIN_VALUE)
sum += (x * maxPos);
else
if
(!isZeroPresent)
sum += (x * maxPos);
}
}
if
(y !=
0
)
{
if
(y <
0
)
{
if
(minNeg != Integer.MAX_VALUE)
sum += (y * minNeg);
else
if
(!isZeroPresent)
sum += (y * minPos);
}
else
{
if
(maxPos != Integer.MIN_VALUE)
sum += (y * maxPos);
else
if
(!isZeroPresent)
sum += (y * maxPos);
}
}
if
(z !=
0
)
{
if
(z <
0
)
{
if
(minNeg != Integer.MAX_VALUE)
sum += (z * minNeg);
else
if
(!isZeroPresent)
sum += (z * minPos);
}
else
{
if
(maxPos != Integer.MIN_VALUE)
sum += (z * maxPos);
else
if
(!isZeroPresent)
sum += (z * maxPos);
}
}
return
sum;
}
public
static
void
main(String[] args)
{
int
arr[] = {
2
,
4
, -
9
, -
64
,
7
,
3
};
int
n = arr.length;
int
x = -
1
, y =
1
, z =
1
;
System.out.print(maxSum(arr, n, x, y, z));
}
}