using
System;
using
System.Collections.Generic;
class
GFG
{
public
class
Pair
{
public
int
first, second;
public
Pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
static
void
generateArrayofPairs(
int
n, List<
int
> speed,
List<
int
> efficiency,
List<Pair> arr)
{
for
(
int
i = 0; i < n; i++)
{
arr.Insert(i,
new
Pair(efficiency[i], speed[i]));
}
arr.Sort((pair1, pair2) =>
{
if
(pair1.first != pair2.first)
return
pair2.first - pair1.first;
return
pair2.second - pair1.second;
});
}
static
int
maximizePerformance(List<
int
> speed,
int
K,
List<
int
> efficiency)
{
int
n = speed.Count;
List<Pair> arr =
new
List<Pair>();
generateArrayofPairs(n, speed,
efficiency, arr);
SortedSet<
int
> pq =
new
SortedSet<
int
>();
int
ans = 0;
int
SumOfSpeed = 0;
for
(
int
i = 0; i < arr.Count; i++)
{
int
e = arr[i].first;
int
s = arr[i].second;
SumOfSpeed += s;
pq.Add(s);
if
(pq.Count > K)
{
int
temp = pq.Min;
SumOfSpeed -= temp;
pq.Remove(temp);
}
ans = Math.Max(ans, SumOfSpeed * e);
}
return
ans;
}
public
static
void
Main(
string
[] args)
{
List<
int
> speed =
new
List<
int
>() { 2, 10, 3, 1, 5, 8 };
List<
int
> efficiency =
new
List<
int
>() { 5, 4, 3, 9, 7, 2 };
int
K = 2;
Console.WriteLine(maximizePerformance(speed, K, efficiency));
}
}