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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <vector>

int task_C (std::vector<unsigned int>& sequence)
{
	// variables for this function
	register unsigned int maxval_1st;
	register unsigned int maxval_2nd;
	register unsigned int maxval_answer;

	// STEP.01
	// initialize the variables to store the maximum values of given sequence
	maxval_1st = 0U;
	maxval_2nd = 0U;

	// STEP.02
	// search the maximum values of given sequence
	for (register auto itr = sequence.begin(); itr != sequence.end(); itr++)
	{
		if (maxval_1st < *itr)
		{
			maxval_2nd = maxval_1st;
			maxval_1st = *itr;
		}
		else if (maxval_2nd < *itr)
		{ maxval_2nd = *itr; }
		else
		{ continue; }
	}

	// STEP.02
	// output the answer of this task
	for (register auto itr = sequence.begin(); itr != sequence.end(); itr++)
	{
		if (*itr != maxval_1st) { maxval_answer = maxval_1st; }
		else                    { maxval_answer = maxval_2nd; }

		std::cout << maxval_answer << std::endl;
	}

	// STEP.END
	return EXIT_SUCCESS;
}

int main (void)
{
	// variables for main process
	register unsigned int buffer_input;
	static   unsigned int N;

	// instances for this function
	std::vector<unsigned int> A;

	// STEP.01
	// read out the given data
	// (the length of the given sequence)
	std::cin >> N;

	// STEP.02
	// read out the given data
	// (the values of the given sequence)
	for (register size_t itr = 0; itr < N; itr++)
	{
		std::cin >> buffer_input;
		A.push_back(buffer_input);
	}

	// STEP.03
	// calculate & output the answer of this task
	task_C (A);

	// STEP.END
	return EXIT_SUCCESS;
}