Under construction

Still being built. Things may change

Data Analysis

Interview

Time limit3sMemory limit1024 MB

Summary
Split N points, sorted by x, into K contiguous groups so the total absolute deviation of y from one horizontal line per group is minimized.
Level

Medium5 of 10

Topics
Dynamic programming, Sorting, Heap
Solved
No attempts yet

Problem

Alice, a data scientist, is asked to analyze data. Each data point is a 2D coordinate (x,y)(x, y).

The analysis finds KK line segments parallel to the xx-axis in the 2D plane. Specifically, you must determine the following values.

  • The xx-coordinates of the segment endpoints 0=p0<p1<⋯<pK=50 0000=p_0 < p_1 < \cdots < p_K = 50\ 000. The endpoints of the ii-th segment are pi−1p_{i-1} and pip_i. Here 1≤i≤K1 \leq i \leq K.
  • The yy-coordinates of the segments m1,m2,⋯ ,mKm_1, m_2, \cdots, m_K.

You cannot choose the values of m1,⋯ ,mKm_1, \cdots, m_K arbitrarily. To improve accuracy, an error is defined, and the values are chosen to minimize it. The error is defined as follows.

  • The error of a data point (x,y)(x, y) is ∣y−mi∣|y - m_i|, where ii satisfies pi−1≤x<pip_{i-1} \leq x < p_i.
  • The total error is the sum of the errors of all data points.

Alice wants to choose p0,⋯ ,pKp_0, \cdots, p_K and m1,⋯ ,mKm_1, \cdots, m_K so that the error is minimized. Find the minimum possible error when these values are chosen appropriately.

Input

The first line contains the number of data points NN and the number of segments to find KK, separated by a space.

The next NN lines each contain a data point (x,y)(x, y), separated by a space. All xx values are distinct.

Output

Print the minimum analysis error when p0,⋯ ,pKp_0, \cdots, p_K and m1,⋯ ,mKm_1, \cdots, m_K are chosen appropriately. An absolute or relative error of up to 10−410^{-4} is accepted.

Constraints

  • The input values are all integers.
  • 1≤N≤3 0001\leq N \leq 3\,000
  • 1≤K≤min⁡(N,10)1\leq K \leq \min(N, 10)
  • 0≤x<50 0000\leq x < 50\,000
  • 0≤y≤1 0000\leq y \leq 1\,000

Examples1

  1. Example 1

    Input
    6 2
    1 1
    2 2
    3 3
    4 4
    5 5
    6 6
    
    Expected output
    4.0000