Hosting the Contest
Time limit1sMemory limit1024 MB
Pick one difficulty from each of N groups and order them so the sum of adjacent absolute differences is as small as possible.
- Level
Medium7 of 10
- Topics
- Two pointers, Sliding window, Sorting
- Solved
- No attempts yet
Problem
Yongbeom is writing problems for Boramcup. He made problems for each of different algorithm types, so problems in total. The difficulty of the -th problem of the -th algorithm is . The contest does not have time for all of them, so he will use exactly one problem from each of the algorithm types, problems in total.
Yongbeom also wants to keep the difficulty curve small so that difficulty does not jump sharply. If the difficulty of the -th problem in the contest is , the difficulty curve is . When , the difficulty curve is defined as .
Help Yongbeom. Arrange the problems in a suitable order and find the minimum possible difficulty curve.
Input
The first line contains two integers and , the number of algorithm types and the number of problems made for each type, separated by a space.
The next lines contain the difficulties. On line , the integers are given, separated by spaces, where is the difficulty of the -th problem of the -th algorithm.
Output
Print the minimum difficulty curve when the problems are arranged in a suitable order.