Problem:
Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5

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

package main

import (
	"fmt"
	"math"
	"sort"
)

var N int
var A []int

func binarySearch(a []int, k int) (right int) {
	n := len(a)
	left := -1
	right = n - 1
	for right-left > 1 {
		mid := (left + right) / 2
		if a[mid] >= k {
			right = mid
		} else {
			left = mid
		}
	}
	return right
}

func main() {
	fmt.Scan(&N)
	A = make([]int, N)
	max_for_all := -1
	for i := 0; i < N; i++ {
		fmt.Scan(&A[i])
		if max_for_all < A[i] {
			max_for_all = A[i]
		}
	}
	B := make([]int, N)
	for i := 0; i < N; i++ {
		B[i] = A[i]
	}
	sort.Ints(B)
	for i := 0; i < N; i++ {
		res := max_for_all
		if A[i] == max_for_all {
			idx := binarySearch(B, A[i])
			if idx == 0 {
				idx++
			}
			if idx < len(B) {
				if B[idx] == B[idx+1] {
					fmt.Println(B[idx])
					continue
				}
			}
			fmt.Println(B[idx-1])
			continue
		}
		fmt.Println(res)
	}
}

var dy = []int{0, 1, 0, -1}
var dx = []int{1, 0, -1, 0}
var inf = int(1e9)

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

func lcm(a, b int) int {
	return a * b / gcd(a, b)
}
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Pair struct {
	a, b int
}

type pairs []Pair

func (p pairs) Len() int {
	return len(p)
}
func (p pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p pairs) Less(i, j int) bool {
	if p[i].a == p[j].a {
		return p[i].b < p[j].b
	}
	return p[i].a < p[j].a
}