Under construction

Still being built. Things may change

Line of Best Fit

Interview

Time limit1sMemory limit128 MB

Summary
Given n points in the plane, compute the slope and intercept of the least squares regression line and print each rounded to three decimals.
Level

Easy3 of 10

Topics
Math, Implementation, Prefix sum, Array
Solved
No attempts yet

Problem

Finding a line that best fits a set of data points in the plane is one of the fundamental problems in statistics and numerical analysis.

You are given a set PP of nn points in the plane, (x1,y1),(x2,y2),…,(xn,yn)(x_1, y_1), (x_2, y_2), \dots, (x_n, y_n). For a line LL defined by the linear equation y=ax+by = ax + b, the error of LL with respect to PP is defined as the sum of the squared vertical distances from the points of PP to the line:

Error(L,P)=∑i=1n(yi−axi−b)2\text{Error}(L, P) = \sum_{i=1}^{n} (y_i - a x_i - b)^2

The least squares method finds the line LL that minimizes this error. Write a program that, for a given set of points PP, computes the coefficients aa and bb of the line LL that minimizes the error.

Figure 1. An example of a set of points and its line of best fit.

Input

The first line contains the number of points nn (1≤n≤1,0001 \le n \le 1{,}000). Each of the next nn lines contains two integers xix_i and yiy_i, the coordinates of a point, separated by a space (∣xi∣≤106|x_i| \le 10^6, ∣yi∣≤106|y_i| \le 10^6). It is guaranteed that the xx-coordinates are not all identical, so the line of best fit is uniquely determined.

Output

Print the value of aa on the first line and the value of bb on the second line. Both values must be rounded to three decimal places.

Examples4

  1. Example 1

    Input
    4
    1 6
    2 5
    3 7
    4 10
    
    Expected output
    1.400
    3.500
    
  2. Example 2

    Input
    2
    1 3
    3 7
    
    Expected output
    2.000
    1.000
    
  3. Example 3

    Input
    4
    0 1
    1 1
    2 4
    3 5
    
    Expected output
    1.500
    0.500
    
  4. Example 4

    Input
    5
    -2 -3
    -1 -1
    0 2
    1 3
    2 5
    
    Expected output
    2.000
    1.200