#P2769. Monkey Tree Assignment
Monkey Tree Assignment
Monkey Tree Assignment
In a monkey village, there is a narrow and straight mountain road. There are n monkeys standing at distinct positions along the road. Their positions are given by positive integers \(X_i\) (for \(1 \le i \le n\)). Along the road, there are also m tall trees planted at distinct positions given by positive integers \(Y_j\) (for \(1 \le j \le m\)). When a tiger appears, the monkeys need to climb the trees to escape. In order to use the trees most effectively, every tree must have at least one monkey on it.
When a monkey at position \(a\) climbs a tree at position \(b\), it consumes energy equal to \(|a-b|\). All monkeys must climb a tree. Your task is to assign each of the \(n\) monkeys to one of the \(m\) trees, with the constraint that every tree must have at least one monkey, in order to minimize the total energy consumed.
Note: It is guaranteed that \(m \le n\).
inputFormat
The first line contains two integers \(n\) and \(m\) separated by a space.
The second line contains \(n\) distinct positive integers \(X_1, X_2, \dots, X_n\) representing the positions of the monkeys.
The third line contains \(m\) distinct positive integers \(Y_1, Y_2, \dots, Y_m\) representing the positions of the trees.
outputFormat
Output a single integer, which is the minimum total energy required for all monkeys to climb a tree with each tree having at least one monkey.
sample
3 2
1 4 6
3 5
4