#include <bits/stdc++.h>
using
namespace
std;
class
triplet {
public
:
int
first;
int
second;
int
diff;
triplet(
int
f,
int
s,
int
d)
: first(f), second(s), diff(d)
{
}
};
bool
compare(triplet& a, triplet& b)
{
return
a.diff > b.diff;
}
int
findMaxAmount(
int
arr1[],
int
arr2[],
int
n,
int
x,
int
y)
{
vector<triplet> v;
for
(
int
i = 0; i < n; i++) {
triplet t(arr1[i], arr2[i],
abs
(arr1[i] - arr2[i]));
v.push_back(t);
}
sort(v.begin(), v.end(), compare);
int
maxAmount = 0;
int
i = 0;
while
(i < n && x > 0 && y > 0) {
if
(v[i].first > v[i].second) {
maxAmount += v[i].first;
x--;
}
if
(v[i].first < v[i].second) {
maxAmount += v[i].second;
y--;
}
if
(v[i].first == v[i].second) {
if
(x > 0) {
maxAmount += v[i].first;
x--;
}
else
if
(y > 0) {
maxAmount += v[i].second;
y--;
}
}
i++;
}
while
(i < v.size() && x--) {
maxAmount += v[i++].first;
}
while
(i < v.size() && y--) {
maxAmount += v[i++].second;
}
return
maxAmount;
}
int
main()
{
int
A[] = { 1, 4, 1, 2 };
int
B[] = { 4, 3, 2, 5 };
int
n =
sizeof
(A) /
sizeof
(A[0]);
int
X = 2, Y = 2;
cout << findMaxAmount(A, B, n, X, Y) <<
"\n"
;
}