Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Distance II

Your task is to calculate the distance between two $n$ dimensional vectors $x = \{x_1, x_2, ..., x_n\}$ and $y = \{y_1, y_2, ..., y_n\}$.

The Minkowski's distance defined below is a metric which is a generalization of both the Manhattan distance and the Euclidean distance.

\[
D_{xy} = (\sum_{i=1}^n |x_i - y_i|^p)^{\frac{1}{p}}
\]

It can be the Manhattan distance

\[
D_{xy} = |x_1 - y_1| + |x_2 - y_2| + ... + |x_n - y_n|
\]

where $p = 1 $.

It can be the Euclidean distance

\[
D_{xy} = \sqrt{(|x_1 - y_1|)^{2} + (|x_2 - y_2|)^{2} + ... + (|x_n - y_n|)^{2}}
\]

where $p = 2 $.

Also, it can be the Chebyshev distance

\[
D_{xy} = max_{i=1}^n (|x_i - y_i|)
\]

where $p = \infty$

Write a program which reads two $n$ dimensional vectors $x$ and $y$, and calculates Minkowski's distance where $p = 1, 2, 3, \infty$ respectively.

Input

In the first line, an integer $n$ is given. In the second and third line, $x = \{x_1, x_2, ... x_n\}$ and $y = \{y_1, y_2, ... y_n\}$ are given respectively. The elements in $x$ and $y$ are given in integers.

Output

Print the distance where $p = 1, 2, 3$ and $\infty$ in a line respectively.
The output should not contain an absolute error greater than 10-5.

Constraints

$1 \leq n \leq 100$

$0 \leq x_i, y_i \leq 1000$

Sample Input

3
1 2 3
2 0 4

Sample Output

4.000000
2.449490
2.154435
2.000000

Solution in JAVA (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class Main{

	public static void main(String[] args) throws IOException{

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int num = Integer.parseInt(br.readLine());
		int[] x = new int[num];
		int[] y = new int[num];
		String[] x_token = br.readLine().split(" ");
		String[] y_token = br.readLine().split(" ");

		for(int i = 0; i < num; i++){
			x[i] = Integer.parseInt(x_token[i]);
			y[i] = Integer.parseInt(y_token[i]);
		}

		double[] ans = new double[4];

		for(int i = 0; i < 3; i++){

			double sum = 0;
			double max = 0;

			for(int j = 0; j < num; j++){
				sum = sum + Math.pow(Math.abs(x[j] - y[j]), i + 1);
				max = Math.max(max, Math.abs(x[j] - y[j]));
			}

			switch(i){
				case 0:
					ans[i] = sum;
					break;
				case 1:
					ans[i] = Math.sqrt(sum);
					break;
				case 2:
					ans[i] = Math.cbrt(sum);
					break;
			}

			ans[3] = max;

		}

		for(int i = 0; i < ans.length; i++){
			System.out.printf("%.8f\n", ans[i]);
		}

	}
}