using
System;
class
GFG
{
static
void
tree(
int
low,
int
high,
int
pos,
int
[]b,
int
[]a,
int
n)
{
if
(low == high)
{
b[pos] = a[high];
return
;
}
int
mid = (high + low) / 2;
tree(low, mid, 2 * pos + 1, b, a, n);
tree(mid + 1, high, 2 * pos + 2, b, a, n);
b[pos] = Math.Max(b[2 * pos + 1], b[2 * pos + 2]);
}
static
int
rangemax(
int
s,
int
e,
int
low,
int
high,
int
pos,
int
[]b,
int
[]a,
int
n)
{
if
(low <= s && high >= e)
return
b[pos];
if
(e < low || s > high)
return
int
.MinValue;
int
mid = (s + e) / 2;
int
left = rangemax(s, mid, low, high,
2 * pos + 1, b, a, n);
int
right = rangemax(mid + 1, e, low, high,
2 * pos + 2, b, a, n);
return
Math.Max(left, right);
}
static
int
solveQuery(
int
l,
int
r,
int
k,
int
n,
int
[]a,
int
[]b,
int
[]prefix)
{
if
(r - l > k)
return
-1;
int
maximum = rangemax(0, n - 1, l, r, 0, b, a, n);
if
(maximum < 0)
maximum = 0;
int
rangesum = prefix[r];
if
(l > 0)
rangesum -= prefix[l - 1];
int
answer = rangesum + (k - (r - l)) * maximum;
return
answer;
}
static
void
solveQueries(
int
n,
int
[]a,
int
[]b,
int
[]prefix,
int
[,]queries,
int
q)
{
for
(
int
i = 0; i < q; i++)
{
int
ans = solveQuery(queries[i,0], queries[i,1],
queries[i,2], n, a, b, prefix);
if
(ans != -1)
Console.WriteLine(ans);
else
Console.WriteLine(
"No"
);
}
}
static
void
findPrefixSum(
int
[]prefix,
int
[]a,
int
n)
{
prefix[0] = a[0];
for
(
int
i = 1; i < n; i++)
{
prefix[i] = prefix[i - 1] + a[i];
}
}
public
static
void
Main(String[] args)
{
int
[]a = { 1, 3, 2, -4, -5 };
int
n = a.Length;
int
[]b =
new
int
[5 * n];
tree(0, n - 1, 0, b, a, n);
int
[]prefix =
new
int
[n];
findPrefixSum(prefix, a, n);
int
[,]queries = { { 0, 2, 2 },
{ 0, 2, 4 },
{ 3, 4, 1 },
{ 0, 4, 2 } };
int
q = queries.GetLength(0);
solveQueries(n, a, b, prefix, queries, q);
}
}