#include <bits/stdc++.h>
int main(void) {
  int n = 0;
  scanf("%d", &n);
  int A[200000];
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  int ans = -1;
  int second = A[0];
  int update = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (i >= 2) {
      if (second < A[i] && A[i] < ans) {
        second = A[i];
      }
    }
    if (ans == A[i]) {
      cnt++;
    }
    if (ans < A[i]) {
      ans = A[i];
      update = i;
      cnt = 0;
    }
  }
  for (int i = 0; i < update; i++) {
    printf("%d\n", ans);
  }
  if (cnt > 0) {
    printf("%d\n", ans);
  } else {
    printf("%d\n", second);
  }
  for (int i = update + 1; i < n; i++) {
    printf("%d\n", ans);
  }
  return 0;
}
