Symphony
Time limit1sMemory limit512 MB
Add one integer X to every element of A, then change at most K elements to any values, minimizing the sum of absolute differences against B.
- Level
Hard8 of 10
- Topics
- Sorting, Binary search, Greedy, Math
- Solved
- No attempts yet
Problem
Almost no one believed in the virtuous abilities of the composer Marin. That was especially true until the day he composed his 9th symphony.
The symphony can be represented as a series of frequencies that are integer numbers. In order for Marin to prove his talent and demonstrate that this symphony is not just one of many, he decided to compare it with the ancient symphony "Little Night Fiesta" of the best musician in history, Stjepan. The stars say that the lengths of these two symphonies are equal to N.
Marin compares the symphonies by writing them one under the other on a piece of paper. The symphony diversity is defined as the sum of the absolute differences of the corresponding frequencies. The diversity of symphonies A and B of length N is:
[\sum_{i=1}^{N}{\left| A_i - B_i \right|}]
Before comparing the two symphonies, Marin will do two things. First, he will modulate his symphony by adding an integer number X to each frequency. Then he will change no more than K frequencies to some other arbitrary frequency value because he had a vision in the dream, as does every top author.
Marin will choose X and change some K frequencies so that his symphony is as similar to Stjepan's as possible, that is, so the defined diversity is minimal. Help Marin and calculate the smallest possible diversity from Stjepan's symphony.
Input
In the first line there are integer numbers N and K (1 ≤ N ≤ 100 000, 0 ≤ K ≤ N), the numbers from the task's text.
In the second line there are N integers Ai (-1 000 000 ≤ Ai ≤ 1 000 000) which represent frequencies of Marin's symphony.
In the third line there are N integers Bi (-1 000 000 ≤ Bi ≤ 1 000 000) which represent frequencies of Stjepan's symphony.
Output
In the only line print out the smallest possible diversity between Marin and Stjepan's symphony.