Problem:
Score : 200 points

Problem Statement

It is only six months until Christmas, and AtCoDeer the reindeer is now planning his travel to deliver gifts.

There are N houses along TopCoDeer street. The i-th house is located at coordinate a_i. He has decided to deliver gifts to all these houses.

Find the minimum distance to be traveled when AtCoDeer can start and end his travel at any positions.

Constraints

1 ≤ N ≤ 100

0 ≤ a_i ≤ 1000

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum distance to be traveled.

Sample Input 1

4
2 3 7 9

Sample Output 1

7

The travel distance of 7 can be achieved by starting at coordinate 9 and traveling straight to coordinate 2.

It is not possible to do with a travel distance of less than 7, and thus 7 is the minimum distance to be traveled.

Sample Input 2

8
3 1 4 1 5 9 2 6

Sample Output 2

8

There may be more than one house at a position.

Solution in Go (1.6) (judge verdict: Runtime Error)

package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	var n int
	fmt.Fscanf(os.Stdin, "%d", &n)
	var input string
	fmt.Fscanf(os.Stdin, "%s", &input)
	nums := make([]int, n)
	dists := make([]int, n-1)
	for i, v := range strings.Split(input, " ") {
		nums[i], _ = strconv.Atoi(v)
	}

	start, end := 1001, 0
	for i := 0; i < n-1; i++ {
		tmp := nums[i] - nums[i+1]
		if tmp < 0 {
			tmp *= -1
		}
		dists[i] = tmp
		if start > nums[i] {
			start = nums[i]
		}
		if start > nums[i+1] {
			start = nums[i+1]
		}
		if end < nums[i] {
			end = nums[i]
		}
		if end < nums[i+1] {
			end = nums[i+1]
		}
	}

	var startPos []int
	var endPos []int
	for i, v := range nums {
		if v == start {
			startPos = append(startPos, i)
		}
		if v == end {
			endPos = append(endPos, i)
		}
	}

	minDist := 1000
	for _, i := range startPos {
		for _, j := range endPos {
			d := 0
			for _, v := range dists[i:j] {
				d += v
			}
			if minDist > d {
				minDist = d
			}
		}
	}

	fmt.Println(minDist)
}