using
System;
using
System.Collections.Generic;
class
GFG {
public
class
Pair {
public
char
first;
public
int
second;
public
Pair(
char
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
public
static
int
minimumCost(
string
S,
int
N,
int
K,
int
[] cost)
{
int
minCost = 0;
List<Pair> V =
new
List<Pair>();
Dictionary<
char
,
int
> mp
=
new
Dictionary<
char
,
int
>();
for
(
int
i = 0; i < N; i++) {
if
(mp.ContainsKey(S[i])) {
mp[S[i]] = mp[S[i]] + 1;
}
else
{
mp.Add(S[i], 1);
}
}
for
(
int
i = 0; i < 26; i++) {
if
(mp.ContainsKey((
char
)(
'a'
+ i))) {
V.Add(
new
Pair((
char
)(
'a'
+ i), cost[i]));
}
}
V.Sort((x, y) => x.second.CompareTo(y.second));
foreach
(Pair p
in
V)
{
int
count = mp[p.first];
if
(count >= K) {
minCost += p.second * K;
break
;
}
else
if
(count < K) {
minCost += p.second * count;
K = K - count;
}
}
return
minCost;
}
static
void
Main(
string
[] args)
{
string
S =
"aabcbc"
;
int
K = 3;
int
[] cost
= { 2, 1, 3, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
int
N = S.Length;
Console.WriteLine(minimumCost(S, N, K, cost));
}
}