Problem:
Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <math.h>

/**
 * 右と左を入れ替える関数。
 */
void swap_right_left( int *a, int *b ){
    int temp = 0;
    
    temp = *a;
    *a = *b;
    *b = temp;
}

/**
 * クイックソートする関数。
 */
int quick_sort( int sum[], int left, int right){
    int result = 0;
    int pivot;
    int Left, Right;

    Left = left, Right = right;

    // 基準値を設ける
    pivot = sum[( right + left ) / 2 ];

    while( 1 ){
        // 左から参照（中央値より小さい間は要素数をインクリメント）
        while( sum[Left] < pivot ){
            Left++;
        }

        // 右から参照（中央値より大きい間は要素数をデクリメント）
        while( pivot < sum[Right] ){
            Right--;
        }


        // 左と右のカーソルが重なったらループを抜ける
        if( Left >= Right ){
            break;
        }

        // rightとleftを入れ替え
        swap_right_left( &sum[Left], &sum[Right] );
        Right--;
        Left++;
    }
    // 左のグループを対象にクイックソートを再帰
    if( left < Left - 1 ){
        quick_sort( sum, left, ( Left - 1 ) );
    }

    if( Right + 1 < right ){
        quick_sort( sum, (Right + 1), right );
    }

    // 処理終了
    result = 1;

    return result;
}

int main(void){
    // 変数宣言
    int n;
    int x[100];
    int sum[100];   // 消費体力の総和がpパターンごとに格納される
    int i, j;
    int p = 100;    // パターン
    int result = 0;
    
    // 入力
    scanf("%d", &n);

    for( i = 0 ; i < n ; i++ ){
        scanf( "%d", &x[i] );
    }

    //初期化
    for( i = 0 ; i < p ; i++ ){
        sum[i] = 0;
    }

    // 消費体力の総和をpパターン毎に格納
    for( j = 0 ; j < p ; j++ ){
        for( i = 0; i < n ; i++ ){
            sum[j] += pow( x[i] - j , 2.0 ); 
        }
    }

    // 最小値を出す
    result = quick_sort( sum, 0, (p - 1) );

    // 出力
    printf( "%d", sum[0] );
    
    return 0;
}