using
System;
using
System.Collections;
using
System.Collections.Generic;
class
Pairs : IComparable
{
public
int
first, second;
public
Pairs(
int
x,
int
y)
{
first = x;
second = y;
}
public
int
CompareTo(
object
obj)
{
if
(obj ==
null
)
return
1;
Pairs p = obj
as
Pairs;
return
p.first - first;
}
}
class
GFG{
static
int
maximiseScore(
int
[] A,
int
[] B,
int
K,
int
N)
{
List<Pairs> pairs =
new
List<Pairs>();
for
(
int
i = 0; i < N; i++)
{
pairs.Add(
new
Pairs(A[i], B[i]));
}
pairs.Sort();
int
sum = 0;
for
(
int
i = 0; i < N; i++)
{
if
(pairs[i].second == 0)
{
sum += pairs[i].first;
}
else
if
(pairs[i].second == 1)
{
if
(K > 0)
{
sum += pairs[i].first;
K--;
}
else
{
sum -= pairs[i].first;
}
}
}
return
sum;
}
public
static
void
Main(
string
[] args)
{
int
[] A = { 5, 4, 6, 2, 8 };
int
[] B = { 1, 0, 1, 1, 0 };
int
K = 2;
int
N = A.Length;
Console.Write(maximiseScore(A, B, K, N));
}
}