Coat Rack
Time limit1sMemory limit1024 MB
Sort garments and targets; sliding garments keeps their order and may stack them, so assign each target to a position minimizing total distance under order constraints.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Divide and conquer, Sorting, Greedy
- Solved
- No attempts yet
Problem
Zigmas works in a cloakroom where fussy people hang up their own clothes but then end up unhappy with the exact spot where they hung them.
The cloakroom has a single straight coat rack on which people have each hung one garment. Every garment hangs at a point with integer coordinate , and at most one garment hangs at any single coordinate. The owner of each garment wants to move it to the point with coordinate , and that owner's dissatisfaction equals the distance from the garment's current position to the desired position.
Zigmas wants to make the total dissatisfaction as small as possible by sliding the garments along the rack. He may not take any garment off the rack, so the garments cannot change their relative order, but he may push several garments so close together that they share the same coordinate.
Compute the minimum possible total dissatisfaction after the garments have been rearranged.
Input
The first line contains two space-separated integers: the number of garments and the length of the rack .
The second line contains space-separated integers , the initial coordinates of the garments.
The third line contains space-separated integers , the coordinates where the owners want their garments to end up.
Output
Print, on a single line, the minimum possible total dissatisfaction after the garments have been rearranged.
Constraints
- when