ACM ICPC World Finals 2009
Let us first consider the special case of using a single fence. To compute the fence length needed, one computes the convex hull of the set of trees. It is then not hard to see that the minimum total fence length will be the total length of the perimeter of the convex hull, plus the circumference of a circle of radius M.
To finish the problem we now need to find a good way of partioning the set of trees into disjoint parts, each of which will be surrounded by a single fence (the perimeter of which can be computed as described above). Because of the small number of trees, an optimal such partition can be computed in a brute-force manner. In order to make it fast enough, one may need to use dynamic programming to remember, for a given set S of trees, what the minimum fence length for S is.