I am able to pass just 2 test cases… Please help!
My code:
#include
#include
#include
#define ll long long int
using namespace std;
ll minDiff (ll boys[], ll girls[], ll n, ll m)
{
ll sum = 0;
ll jChange = 0;
for (ll i = 0; i < n; i++)
{
ll ans = 100000000;
for (ll j = 0; j < m - i; j++)
{
ll num = min(ans, abs(girls[j] - boys[i]));
if (num < ans)
{
ans = num;
jChange = j;
}
}
if (ans == 100000000)
{
sum += girls[0];
}
else
{
sum += ans;
}
girls[jChange] = 10000000 + jChange;
sort(girls, girls + m);
}
return sum;
}
int main()
{
ll n = 0, m = 0;
cin >> n >> m;
ll boys[n];
ll girls[m];
for (ll i = 0; i < n; i++)
{
cin >> boys[i];
}
for (ll i = 0; i < m; i++)
{
cin >> girls[i];
}
sort(boys, boys + n);
sort(girls, girls + m);
cout << minDiff(boys, girls, n, m) << endl;
return 0;
}