#P6934. Posterized Red Channel Quantization

    ID: 20141 Type: Default 1000ms 256MiB

Posterized Red Channel Quantization

Posterized Red Channel Quantization

In digital images, each pixel's red intensity is an integer from \(0\) to \(255\). In a posterize operation, rather than allowing all 256 possible values, at most \(k\) distinct integers are allowed. Each pixel's original red value \(r_i\) is replaced by the allowed integer that is nearest to it. The total sum of squared errors introduced is

i=1nmin1jk(rivj)2,\sum_{i=1}^n \min_{1 \le j \le k} (r_i - v_j)^2,

where the \(v_j\)'s are the chosen allowed integers. Given the parameter \(k\) and the image's red intensities, your task is to choose \(k\) allowed integers (each in \(0 \ldots 255\)) so that the above sum is minimized, and output this minimum sum of squared errors.

Note: For each group (or segment) of pixels that are approximated by one allowed value, the best choice is the integer that minimizes the squared error over that segment. This optimal value is the nearest integer to the arithmetic mean of the values in that segment.

inputFormat

The first line contains two integers \(k\) and \(n\) (\(1 \le k \le 256\), \(1 \le n \le 10000\)), where \(k\) is the maximum number of allowed red intensity values and \(n\) is the number of pixels.

The second line contains \(n\) integers, each in the range \(0\) to \(255\), representing the red intensity of each pixel.

outputFormat

Output a single integer: the minimum achievable sum of squared errors after replacing each pixel's red intensity with the nearest of the chosen \(k\) allowed integers.

sample

1 3
100 150 200
5000