Permanent-magnet systems are widely used for generation of magnetic fields with specific properties. The reciprocity theorem, an energy-equivalence principle in magnetostatics, can be employed to calculate the optimal remanent flux density of the permanent-magnet system, given any objective functional that is linear in the magnetic field. This approach, however, yields a continuously varying remanent flux density, while in practical applications, magnetic assemblies are realized by combining uniformly magnetized segments. The problem of determining the optimal shape of each of these segments remains unsolved. We show that the problem of optimal segmentation of a two-dimensional permanent-magnet assembly with respect to a linear objective functional can be reduced to the problem of piecewise linear approximation of a plane curve by perimeter maximization. Once the problem has been cast into this form, the globally optimal solution can be easily computed employing dynamic programming.