Help with the algorithm please!

I found this question on a website.

Your team is playing a chess tournament against a visiting team. Your opponents have arrived with a team of M players, numbered 1,2,…,M. You have N players, numbered 1,2,…,N from which to choose your team, where N ≥ M.
Each of the M players from the visiting team must be paired up with one of your N players. The tournament rules insist that the pairings must respect the order that has been fixed for both teams. That is, when you pick players i1, i2,…,iM, to play against opponents numbered 1,2,…,M, it must be the case that i1<i2<…<iM, in terms of the order 1,2,…,N in which your players are listed.
You want to ensure a good fight, so you plan to pick your team so that the teams are as evenly balanced as possible. Each player j on your team has a numerical score YS(j) that represents his or her playing ability. Likewise, each player i in the opponent team has a playing ability indicated by a numerical score OS(i). The difference in strength between a player ij from your team and his or her opponent j on the visiting team is the absolute value |YS(ij) - OS(j)|. The imbalance of a pairing is the sum of these differences across all M match-ups in the pairing. Your aim is to minimize this imbalance.

The solution i had in mind, was to sort the two arrays, and find the least number of adjacent switches till there was no i>i+1, index.

However this would not work when one sorted array is:

while the other is: