using
System;
using
System.Collections.Generic;
public
class
Pair<T1, T2> {
public
T1 Key;
public
T2 Value;
public
Pair(T1 key, T2 value) {
Key = key;
Value = value;
}
}
public
class
Program {
public
static
int
SortByFirstDesc(Pair<
int
,
int
> a, Pair<
int
,
int
> b) {
return
b.Key.CompareTo(a.Key);
}
public
static
int
MaxCost(Pair<
int
,
int
>[] a,
int
N,
int
K) {
int
res = 0, sum = 0;
SortedSet<Pair<
int
,
int
>> s =
new
SortedSet<Pair<
int
,
int
>>(
Comparer<Pair<
int
,
int
>>.Create(SortByFirstDesc));
Array.Sort(a, 0, N, Comparer<Pair<
int
,
int
>>.Create(SortByFirstDesc));
for
(
int
i = 0; i < N; ++i) {
s.Add(
new
Pair<
int
,
int
>(a[i].Key, i));
sum += a[i].Key;
while
(s.Count > K) {
Pair<
int
,
int
> p = s.Min;
sum -= p.Key;
s.Remove(p);
}
res = Math.Max(res, sum * a[i].Value);
}
return
res;
}
public
static
void
Main() {
Pair<
int
,
int
>[] arr =
new
Pair<
int
,
int
>[] {
new
Pair<
int
,
int
>(12, 3),
new
Pair<
int
,
int
>(62, 21),
new
Pair<
int
,
int
>(31, 16),
new
Pair<
int
,
int
>(19, 2),
new
Pair<
int
,
int
>(32, 19),
new
Pair<
int
,
int
>(12, 17),
new
Pair<
int
,
int
>(1, 7)
};
int
N = arr.Length;
int
K = 3;
Console.WriteLine(MaxCost(arr, N, K));
}
}