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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace abc134_c
{
    class Program
    {
        static void Main(string[] args)
        {
            
            var N = int.Parse(Console.ReadLine());
            var A = new List<int>();

            for (int i = 0; i < N; i++)
            {
                A.Add(int.Parse(Console.ReadLine()));
            }
            for (int i = 0; i < N; i++) 
            {
                var pq = new PriorityQueue<int>();
                for (int j = 0; j < N; j++)
                {
                    if (j != i) pq.Push(A[j]);
                }

                Console.WriteLine(pq.Top);

            }

        }

        /// <summary>
        /// 優先度付き待ち行列
        /// </summary>
        /// <typeparam name="T">要素の型</typeparam>
        class PriorityQueue<T>
          where T : IComparable<T>
        {
            #region フィールド

            public IList<T> buffer;

            #endregion
            #region 初期化

            public PriorityQueue() { this.buffer = new List<T>(); }
            public PriorityQueue(int capacity) { this.buffer = new List<T>(capacity); }

            #endregion
            #region ヒープ操作

            /// <summary>
            /// ヒープ化されている配列リストに新しい要素を追加する。
            /// </summary>
            /// <param name="array">対象の配列リスト</param>
            static void PushHeap(IList<T> array, T elem)
            {
                int n = array.Count;
                array.Add(elem);

                while (n != 0)
                {
                    int i = (n - 1) / 2;
                    if (array[n].CompareTo(array[i]) > 0)
                    {
                        T tmp = array[n]; array[n] = array[i]; array[i] = tmp;
                    }
                    n = i;
                }
            }

            /// <summary>
            /// ヒープから最大値を削除する。
            /// </summary>
            /// <param name="array">対象の配列リスト</param>
            static void PopHeap(IList<T> array)
            {
                int n = array.Count - 1;
                array[0] = array[n];
                array.RemoveAt(array.Count - 1);

                for (int i = 0, j; (j = 2 * i + 1) < n;)
                {
                    if ((j != n - 1) && (array[j].CompareTo(array[j + 1]) < 0))
                        j++;
                    if (array[i].CompareTo(array[j]) < 0)
                    {
                        T tmp = array[j]; array[j] = array[i]; array[i] = tmp;
                    }
                    i = j;
                }
            }

            #endregion
            #region 要素の挿入・削除

            /// <summary>
            /// 要素のプッシュ。
            /// </summary>
            /// <param name="elem">挿入したい要素</param>
            public void Push(T elem)
            {
                PushHeap(this.buffer, elem);
            }

            /// <summary>
            /// 要素を1つポップ。
            /// </summary>
            /// <remarks>
            /// 今回の実装では、先頭要素の読み出しと削除は別に行う。
            /// この Pop では削除のみ。
            /// 読み出しには Top プロパティを使う。
            /// </remarks>
            public void Pop()
            {
                PopHeap(this.buffer);
            }

            /// <summary>
            /// 先頭要素の読み出し。
            /// </summary>
            public T Top
            {
                get { return this.buffer[0]; }
            }

            public int Count
            {
                get { return this.buffer.Count; }
            }

            #endregion
        }
    }
}