#include <bits/stdc++.h>
#define my_max(a, b) (a > b ? a : b)
#define my_min(a, b) (a < b ? a : b)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i, s, t) for(int i = (int)(s); i <= (int)(t); i++)
#define rev(i, t, s) for(int i = (int)(t); i >= (int)(s); i--)
#define lson rt << 1
#define rson rt << 1 | 1
#define sz(x) (int)(x).size()

typedef long long ll;
typedef long double lb;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
ll gcd(ll x, ll y){ return y % x == 0 ? x : gcd(y % x, x); }
template<class T>T my_abs(T a){ if(a < 0) a = -a; return a; }

const int maxn = 2e5 + 7;
int a[maxn], l[maxn], r[maxn], n, ans[maxn];
int main(){
#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    scanf("%d", &n);
    rep(i, 1, n) scanf("%d", a + i);
    std::stack<int> s;
    rep(i, 1, n){
        while(s.size() && a[s.top()] >= a[i]) s.pop();
        if(s.size()) l[i] = s.top() + 1;
        else l[i] = 1;
        s.push(i);
    }
    while(s.size()) s.pop();
    rev(i, n, 1){
        while(s.size() && a[s.top()] >= a[i]) s.pop();
        if(s.size()) r[i] = s.top() - 1;
        else r[i] = n;
        s.push(i);
    }
    rep(i, 1, n){
        int c = r[i] - l[i] + 1;
        //printf("%d %d\n", l[i], r[i]);
        ans[c] = my_max(ans[c], a[i]);
    }
    rev(i, n, 1) ans[i] = my_max(ans[i], ans[i + 1]);
    rep(i, 1, n) printf("%d%c", ans[i], i == n ? '\n' : ' ');
    return 0;
}


/*
 * Matheus Oliveira
 * 22:13:37 13/03/2019
 * 548D.cpp
*/

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF 2e9
#define MAXL 20
#define MAXN 200100
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, ini, fim) printf("%s: ", #x); for(int i = ini; i <= fim; i++) printf("%d%c", x[i], (i == fim) ? '\n' : ' ');
typedef long long ll;
typedef pair<int, int> pii;

int lg[MAXN], spt[MAXL][MAXN], elements[MAXN], n, ans[MAXN];

void build() {
	int aux, val;

	for(int i = 1; i <= n; i++) {
		aux = i;
		val = 0;

		while( (aux >> 1) > 0) val++, aux >>= 1;
		lg[i] = val;
	}

	for(int i = 0; i < n; i++) spt[0][i] = elements[i];

	for(int j = 1; j <= lg[n]; j++) {
		for(int i = 0; (i + (1 << (j-1)) ) < n; i++) {
			spt[j][i] = min(spt[j-1][i], spt[j-1][i + (1 << (j-1))]);
		}
	}
}

int query(int l, int r) {
	int elementosNoIntervalo = r-l+1;
	return min( spt[ lg[elementosNoIntervalo] ][l], spt[ lg[elementosNoIntervalo] ][ r - (1 << lg[elementosNoIntervalo]) + 1 ] );
}

vector<pii> options;

bool compare(pii f, pii s) {
	if(f.first == s.first) return f.second >= s.second;
	return f.first < s.first;
}

int main() {
	int left, mid, right, aux, r1, r2, possible, l, r, actual;
	scanf("%d", &n);

	for(int i = 0; i < n; i++) scanf("%d", &elements[i]);
	build();

	for(int i = 0; i < n; i++) {
		left = 1;
		right = n;

		while(left <= right) {
			mid = (left + right) >> 1;

			actual = -INF;
			for(int j = i - mid + 1; j <= i; j++) {
				l = min(j, j + mid - 1);
				r = max(j, j + mid - 1);

				if(l >= 0 and r < n) {
					aux = query(l, r);
					if(aux == elements[i]) {
						actual = elements[i];
						break;
					}
				}
			}

			//printf("mid: %d, actual: %d\n", mid, actual);

			if(actual == elements[i]) {
				possible = mid;
				left = mid+1;
			}
			else right = mid-1;
		}

		options.pb( mp(possible, elements[i]) );
	}

	//debugarr(stats, 0, n-1);
	sort(options.begin(), options.end(), compare);

	int p = 0;
	for(int i = 1; i <= n; i++) {
		while( options[p].first < i ) p++;
		ans[i] = options[p].second;
	}

	for(int i = 1; i <= n; i++) {
		if(i != 1) printf(" ");
		printf("%d", ans[i]);
	}
	printf("\n");

	return 0;
}


/*
 * Matheus Oliveira
 * 22:13:37 13/03/2019
 * 548D.cpp
*/

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF 2e9
#define MAXL 20
#define MAXN 200100
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, ini, fim) printf("%s: ", #x); for(int i = ini; i <= fim; i++) printf("%d%c", x[i], (i == fim) ? '\n' : ' ');
typedef long long ll;
typedef pair<int, int> pii;

vector< pii > options;
int lg[MAXN], spt[MAXL][MAXN], elements[MAXN], n, ans[MAXN];

void build() {
	int aux, val;

	for(int i = 1; i <= n; i++) {
		aux = i;
		val = 0;

		while( (aux >> 1) > 0) val++, aux >>= 1;
		lg[i] = val;
	}

	for(int i = 0; i < n; i++) spt[0][i] = elements[i];

	for(int j = 1; j <= lg[n]; j++) {
		for(int i = 0; (i + (1 << (j-1)) ) < n; i++) {
			spt[j][i] = min(spt[j-1][i], spt[j-1][i + (1 << (j-1))]);
		}
	}
}

int query(int l, int r) {
	if(l > r) swap(l, r);
	int elementosNoIntervalo = r-l+1;
	return min( spt[ lg[elementosNoIntervalo] ][l], spt[ lg[elementosNoIntervalo] ][ r - (1 << lg[elementosNoIntervalo]) + 1 ] );
}

bool compare(pii f, pii s) {
	if(f.first == s.first) return f.second >= s.second;
	return f.first > s.first;
}

int bs(int idx, bool checkLeft) {
	int left, right, mid, resp = 0;

	if(checkLeft) left = 0, right = idx - 1;
	else left = idx + 1, right = n-1;

	while(left <= right) {
		mid = (left + right) >> 1;

		if( query(idx, mid) == elements[idx] ) {
			resp = abs(idx - mid);
			left = mid + 1;
		}
		else right = mid - 1;
	}

	return resp;
}

int main() {
	int resp, aleft, aright;
	scanf("%d", &n);

	for(int i = 0; i < n; i++) scanf("%d", &elements[i]);
	build();

	for(int i = 0; i < n; i++) {
		aleft = bs(i, true);
		aright = bs(i, false);

		resp = 1 + aleft + aright;
		//printf("resp para %d foi %d. left: %d, right: %d\n", elements[i], resp, aleft, aright);
		options.pb( mp(resp, elements[i]) );
	}

	sort(options.begin(), options.end(), compare);

	int last = -1, p = 0;
	for(int i = n; i >= 1; i--) {
		while(p < n) {
			if(options[p].first >= i) {
				last = max(last, options[p].second);
				p++;
			}
			else break;
		}

		ans[i] = last;
	}

	for(int i = 1; i <= n; i++) {
		if(i != 1) printf(" ");
		printf("%d", ans[i]);
	}
	printf("\n");

	return 0;
}


/*
 * Matheus Oliveira
 * 22:13:37 13/03/2019
 * 548D.cpp
*/

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF 2e9
#define MAXL 20
#define MAXN 200100
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, ini, fim) printf("%s: ", #x); for(int i = ini; i <= fim; i++) printf("%d%c", x[i], (i == fim) ? '\n' : ' ');
typedef long long ll;
typedef pair<int, int> pii;

int lg[MAXN], spt[MAXL][MAXN], elements[MAXN], n, ans[MAXN];

void build() {
	int aux, val;

	for(int i = 1; i <= n; i++) {
		aux = i;
		val = 0;

		while( (aux >> 1) > 0) val++, aux >>= 1;
		lg[i] = val;
	}

	for(int i = 0; i < n; i++) spt[0][i] = elements[i];

	for(int j = 1; j <= lg[n]; j++) {
		for(int i = 0; (i + (1 << (j-1)) ) < n; i++) {
			spt[j][i] = min(spt[j-1][i], spt[j-1][i + (1 << (j-1))]);
		}
	}
}

int query(int l, int r) {
	int elementosNoIntervalo = r-l+1;
	return min( spt[ lg[elementosNoIntervalo] ][l], spt[ lg[elementosNoIntervalo] ][ r - (1 << lg[elementosNoIntervalo]) + 1 ] );
}

vector<pii> options;

bool compare(pii f, pii s) {
	if(f.first == s.first) return f.second >= s.second;
	return f.first < s.first;
}

int main() {
	int left, mid, right, aux, r1, r2, possible, l, r, actual;
	scanf("%d", &n);

	for(int i = 0; i < n; i++) scanf("%d", &elements[i]);
	build();

	for(int i = 0; i < n; i++) {
		left = 1;
		right = n;

		while(left <= right) {
			mid = (left + right) >> 1;

			actual = -INF;
			for(int j = i - mid + 1; j <= i; j++) {
				l = min(j, j + mid - 1);
				r = max(j, j + mid - 1);

				if(l >= 0 and r < n) {
					aux = query(l, r);
					actual = max(actual, aux);
				}
			}

			//printf("mid: %d, actual: %d\n", mid, actual);

			if(actual == elements[i]) {
				possible = mid;
				left = mid+1;
			}
			else right = mid-1;
		}

		options.pb( mp(possible, elements[i]) );
	}

	//debugarr(stats, 0, n-1);
	sort(options.begin(), options.end(), compare);

	int p = 0;
	for(int i = 1; i <= n; i++) {
		while( options[p].first < i ) p++;
		ans[i] = options[p].second;
	}

	for(int i = 1; i <= n; i++) {
		if(i != 1) printf(" ");
		printf("%d", ans[i]);
	}
	printf("\n");

	return 0;
}


/*
 * Matheus Oliveira
 * 22:13:37 13/03/2019
 * 548D.cpp
*/

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF 2e9
#define MAXL 20
#define MAXN 200100
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, ini, fim) printf("%s: ", #x); for(int i = ini; i <= fim; i++) printf("%d%c", x[i], (i == fim) ? '\n' : ' ');
typedef long long ll;
typedef pair<int, int> pii;

int lg[MAXN], spt[MAXL][MAXN], elements[MAXN], n, ans[MAXN], stats[MAXN];

void build(bool flag) {
	int aux, val;

	for(int i = 1; i <= n; i++) {
		aux = i;
		val = 0;

		while( (aux >> 1) > 0) val++, aux >>= 1;
		lg[i] = val;
	}

	for(int i = 0; i < n; i++) {
		if(not flag) spt[0][i] = elements[i];
		else spt[0][i] = stats[i];
	}

	for(int j = 1; j <= lg[n]; j++) {
		for(int i = 0; (i + (1 << (j-1)) ) < n; i++) {
			spt[j][i] = min(spt[j-1][i], spt[j-1][i + (1 << (j-1))]);
		}
	}
}

int query(int l, int r) {
	int elementosNoIntervalo = r-l+1;
	return min( spt[ lg[elementosNoIntervalo] ][l], spt[ lg[elementosNoIntervalo] ][ r - (1 << lg[elementosNoIntervalo]) + 1 ] );
}

bool compare(pii f, pii s) {
	if(f.first == s.first) return f.second >= s.second;
	return f.first < s.first;
}

int main() {
	int left, mid, right, aux, r1, r2, possible, l, r, actual;
	scanf("%d", &n);

	for(int i = 0; i < n; i++) scanf("%d", &elements[i]);
	build(false);

	for(int i = 0; i < n; i++) {
		left = 1;
		right = n;

		while(left <= right) {
			mid = (left + right) >> 1;

			actual = -INF;
			for(int j = i - mid + 1; j <= i; j++) {
				l = min(j, j + mid - 1);
				r = max(j, j + mid - 1);

				if(l >= 0 and r < n) {
					aux = query(l, r);
					if(aux == elements[i]) {
						actual = elements[i];
						break;
					}
				}
			}

			//printf("mid: %d, actual: %d\n", mid, actual);

			if(actual == elements[i]) {
				possible = mid;
				left = mid+1;
			}
			else right = mid-1;
		}

		//printf("i: %d, possible: %d\n", i, possible);
		stats[ possible-1 ] = max(stats[possible-1], elements[i]);
	}

	//debugarr(stats, 0, n-1);

	int mini = stats[n-1];
	for(int i = n-2; i >= 0; i--) {
		if(stats[i] != 0) mini = max(mini, stats[i]);
		stats[i] = mini;
	}

	//debugarr(stats, 0, n-1);

	build(true);
	for(int i = 1; i <= n; i++) ans[i] = query(0, i-1);

	for(int i = 1; i <= n; i++) {
		if(i != 1) printf(" ");
		printf("%d", ans[i]);
	}
	printf("\n");

	return 0;
}


/*
 * Matheus Oliveira
 * 22:13:37 13/03/2019
 * 548D.cpp
*/

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF 2e9
#define MAXL 20
#define MAXN 200100
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, ini, fim) printf("%s: ", #x); for(int i = ini; i <= fim; i++) printf("%d%c", x[i], (i == fim) ? '\n' : ' ');
typedef long long ll;
typedef pair<int, int> pii;

vector< pii > options;
int lg[MAXN], spt[MAXL][MAXN], elements[MAXN], n, ans[MAXN];

void build() {
	int aux, val;

	for(int i = 1; i <= n; i++) {
		aux = i;
		val = 0;

		while( (aux >> 1) > 0) val++, aux >>= 1;
		lg[i] = val;
	}

	for(int i = 0; i < n; i++) spt[0][i] = elements[i];

	for(int j = 1; j <= lg[n]; j++) {
		for(int i = 0; (i + (1 << (j-1)) ) < n; i++) {
			spt[j][i] = min(spt[j-1][i], spt[j-1][i + (1 << (j-1))]);
		}
	}
}

int query(int l, int r) {
	if(l > r) swap(l, r);
	int elementosNoIntervalo = r-l+1;
	return min( spt[ lg[elementosNoIntervalo] ][l], spt[ lg[elementosNoIntervalo] ][ r - (1 << lg[elementosNoIntervalo]) + 1 ] );
}

bool compare(pii f, pii s) {
	if(f.first == s.first) return f.second >= s.second;
	return f.first > s.first;
}

int bs(int idx, bool checkLeft) {
	int left, right, mid, resp = 0;

	if(checkLeft) left = 0, right = idx - 1;
	else left = idx + 1, right = n-1;

	while(left <= right) {
		mid = (left + right) >> 1;

		if( query(idx, mid) == elements[idx] ) {
			resp = abs(idx - mid);

			if(checkLeft) right = mid - 1;
			else left = mid + 1;
		}
		else {
			if(checkLeft) left = mid + 1;
			else right = mid - 1;
		}
	}

	return resp;
}

int main() {
	int resp, aleft, aright;
	scanf("%d", &n);

	for(int i = 0; i < n; i++) scanf("%d", &elements[i]);
	build();

	for(int i = 0; i < n; i++) {
		aleft = bs(i, true);
		aright = bs(i, false);

		resp = 1 + aleft + aright;
		//printf("resp para %d foi %d. left: %d, right: %d\n", elements[i], resp, aleft, aright);
		options.pb( mp(resp, elements[i]) );
	}

	sort(options.begin(), options.end(), compare);

	int last = -1, p = 0;
	for(int i = n; i >= 1; i--) {
		while(p < n) {
			if(options[p].first >= i) {
				last = max(last, options[p].second);
				p++;
			}
			else break;
		}

		ans[i] = last;
	}

	for(int i = 1; i <= n; i++) {
		if(i != 1) printf(" ");
		printf("%d", ans[i]);
	}
	printf("\n");

	return 0;
}


/*
 * Matheus Oliveira
 * 22:13:37 13/03/2019
 * 548D.cpp
*/

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF
#define MAXL 20
#define MAXN 200100
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, ini, fim) printf("%s: ", #x); for(int i = ini; i <= fim; i++) printf("%d%c", x[i], (i == fim) ? '\n' : ' ');
typedef long long ll;
typedef pair<int, int> pii;

int stats[MAXN], lg[MAXN], spt[MAXL][MAXN], elements[MAXN], n, ans[MAXN];

void build() {
	int aux, val;

	for(int i = 1; i <= n; i++) {
		aux = i;
		val = 0;

		while( (aux >> 1) > 0) val++, aux >>= 1;
		lg[i] = val;
	}

	for(int i = 0; i < n; i++) spt[0][i] = elements[i];

	for(int j = 1; j <= lg[n]; j++) {
		for(int i = 0; (i + (1 << (j-1)) ) < n; i++) {
			spt[j][i] = min(spt[j-1][i], spt[j-1][i + (1 << (j-1))]);
		}
	}
}

int query(int l, int r) {
	int elementosNoIntervalo = r-l+1;
	return min( spt[ lg[elementosNoIntervalo] ][l], spt[ lg[elementosNoIntervalo] ][ r - (1 << lg[elementosNoIntervalo]) + 1 ] );
}

vector<pii> options;

bool compare(pii f, pii s) {
	if(f.first == s.first) return f.second >= s.second;
	return f.first < s.first;
}

int main() {
	int left, mid, right, aux, r1, r2, possible;
	scanf("%d", &n);

	for(int i = 0; i < n; i++) scanf("%d", &elements[i]);
	build();

	for(int i = 0; i < n; i++) {
		left = 1;
		right = n;

		while(left <= right) {
			mid = (left + right) >> 1;
			r1 = r2 = -1;

			aux = i + mid - 1;
			if(aux < n) r1 = query(i, aux);
			//printf("[%d, %d] => %d\n", i, aux, r1);

			aux = i - mid + 1;
			if(aux >= 0) r2 = query(aux, i);
			//printf("[%d, %d] => %d\n", aux, i, r2);

			if(r1 == elements[i] or r2 == elements[i]) {
				possible = mid;
				left = mid+1;
			}
			else right = mid-1;
		}

		stats[i] = possible;
		options.pb( mp(possible, elements[i]) );
	}

	sort(options.begin(), options.end(), compare);

	int p = 0;
	for(int i = 1; i <= n; i++) {
		while( options[p].first < i ) p++;
		ans[i] = options[p].second;
	}

	for(int i = 1; i <= n; i++) {
		if(i != 1) printf(" ");
		printf("%d", ans[i]);
	}
	printf("\n");

	return 0;
}


/*
 * Matheus Oliveira
 * 22:13:37 13/03/2019
 * 548D.cpp
*/

#include <bits/stdc++.h>
using namespace std;

#define EPS
#define INF 2e9
#define MAXL 20
#define MAXN 200100
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define debug(x) printf("%s: %d\n", #x, x);
#define debugarr(x, ini, fim) printf("%s: ", #x); for(int i = ini; i <= fim; i++) printf("%d%c", x[i], (i == fim) ? '\n' : ' ');
typedef long long ll;
typedef pair<int, int> pii;

vector< pii > options;
int lg[MAXN], spt[MAXL][MAXN], elements[MAXN], n, ans[MAXN];

void build() {
	int aux, val;

	for(int i = 1; i <= n; i++) {
		aux = i;
		val = 0;

		while( (aux >> 1) > 0) val++, aux >>= 1;
		lg[i] = val;
	}

	for(int i = 0; i < n; i++) spt[0][i] = elements[i];

	for(int j = 1; j <= lg[n]; j++) {
		for(int i = 0; (i + (1 << (j-1)) ) < n; i++) {
			spt[j][i] = min(spt[j-1][i], spt[j-1][i + (1 << (j-1))]);
		}
	}
}

int query(int l, int r) {
	if(l > r) swap(l, r);
	int elementosNoIntervalo = r-l+1;
	return min( spt[ lg[elementosNoIntervalo] ][l], spt[ lg[elementosNoIntervalo] ][ r - (1 << lg[elementosNoIntervalo]) + 1 ] );
}

bool compare(pii f, pii s) {
	if(f.first == s.first) return f.second > s.second;
	return f.first > s.first;
}

int bs(int idx, bool checkLeft) {
	int left, right, mid, resp = 0;

	if(checkLeft) left = 0, right = idx - 1;
	else left = idx + 1, right = n-1;

	while(left <= right) {
		mid = (left + right) >> 1;

		if( query(idx, mid) == elements[idx] ) {
			resp = abs(idx - mid);

			if(checkLeft) right = mid - 1;
			else left = mid + 1;
		}
		else {
			if(checkLeft) left = mid + 1;
			else right = mid - 1;
		}
	}

	return resp;
}

int main() {
	int resp, aleft, aright;
	scanf("%d", &n);

	for(int i = 0; i < n; i++) scanf("%d", &elements[i]);
	build();

	for(int i = 0; i < n; i++) {
		aleft = bs(i, true);
		aright = bs(i, false);

		resp = 1 + aleft + aright;
		//printf("resp para %d foi %d. left: %d, right: %d\n", elements[i], resp, aleft, aright);
		options.pb( mp(resp, elements[i]) );
	}

	sort(options.begin(), options.end(), compare);

	int last = -1, p = 0;
	for(int i = n; i >= 1; i--) {
		while(p < n) {
			if(options[p].first >= i) {
				last = max(last, options[p].second);
				p++;
			}
			else break;
		}

		ans[i] = last;
	}

	for(int i = 1; i <= n; i++) {
		if(i != 1) printf(" ");
		printf("%d", ans[i]);
	}
	printf("\n");

	return 0;
}


//  main.cpp
//  solve
//
//  Created by Ahmed on 11/16/16.
//  Copyright © 2016 Abdellah. All rights reserved.
//

#include<set>
#include<map>
#include <unordered_map>
#include <unordered_set>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
#include<math.h>
#include <strings.h>

#define f first
#define s second
#define pb push_back
#define sz(a) (int)a.size()
#define lp(i,a,n) for(int (i)=(a);(i)<=(int)(n);(i)++)
#define lpd(i,n,a) for(ll (i)=(n);(i)>=(a);--(i))
#define mp make_pair
#define clr(a,b) memset(a,b,sizeof a)
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define eps 1e-6
#define infi 1000000002
#define infll 1e16
#define MX 1000000
#define X real()
#define Y imag()
#define polar(r,t) ((r)* exp(point(0,(t))))
#define length(a) hypot( (a).X , (a).Y )
#define angle(a) atan2( (a).Y , (a).X )
#define vec(a,b) ( (b) - (a) )
#define dot(a,b) (conj(a)*(b)).X
#define cross(a,b) (conj(a)*(b)).Y
#define lengthsqr(a) dot(a,a)
#define reflect(V,M) (conj((V)/(M)) * (M))
#define normalize(a)   ((a)/length(a))
#define ccw(a,b,c)   cross(vec(a,b) , vec(a,c)) > -eps
#define cosRule(a,b,c) (acos(((a)*(a)+(b)*(b)-(c)*(c))/(2*(a)*(b))))
#define cosDot(a,b) (acos(dot(a,b)/length(a)/length(b)))
#define EQ(a,b) (fabs((a) - (b)) <= eps) /* equal to */
#define NE(a,b) (fabs((a) - (b)) > eps)  /* not equal to */
#define LT(a,b) ((a) < (b) - eps)        /* less than */
#define GT(a,b) ((a) > (b) + eps)        /* greater than */
#define LE(a,b) ((a) <= (b) + eps)       /* less than or equal to */
#define GE(a,b) ((a) >= (b) - eps)       /* greater than or equal to */
#define mod1 100050001
#define mod2 100030001
#define base1 37
#define base2 31
#define que priority_queue<pair<int,int>, vector<pair<int,int>> , greater<pair<int,int> > >
#define rotate(v,t) ((v)*exp(point(0,t)))
#define rotateabout(v,t,a) (rotate(vec(a,v),t)+(a))
#define PI atan(1)*4

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef set<int> si;
typedef map<int, int> mii;
typedef complex<long double> point;
typedef pair<point, point> line;
typedef pair<double , point> Circle;

const int N = 100005 ;
int n,a[N],l[N],r[N],ans[N];

int main()
{
    scanf("%d",&n);

    lp(i, 1, n) scanf("%d",&a[i]);

    stack<int> st;

    lp(i,1 , n) {

        while(!st.empty() && a[st.top()] > a[i]) r[st.top()] = i , st.pop();

        if(!st.empty()) l[i] = st.top();

        st.push(i);
    }

    while(!st.empty()) r[st.top()] = n+1 , st.pop();

    lp(i, 1, n)
        ans[r[i]-l[i]-1] = max(a[i],ans[r[i]-l[i]-1]);

    lpd(i, n, 1) ans[i] = max(ans[i], ans[i+1]);

    lp(i, 1, n) printf("%d ",ans[i]);
    printf("\n");
    return 0;
}

/*
 */
//freopen("output.txt","w",stdout);
//freopen("input.txt","r",stdin);
//ios::sync_with_stdio(0);cin.tie(0);


//  main.cpp
//  solve
//
//  Created by Ahmed on 11/16/16.
//  Copyright © 2016 Abdellah. All rights reserved.
//

#include<set>
#include<map>
#include <unordered_map>
#include <unordered_set>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
#include<math.h>
#include <strings.h>

#define f first
#define s second
#define pb push_back
#define sz(a) (int)a.size()
#define lp(i,a,n) for(int (i)=(a);(i)<=(int)(n);(i)++)
#define lpd(i,n,a) for(ll (i)=(n);(i)>=(a);--(i))
#define mp make_pair
#define clr(a,b) memset(a,b,sizeof a)
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define eps 1e-6
#define infi 1000000002
#define infll 1e16
#define MX 1000000
#define X real()
#define Y imag()
#define polar(r,t) ((r)* exp(point(0,(t))))
#define length(a) hypot( (a).X , (a).Y )
#define angle(a) atan2( (a).Y , (a).X )
#define vec(a,b) ( (b) - (a) )
#define dot(a,b) (conj(a)*(b)).X
#define cross(a,b) (conj(a)*(b)).Y
#define lengthsqr(a) dot(a,a)
#define reflect(V,M) (conj((V)/(M)) * (M))
#define normalize(a)   ((a)/length(a))
#define ccw(a,b,c)   cross(vec(a,b) , vec(a,c)) > -eps
#define cosRule(a,b,c) (acos(((a)*(a)+(b)*(b)-(c)*(c))/(2*(a)*(b))))
#define cosDot(a,b) (acos(dot(a,b)/length(a)/length(b)))
#define EQ(a,b) (fabs((a) - (b)) <= eps) /* equal to */
#define NE(a,b) (fabs((a) - (b)) > eps)  /* not equal to */
#define LT(a,b) ((a) < (b) - eps)        /* less than */
#define GT(a,b) ((a) > (b) + eps)        /* greater than */
#define LE(a,b) ((a) <= (b) + eps)       /* less than or equal to */
#define GE(a,b) ((a) >= (b) - eps)       /* greater than or equal to */
#define mod1 100050001
#define mod2 100030001
#define base1 37
#define base2 31
#define que priority_queue<pair<int,int>, vector<pair<int,int>> , greater<pair<int,int> > >
#define rotate(v,t) ((v)*exp(point(0,t)))
#define rotateabout(v,t,a) (rotate(vec(a,v),t)+(a))
#define PI atan(1)*4

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef set<int> si;
typedef map<int, int> mii;
typedef complex<long double> point;
typedef pair<point, point> line;
typedef pair<double , point> Circle;

const int N = 100005 ;
int n,a[N],l[N],r[N],ans[N];

int main()
{
    scanf("%d",&n);

    lp(i, 1, n) scanf("%d",&a[i]);

    stack<int> st;

    lp(i,1 , n) {

        while(!st.empty() && a[st.top()] > a[i]) r[st.top()] = i , st.pop();

        if(!st.empty()) l[i] = st.top();

        st.push(i);
    }

    while(!st.empty()) r[st.top()] = n+1 , st.pop();

    lp(i, 1, n)
        ans[r[i]-l[i]-1] = a[i];

    lpd(i, n, 1) ans[i] = max(ans[i], ans[i+1]);

    lp(i, 1, n) printf("%d ",ans[i]);
    printf("\n");
    return 0;
}

/*
 */
//freopen("output.txt","w",stdout);
//freopen("input.txt","r",stdin);
//ios::sync_with_stdio(0);cin.tie(0);


//  main.cpp
//  solve
//
//  Created by Ahmed on 11/16/16.
//  Copyright © 2016 Abdellah. All rights reserved.
//

#include<set>
#include<map>
#include <unordered_map>
#include <unordered_set>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
#include<math.h>
#include <strings.h>

#define f first
#define s second
#define pb push_back
#define sz(a) (int)a.size()
#define lp(i,a,n) for(int (i)=(a);(i)<=(int)(n);(i)++)
#define lpd(i,n,a) for(ll (i)=(n);(i)>=(a);--(i))
#define mp make_pair
#define clr(a,b) memset(a,b,sizeof a)
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define eps 1e-6
#define infi 1000000002
#define infll 1e16
#define MX 1000000
#define X real()
#define Y imag()
#define polar(r,t) ((r)* exp(point(0,(t))))
#define length(a) hypot( (a).X , (a).Y )
#define angle(a) atan2( (a).Y , (a).X )
#define vec(a,b) ( (b) - (a) )
#define dot(a,b) (conj(a)*(b)).X
#define cross(a,b) (conj(a)*(b)).Y
#define lengthsqr(a) dot(a,a)
#define reflect(V,M) (conj((V)/(M)) * (M))
#define normalize(a)   ((a)/length(a))
#define ccw(a,b,c)   cross(vec(a,b) , vec(a,c)) > -eps
#define cosRule(a,b,c) (acos(((a)*(a)+(b)*(b)-(c)*(c))/(2*(a)*(b))))
#define cosDot(a,b) (acos(dot(a,b)/length(a)/length(b)))
#define EQ(a,b) (fabs((a) - (b)) <= eps) /* equal to */
#define NE(a,b) (fabs((a) - (b)) > eps)  /* not equal to */
#define LT(a,b) ((a) < (b) - eps)        /* less than */
#define GT(a,b) ((a) > (b) + eps)        /* greater than */
#define LE(a,b) ((a) <= (b) + eps)       /* less than or equal to */
#define GE(a,b) ((a) >= (b) - eps)       /* greater than or equal to */
#define mod1 100050001
#define mod2 100030001
#define base1 37
#define base2 31
#define que priority_queue<pair<int,int>, vector<pair<int,int>> , greater<pair<int,int> > >
#define rotate(v,t) ((v)*exp(point(0,t)))
#define rotateabout(v,t,a) (rotate(vec(a,v),t)+(a))
#define PI atan(1)*4

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef set<int> si;
typedef map<int, int> mii;
typedef complex<long double> point;
typedef pair<point, point> line;
typedef pair<double , point> Circle;

const int N = 200005 ;
int n,a[N],l[N],r[N],ans[N];

int main()
{
    scanf("%d",&n);

    lp(i, 1, n) scanf("%d",&a[i]);

    stack<int> st;

    lp(i,1 , n) {

        while(!st.empty() && a[st.top()] > a[i]) r[st.top()] = i , st.pop();

        if(!st.empty()) l[i] = st.top();

        st.push(i);
    }

    while(!st.empty()) r[st.top()] = n+1 , st.pop();

    lp(i, 1, n)
        ans[r[i]-l[i]-1] = max(a[i],ans[r[i]-l[i]-1]);

    lpd(i, n, 1) ans[i] = max(ans[i], ans[i+1]);

    lp(i, 1, n) printf("%d ",ans[i]);
    printf("\n");
    return 0;
}

/*
 */
//freopen("output.txt","w",stdout);
//freopen("input.txt","r",stdin);
//ios::sync_with_stdio(0);cin.tie(0);


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class MikeFeet {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        final int n = Integer.parseInt(line);
        line = br.readLine();
        StringTokenizer st = new StringTokenizer(line);
        Bear[] originalBears = new Bear[n], sortedBears;
        for (int i = 0; i < n; i++) {
            int h = Integer.parseInt(st.nextToken());
            originalBears[i] = new Bear(h, i);
        }
        sortedBears = Arrays.copyOf(originalBears, n);
        Arrays.sort(sortedBears);
        int[] heights = new int[n + 1];
        int maxGrpLen = 0;
        for (Bear bear : sortedBears) {
            bear.marked = true;
            Bear left = bear, right = bear;
            int nIdx = bear.index - 1;
            if (nIdx >= 0 && originalBears[nIdx].marked) {
                left = originalBears[originalBears[nIdx].groupLeft];
            }
            nIdx += 2;
            if (nIdx < n && originalBears[nIdx].marked) {
                right = originalBears[originalBears[nIdx].groupRight];
            }

            left.groupRight = right.index;
            right.groupLeft = left.index;

            int newGrpLen = right.index - left.index + 1;
            if (maxGrpLen < newGrpLen) {
                for (int i = maxGrpLen + 1; i <= newGrpLen; i++) {
                    heights[i] = bear.height;
                }
                maxGrpLen = newGrpLen;
            }
        }
        for (int i = 1; i <= n; i++) {
            if (i > 1) {
                System.out.print(" ");
            }
            System.out.print(heights[i]);
        }
        System.out.println();
    }

    private static class Bear implements Comparable<Bear> {

        private final int height, index;
        private boolean marked;
        private int groupLeft, groupRight;

        Bear(int h, int i) {
            height = h;
            index = i;
            groupLeft = groupRight = i;
        }

        @Override
        public int compareTo(Bear o) {
            // reverse sort
            return o.height - height;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class MikeFeet {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        final int n = Integer.parseInt(line);
        line = br.readLine();
        StringTokenizer st = new StringTokenizer(line);
        Bear[] originalBears = new Bear[n], sortedBears;
        for (int i = 0; i < n; i++) {
            int h = Integer.parseInt(st.nextToken());
            originalBears[i] = new Bear(h, i);
        }
        sortedBears = Arrays.copyOf(originalBears, n);
        Arrays.sort(sortedBears);
        int[] heights = new int[n + 1];
        int maxGrpLen = 0;
        for (Bear bear : sortedBears) {
            bear.marked = true;
            Bear left = bear, right = bear;
            int nIdx = bear.index - 1;
            if (nIdx >= 0 && originalBears[nIdx].marked) {
                left = originalBears[originalBears[nIdx].groupLeft];
            }
            nIdx += 2;
            if (nIdx < n && originalBears[nIdx].marked) {
                right = originalBears[originalBears[nIdx].groupRight];
            }

            left.groupRight = right.index;
            right.groupLeft = left.index;

            int newGrpLen = right.index - left.index + 1;
            if (maxGrpLen < newGrpLen) {
                for (int i = maxGrpLen + 1; i <= newGrpLen; i++) {
                    heights[i] = bear.height;
                }
                maxGrpLen = newGrpLen;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= n; i++) {
            if (i > 1) {
                sb.append(" ");
            }
            sb.append(heights[i]);
        }
        System.out.println(sb.toString());
    }

    private static class Bear implements Comparable<Bear> {

        private final int height, index;
        private boolean marked;
        private int groupLeft, groupRight;

        Bear(int h, int i) {
            height = h;
            index = i;
            groupLeft = groupRight = i;
        }

        @Override
        public int compareTo(Bear o) {
            // reverse sort
            return o.height - height;
        }
    }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

/**
 *
 * @author sultan.of.swing
 */

public class TaskD {

	public FasterScanner mFScanner;
	public PrintWriter mOut;
	private int sz[];
	private int parent[];

	public TaskD() {
		mFScanner = new FasterScanner();
		mOut = new PrintWriter(System.out);
	}

	public int get(int u) {
		return u == parent[u] ? u : (parent[u] = get(parent[u]));
	}

	public void union(int u, int v, int n) {
		if (u < 0 || u >= n || v < 0 || v >= n)
			return;

		if (parent[u] == -1 || parent[v] == -1)
			return;

		u = get(u);
		v = get(v);

		if (u == -1 || v == -1)
			return;

		sz[u] += sz[v];
		parent[v] = u;

	}

	public void solve() {
		int n;
		Node array[];
		int res[];
		int idx;
		Node node;

		n = mFScanner.nextInt();
		array = new Node[n];
		res = new int[n];
		sz = new int[n];
		parent = new int[n];

		for (int i = 0; i < n; i++) {
			array[i] = new Node(i, mFScanner.nextInt());
			sz[i] = 1;
			parent[i] = -1;
		}

		Arrays.sort(array);

		for (int i = 0; i < n; i++) {
			node = array[i];
			idx = node.index;
			parent[idx] = idx;

			union(idx, idx - 1, n);
			union(idx, idx + 1, n);

			res[sz[idx] - 1] = Math.max(node.value, res[sz[idx] - 1]);

		}

		for (int i = n - 2; i >= 0; i--) {
			res[i] = Math.max(res[i], res[i + 1]);
		}

		for (int i = 0; i < n; i++) {
			mOut.print(res[i] + " ");
		}

	}

	class Node implements Comparable<Node> {
		int index;
		int value;

		public Node(int index, int value) {
			this.index = index;
			this.value = value;
		}

		@Override
		public int compareTo(Node o) {
			// TODO Auto-generated method stub
			return Integer.compare(o.value, this.value);
		}
	}

	public void flush() {
		mOut.flush();
	}

	public void close() {
		mOut.close();
	}

	public static void main(String[] args) {
		TaskD mSol = new TaskD();
		mSol.solve();
		mSol.flush();
		mSol.close();
	}

	class FasterScanner {
		private InputStream mIs;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FasterScanner() {
			this(System.in);
		}

		public FasterScanner(InputStream is) {
			mIs = is;
		}

		public int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = mIs.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public String nextString() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public double nextDouble() {
			Double next;
			next = Double.parseDouble(nextString());
			return next;
		}

		public boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		public char[] nextCharArray(int N) {
			int i;
			char[] array;
			String str;

			array = new char[N];

			i = 0;

			str = nextLine();

			for (i = 0; i < N && i < str.length(); i++) {
				array[i] = str.charAt(i);
			}

			return array;
		}

		public char[][] nextChar2DArray(int M, int N) {
			int i;
			char[][] array;

			array = new char[M][N];

			i = 0;

			for (i = 0; i < M; i++) {
				array[i] = nextCharArray(N);
			}

			return array;
		}

		public int[] nextIntArray(int N) {
			int i;
			int[] array;

			array = new int[N];

			i = 0;

			for (i = 0; i < N; i++) {
				array[i] = nextInt();
			}

			return array;
		}

		public int[][] nextInt2DArray(int M, int N) {
			int i;
			int[][] array;

			array = new int[M][N];

			i = 0;

			for (i = 0; i < M; i++) {
				array[i] = nextIntArray(N);
			}

			return array;
		}

		public long[] nextLongArray(int N) {
			int i;
			long[] array;

			array = new long[N];

			i = 0;

			for (i = 0; i < N; i++) {
				array[i] = nextLong();
			}

			return array;
		}

		public long[][] nextLong2DArray(int M, int N) {
			int i;
			long[][] array;

			array = new long[M][N];

			i = 0;

			for (i = 0; i < M; i++) {
				array[i] = nextLongArray(N);
			}

			return array;
		}

		public double[] nextDoubleArray(int N) {
			int i;
			double[] array;

			array = new double[N];

			for (i = 0; i < N; i++) {
				array[i] = nextDouble();
			}

			return array;
		}

		public double[][] nextDouble2DArray(int M, int N) {
			int i;
			double[][] array;

			array = new double[M][N];

			for (i = 0; i < M; i++) {
				array[i] = nextDoubleArray(N);
			}

			return array;
		}

	}
}


import java.io.*;
import java.util.*;
import java.math.*;

public class D {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		int n = Integer.parseInt(in.readLine());
		int[] a = new int[n];
		st = new StringTokenizer(in.readLine());
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(st.nextToken());
		}

		Stack<Integer> s = new Stack<Integer>();
		int[] l = new int[n];
		int[] r = new int[n];
		int[] ans = new int[n+2];

		Arrays.fill(l, -1);
		Arrays.fill(r, n);

		for (int i = 0; i < n; i++) {
			while (!s.empty() && a[s.peek()] >= a[i]) {
				s.pop();
			}
			if (!s.empty()) {
				l[i] = s.peek();
			}

			s.push(i);
		}

		while (!s.empty())
			s.pop();

		for (int i = n-1; i >= 0; i--) {
			while (!s.empty() && a[s.peek()] >= a[i]) {
				s.pop();
			}
			if (!s.empty()) {
				r[i] = s.peek();
			}

			s.push(i);
		}

		for (int i = 0; i < n; i++) {
			int len = r[i] - l[i] - 1;
			ans[len] = Math.max(ans[len], a[i]);
		}

		for (int i = n; i >= 0; i--) {
			ans[i] = Math.max(ans[i], ans[i+1]);
		}

		for (int i = 1; i <= n; i++) {
			System.out.print(ans[i] + " ");
		}
		System.out.println();
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Main
{

    class Bear
    {
        int n, a;
        Bear( int q, int w )
        {
            n = q; a = w;
        }
    }

    void solve() throws IOException
    {
        int n = nextInt();
        Bear[] bears = new Bear[ n ];
        for( int i = 0; i < n; i++ )
            bears[ i ] = new Bear( i + 1, nextInt() );
        Arrays.sort(
                bears,
                new Comparator<Bear>()
                {
                    public int compare( Bear o1, Bear o2 )
                    {
                        return o2.a - o1.a;
                    }
                }
        );
        Bear[] seg = new Bear[ n + 2 ];
        int was = 0;
        int mSeg = 0;
        int i = 0;
        while( i < n )
        {
            int a = bears[ i ].a;
            while( i < n && bears[ i ].a == a )
            {
                int j = bears[ i ].n;
                seg[ j ] = new Bear( j, j );
                if( seg[ j - 1 ] != null ) seg[ j ].n = seg[ j - 1 ].n;
                if( seg[ j + 1 ] != null ) seg[ j ].a = seg[ j + 1 ].a;
                seg[ seg[ j ].n ].a = seg[ j ].a;
                seg[ seg[ j ].a ].n = seg[ j ].n;
                mSeg = Math.max( mSeg, seg[ j ].a - seg[ j ].n + 1 );
                i++;
            }
            while( mSeg > was )
            {
                was++;
                out.print( a + " " );
            }
        }
    }

    public static void main( String[] args )
    {
        new Main().run();
    }

    public void run()
    {
        try
        {
            in = new BufferedReader( new InputStreamReader( System.in ) );
            out = new PrintWriter( System.out );
            st = null;
            solve();
        }
        catch( Exception e )
        {
            e.printStackTrace();
            System.exit( 1 );
        }
        finally
        {
            out.close();
        }
    }

    String nextToken() throws IOException
    {
        while( st == null || !st.hasMoreTokens() ) st = new StringTokenizer( in.readLine() );
        return st.nextToken();
    }

    int nextInt() throws IOException
    {
        return Integer.parseInt( nextToken() );
    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer st;
}


import java.util.*;
public class Codeforces305D {
public static void main(String args[]){
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    int arr[]=new int[n],l[]=new int[n],r[]=new int[n],ans[]=new int[n];
    for(int i=0;i<n;i++)
        arr[i]=in.nextInt();
    Stack<Integer> st=new Stack<Integer>();
    for(int i=0;i<n;i++){
        while(!st.isEmpty() && arr[st.peek()]>=arr[i])
            st.pop();
        if(st.isEmpty())
            l[i]=-1;
        else
            l[i]=st.peek();
        st.push(i);
    }
    st.clear();
    for(int i=n-1;i>=0;i--){
        while(!st.isEmpty() && arr[st.peek()]>=arr[i])
            st.pop();
        if(st.isEmpty())
            r[i]=n;
        else
            r[i]=st.peek();
        st.push(i);
    }
    for(int i=0;i<n;i++){
        int len=r[i]-l[i]-1;
        ans[len-1]=Math.max(arr[i], ans[len-1]);
    }
    for(int i=n-2;i>=0;i--){
        ans[i]=Math.max(ans[i], ans[i+1]);
    }
    for(int i=0;i<n;i++)
        System.out.print(ans[i]+" ");
    in.close();
}
}


import java.util.*;
public class Codeforces305D {
    static int max(int a,int b){
        return a>b?a:b;
    }

public static void main(String args[]){
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    int arr[]=new int[n],l[]=new int[n],r[]=new int[n],ans[]=new int[n];
    for(int i=0;i<n;i++)
        arr[i]=in.nextInt();
    int st1[]=new int[n],st2[]=new int[n];
    int ind1=-1,ind2=-1;
    for(int i=0;i<n;i++){
        while(ind1!=-1 && arr[st1[ind1]]>=arr[i])
            ind1--;
        if(ind1==-1)
            l[i]=-1;
        else
            l[i]=st1[ind1];
        st1[++ind1]=i;
    }
    for(int i=n-1;i>=0;i--){
        while(ind2!=-1 && arr[st2[ind2]]>=arr[i])
            ind2--;
        if(ind2==-1)
            r[i]=n;
        else
            r[i]=st2[ind2];
        st2[++ind2]=i;
    }
    for(int i=0;i<n;i++){
        int len=r[i]-l[i]-1;
        ans[len-1]=max(arr[i], ans[len-1]);
    }
    for(int i=n-2;i>=0;i--){
        ans[i]=max(ans[i], ans[i+1]);
    }
    for(int i=0;i<n;i++)
        System.out.print(ans[i]+" ");
    in.close();
}
}


import java.util.*;
public class Codeforces305D {
    static int max(int a,int b){
        return a>b?a:b;
    }

public static void main(String args[]){
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    int arr[]=new int[n],l[]=new int[n],r[]=new int[n],ans[]=new int[n];
    for(int i=0;i<n;i++)
        arr[i]=in.nextInt();
    Stack<Integer> st=new Stack<Integer>();
    for(int i=0;i<n;i++){
        while(!st.isEmpty() && arr[st.peek()]>=arr[i])
            st.pop();
        if(st.isEmpty())
            l[i]=-1;
        else
            l[i]=st.peek();
        st.push(i);
    }
    st.clear();
    for(int i=n-1;i>=0;i--){
        while(!st.isEmpty() && arr[st.peek()]>=arr[i])
            st.pop();
        if(st.isEmpty())
            r[i]=n;
        else
            r[i]=st.peek();
        st.push(i);
    }
    for(int i=0;i<n;i++){
        int len=r[i]-l[i]-1;
        ans[len-1]=max(arr[i], ans[len-1]);
    }
    for(int i=n-2;i>=0;i--){
        ans[i]=max(ans[i], ans[i+1]);
    }
    for(int i=0;i<n;i++)
        System.out.print(ans[i]+" ");
    in.close();
}
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class Main {
	static class Element implements Comparable<Element> {
		public int data;
		public int index;

		public Element(int data, int index) {
			this.data = data;
			this.index = index;
		}

		public int compareTo(Element shr) {
			if (data > shr.data) {
				return -1;
			} else if (data < shr.data) {
				return 1;
			} else if (index < shr.index) {
				return -1;
			} else if (index > shr.index) {
				return 1;
			} else {
				return 0;
			}
		}

		@Override
		public String toString() {
			return data + " " + index;
		}
	}

	static final int N = 200_003;
	static int[] size = new int[N];
	static int[] parent = new int[N];
	static void initialize() {
		for (int i = 0; i < N; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	static int getParrent(int v) {
		if (v == parent[v])
			return v;
		return parent[v] = getParrent(parent[v]);
	}

	static int merge(int a, int b) {
		int parrentA = getParrent(a);
		int parrentB = getParrent(b);
		if (parrentA != parrentB) {
			if (size[parrentA] < size[parrentB]) {
				int temp = parrentA;
				parrentA = parrentB;
				parrentB = temp;
			}
			parent[parrentB] = parrentA;
			return size[parrentA] += size[parrentB];
		}
		return size[parrentA];
	}

	static int max = 1;
	static boolean[] used;

	static void DSUAdd(int index, int n) {
		used[index] = true;
		if (index-1 >= 0 && used[index-1]) {
			max = Math.max(max, merge(index-1, index));
		}
		if (index+1 < n && used[index+1]) {
			max = Math.max(max, merge(index+1, index));
		}
	}

	public static void main(String[] args) throws Exception {
		initialize();

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

		int n = Integer.parseInt(in.readLine());
		Element[] heights = new Element[n];
		used = new boolean[n];

		String[] inputData = in.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			heights[i] = new Element(Integer.parseInt(inputData[i]), i);
		}
		Arrays.sort(heights);
		int cur = 1;
		DSUAdd(heights[0].index, n);
		//System.out.println(max);
		for (int i = 0; i < n; i++) {
			while (max < i+1) {
				DSUAdd(heights[cur++].index, n);
			}
			//System.err.println(max + " " + cur);
			out.write(heights[cur-1].data + " ");
		}
		in.close();
		out.close();
	}
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Task_Rnd305_D {

	public static void main(String[] args) throws Exception
	{
		//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		//PrintStream out = new PrintStream(new BufferedOutputStream(System.out));

		int n = reader.nextInt();
		int aa[] = new int[n];

		for (int i = 0; i < n; i++)
			aa[i] = reader.nextInt();

		int bb[] = new int[n];
		for(int i = 0; i < n; i++)
			bb[i] = aa[i];

		for (int i = 0; i < n; i++)
		{
			int g = n - i;

			int max = -1;
			for (int j = 0; j < g; j++)
			{
				if (aa[j+i] < bb[j])
					bb[j] = aa[j+i];
				if (bb[j] > max)
					max = bb[j];
			}

			if (i > 0)
				printer.print(' ');
			printer.print(max);
		}

		printer.print("\n");
		//out.flush();
		printer.close();
	}

	static PrintWriter printer=new PrintWriter(new OutputStreamWriter(System.out));
	static class reader{
		static BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
		static StringTokenizer token=new StringTokenizer("");
		static String readNextLine() throws Exception{
			return bReader.readLine();
		}
		static String next() throws Exception{
			while (token.hasMoreTokens()==false){
				token=new StringTokenizer(bReader.readLine());
			}
			return token.nextToken();
		}
		static int nextInt()throws Exception{
			while (token.hasMoreTokens()==false){
				token=new StringTokenizer(bReader.readLine());
			}
			return Integer.parseInt(token.nextToken());
		}
		static long nextLong()throws Exception{
			while (token.hasMoreTokens()==false){
				token=new StringTokenizer(bReader.readLine());
			}
			return Long.parseLong(token.nextToken());
		}
		static double nextDouble()throws Exception{
			while (token.hasMoreTokens()==false){
				token=new StringTokenizer(bReader.readLine());
			}
			return Double.parseDouble(token.nextToken());
		}
	}
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class Task_Rnd305_D {

	public static void main(String[] args) throws NumberFormatException, IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintStream out = new PrintStream(new BufferedOutputStream(System.out));

		int n = Integer.valueOf(br.readLine());
		int aa[] = new int[n];

		String[] ss = br.readLine().trim().split("\\s+");
		for (int i = 0; i < n; i++)
			aa[i] = Integer.valueOf(ss[i]);

		int bb[] = new int[n];
		for(int i = 0; i < n; i++)
			bb[i] = aa[i];

		for (int i = 0; i < n; i++)
		{
			int g = n - i;

			int max = -1;
			for (int j = 0; j < g; j++)
			{
				if (aa[j+i] < bb[j])
					bb[j] = aa[j+i];
				if (bb[j] > max)
					max = bb[j];
			}

			if (i > 0)
				out.print(' ');
			out.print(max);
		}

		out.println();
		out.flush();
	}
}


import java.io.FileInputStream;
import java.io.*;
import java.util.Locale;
import java.util.StringTokenizer;

public class Task_Rnd305_D {

	public static void main(String[] args)
	{
		reader.init(true);	///!!!
		run();
		wr.flush();
	}

    public static void run()
    {
		int n = reader.nextInt();
		int aa[] = new int[n];
		for (int i = 0; i < n; i++)
			aa[i] = reader.nextInt();

		int LL[] = new int[n];
		int RR[] = new int[n];
		int ss[] = new int[n];
		int st = 0;

		for (int ii = 0; ii < n; ii++) {
			while (st > 0 && aa[ss[st-1]] >= aa[ii])
				st--;
			if (st == 0)
				LL[ii] = -1;
			else
				LL[ii] = ss[st-1];

			ss[st++] = ii;
		}

		st = 0;
		for (int ii = n-1; ii >= 0; ii--) {
			while (st > 0 && aa[ss[st-1]] >= aa[ii])
				st--;
			if (st == 0)
				RR[ii] = n;
			else
				RR[ii] = ss[st-1];

			ss[st++] = ii;
		}

		int res[] = new int[n+1];
		for (int ii = 0; ii < n; ii++) {
			int dd = RR[ii] - LL[ii] - 1;
			res[dd] = Math.max(res[dd], aa[ii]);
		}

		for (int ii = n-1; ii >= 1; ii--) {
			res[ii] = Math.max(res[ii], res[ii+1]);
		}

		for (int ii = 0; ii < n-1; ii++) {
			wr.print(res[ii+1]);
			wr.print(' ');
		}
		wr.print(res[n]);
		wr2.endl();
    }

    public static void run_slow()
    {
		int n = reader.nextInt();
		int aa[] = new int[n];

		for (int i = 0; i < n; i++)
			aa[i] = reader.nextInt();

		int bb[] = new int[n];
		for(int i = 0; i < n; i++)
			bb[i] = aa[i];

		for (int i = 0; i < n; i++)
		{
			int g = n - i;

			int max = -1;
			for (int j = 0; j < g; j++)
			{
				if (aa[j+i] < bb[j])
					bb[j] = aa[j+i];
				if (bb[j] > max)
					max = bb[j];
			}

			if (i > 0)
				wr.print(' ');
			wr.print(max);
		}

		wr2.endl();
	}

    static public PrintWriter wr;
    static OutputWriter wr2=new OutputWriter(System.out);
	static class reader{
		static BufferedReader br; static StringTokenizer tkn;
		static String readln() { try { return br.readLine(); } catch (Exception e) { return ""; } }
		static void init(boolean console) {
			if (console) { br=new BufferedReader(new InputStreamReader(System.in)); }
			else { try { br=new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); }
				catch (Exception e) { System.exit(551); } }
			tkn=new StringTokenizer("");
		}
		static String next() { while (!tkn.hasMoreTokens()){ tkn=new StringTokenizer(readln()); } return tkn.nextToken(); }
		static int nextInt(){ while (!tkn.hasMoreTokens()){ tkn=new StringTokenizer(readln()); } return Integer.parseInt(tkn.nextToken()); }
		static long nextLong(){ while (!tkn.hasMoreTokens()){ tkn=new StringTokenizer(readln()); } return Long.parseLong(tkn.nextToken()); }
		static double nextDouble(){ while (!tkn.hasMoreTokens()){ tkn=new StringTokenizer(readln()); } return Double.parseDouble(tkn.nextToken()); }
	}
    static class OutputWriter {
        OutputWriter(OutputStream stream) { wr = new PrintWriter(stream); }
        public void printf(String format, Object... args) { wr.print(String.format(Locale.ENGLISH, format, args)); }
        public void endl() { wr.print("\n"); }
    }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class Task_Rnd305_D {

	public static void main(String[] args) throws NumberFormatException, IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintStream out = new PrintStream(new BufferedOutputStream(System.out));

		int n = Integer.valueOf(br.readLine());
		int aa[] = new int[n];

		String[] ss = br.readLine().trim().split("\\s+");
		for (int i = 0; i < n; i++)
			aa[i] = Integer.valueOf(ss[i]);

		int bb[] = new int[n];
		for(int i = 0; i < n; i++)
			bb[i] = aa[i];

		for (int i = 0; i < n; i++)
		{
			int g = n - i;

			if (i > 0)
			{
				for (int j = 0; j < g; j++)
				{
					bb[j] = Math.min(bb[j], aa[j+i]);
				}
			}

			int max = bb[0];
			for (int j = 0; j < g; j++)
			{
				if (bb[j] > max)
					max = bb[j];
			}

			if (i > 0)
				out.print(' ');
			out.print(max);
		}

		out.println();
		out.flush();
	}
}


import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

/**
 * Created by andywu on 5/26/15.
 */
public class TaskD {
    public static void main(String[] args){
        new TaskD().solve(new Scanner(System.in), System.out);
    }

    class R implements Comparable<R>{
        int idx;

        R(int idx, int val) {
            this.idx = idx;
            this.val = val;
        }

        int val;

        public int compareTo(R o) {
            return val - o.val;
        }
    }

    private void solve(Scanner scanner, PrintStream out) {
        int n = scanner.nextInt();
        int[] heights = new int[n];
        for(int i = 0; i < n;i++) heights[i] = scanner.nextInt();
        int[] left = new int[n];
        int[] right = new int[n];
        Stack<Integer> s = new Stack<Integer>();
        s.push(-1);
        for(int i = 0; i < n; i++){
            while (s.peek()>=0 && heights[s.peek()]>=heights[i]){
                s.pop();
            }
            left[i] = s.peek();
            s.push(i);
        }
        s.clear();
        s.push(n);
        for(int i = n-1; i>=0; i--){
            while (s.peek()<n&&heights[s.peek()]>=heights[i]){
                s.pop();
            }
            right[i] = s.peek();
            s.push(i);
        }
        R[] rs = new R[n];
        for(int i = 0; i < n; i++){
            rs[i] = new R(i, heights[i]);
        }
        Arrays.sort(rs);
        int val = Integer.MAX_VALUE;
        int mlen = 0;
        int[] ans = new int[n];
        Arrays.fill(ans, 0);
        int cur = 0;
        for(int i = n-1; i>=0; i--){
            if(val == rs[i].val){
                mlen = Math.max(mlen, right[rs[i].idx] - left[rs[i].idx] - 1);
            }else {
                int oval = val;
                val = rs[i].val;
                int len = mlen;
                mlen = right[rs[i].idx] - left[rs[i].idx] - 1;
                if(len>0 && len > cur) {
                    ans[len-1] = Math.max(ans[len-1], oval);
                    cur = len-1;
                }
            }
        }
        ans[mlen-1] = Math.max(ans[mlen-1], val);
        StringBuilder sb = new StringBuilder();
        int count = 0;
        for(int i = 0; i < n; i++){
            if(ans[i] == 0) count++;
            else{
                for(int j = 0; j <= count; j++){
                    sb.append(ans[i] + " ");
                }
                count = 0;
            }
        }
        out.println(sb.toString());
    }
}


import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

/**
 * Created by andywu on 5/26/15.
 */
public class TaskD {
    public static void main(String[] args){
        new TaskD().solve(new Scanner(System.in), System.out);
    }

    class R implements Comparable<R>{
        int idx;

        R(int idx, int val) {
            this.idx = idx;
            this.val = val;
        }

        int val;

        public int compareTo(R o) {
            return val - o.val;
        }
    }

    private void solve(Scanner scanner, PrintStream out) {
        int n = scanner.nextInt();
        int[] heights = new int[n];
        for(int i = 0; i < n;i++) heights[i] = scanner.nextInt();
        int[] left = new int[n];
        int[] right = new int[n];
        Stack<Integer> s = new Stack<Integer>();
        s.push(-1);
        for(int i = 0; i < n; i++){
            while (s.peek()>=0 && heights[s.peek()]>=heights[i]){
                s.pop();
            }
            left[i] = s.peek();
            s.push(i);
        }
        s.clear();
        s.push(n);
        for(int i = n-1; i>=0; i--){
            while (s.peek()<n&&heights[s.peek()]>=heights[i]){
                s.pop();
            }
            right[i] = s.peek();
            s.push(i);
        }
        R[] rs = new R[n];
        for(int i = 0; i < n; i++){
            rs[i] = new R(i, heights[i]);
        }
        Arrays.sort(rs);
        int val = Integer.MAX_VALUE;
        int mlen = 0;
        int[] ans = new int[n];
        Arrays.fill(ans, 0);
        for(int i = n-1; i>=0; i--){
            if(val == rs[i].val){
                mlen = Math.max(mlen, right[rs[i].idx] - left[rs[i].idx] - 1);
            }else {
                int oval = val;
                val = rs[i].val;
                int len = mlen;
                mlen = right[rs[i].idx] - left[rs[i].idx] - 1;
                if(len>0) ans[len-1] = Math.max(ans[len-1], oval);
            }
        }
        ans[mlen-1] = Math.max(ans[mlen-1], val);
        StringBuilder sb = new StringBuilder();
        int count = 0;
        for(int i = 0; i < n; i++){
            if(ans[i] == 0) count++;
            else{
                for(int j = 0; j <= count; j++){
                    sb.append(ans[i] + " ");
                }
                count = 0;
            }
        }
        out.println(sb.toString());
    }
}


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main
{
	/********************************************** a list of common variables **********************************************/

	private MyScanner scan = new MyScanner();
	private PrintWriter out = new PrintWriter(System.out);
	private final double PI = Math.acos(-1.0);
	private final int SIZEN = (int)(1e7);
	private final int MOD = (int)(1e9 + 7);

	public void foo()
	{
		int n = scan.nextInt();
		int[] a = new int[n];
		for(int i = 0;i < n;++i)
		{
			a[i] = scan.nextInt();
		}
		int[] left = new int[n];
		int[] right = new int[n];
		Stack<int[]> s = new Stack<int[]>();
		s.push(new int[]{-1, -1}); //p[0]:num, p[1]:index
		for(int i = 0;i < n;++i)
		{
			while(!s.isEmpty())
			{
				int[] p = s.peek();
				if(a[i] >= p[0])
				{
					left[i] = a[i] > p[0] ? p[1] + 1 : left[p[1]];
					break;
				}
				else
				{
					right[p[1]] = i - 1;
					s.pop();
				}
			}
			s.push(new int[]{a[i], i});
		}
		while(s.size() > 1)
		{
			int[] p = s.pop();
			right[p[1]] = n - 1;
		}
		int[] ans = new int[n + 1];
		for(int i = 0;i < n;++i)
		{
			int d = right[i] - left[i] + 1;
			ans[d] = Math.max(ans[d], a[i]);
		}
		for(int i = n - 1;i >= 1;--i)
		{
			ans[i] = Math.max(ans[i], ans[i + 1]);
		}
		for(int i = 1;i <= n;++i)
		{
			out.print(ans[i] + " ");
		}
	}

	public static void main(String[] args)
	{
		Main m = new Main();
		m.foo();
		m.out.close();
	}

	/********************************************** a list of common algorithms **********************************************/

	/**
	 * KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1.
	 *
	 * @param t: String to match.
	 * @param p: String to be matched.
	 * @return if can match, first index; otherwise -1.
	 */
	public int kmpMatch(char[] t, char[] p)
	{
		int n = t.length;
		int m = p.length;
		int[] next = new int[m + 1];
		next[0] = -1;
		int j = -1;
		for(int i = 1;i < m;++i)
		{
			while(j >= 0 && p[i] != p[j + 1])
			{
				j = next[j];
			}
			if(p[i] == p[j + 1])
			{
				++j;
			}
			next[i] = j;
		}
		j = -1;
		for(int i = 0;i < n;++i)
		{
			while(j >= 0 && t[i] != p[j + 1])
			{
				j = next[j];
			}
			if(t[i] == p[j + 1])
			{
				++j;
			}
			if(j == m - 1)
			{
				return i - m + 1;
			}
		}
		return -1;
	}

	class MyScanner
	{
	    private byte[] buf = new byte[1024];
	    private int curChar;
	    private int numChars;
	    BufferedInputStream bis = new BufferedInputStream(System.in);

	    public int read()
	    {
	        if (-1 == numChars)
	        {
	        	throw new InputMismatchException();
	        }
	        if (curChar >= numChars)
	        {
	            curChar = 0;
	            try
	            {
	                numChars = bis.read(buf);
	            }
	            catch (IOException e)
	            {
	                throw new InputMismatchException();
	            }
	            if (numChars <= 0)
	            {
	            	return -1;
	            }
	        }
	        return buf[curChar++];
	    }

	    public int nextInt()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        int sgn = 1;
	        if (c == '-')
	        {
	            sgn = -1;
	            c = read();
	        }
	        int res = 0;
	        do
	        {
	            if (c < '0' || c > '9')
	            {
	            	throw new InputMismatchException();
	            }
	            res *= 10;
	            res += c - '0';
	            c = read();
	        } while (!isSpaceChar(c));
	        return res * sgn;
	    }

	    public long nextLong()
	    {
	    	int c = read();
	    	while (isSpaceChar(c))
	    	{
	    		c = read();
	    	}
	    	int sgn = 1;
	    	if (c == '-')
	    	{
	    		sgn = -1;
	    		c = read();
	    	}
	    	long res = 0;
	    	do
	    	{
	    		if (c < '0' || c > '9')
	    		{
	    			throw new InputMismatchException();
	    		}
	    		res *= 10;
	    		res += c - '0';
	    		c = read();
	    	} while (!isSpaceChar(c));
	    	return res * sgn;
	    }

	    public double nextDouble()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        int sgn = 1;
	        if (c == '-')
	        {
	            sgn = -1;
	            c = read();
	        }
	        double res = 0;
	        while (!isSpaceChar(c) && c != '.')
	        {
	            if (c == 'e' || c == 'E')
	            {
	            	return res * Math.pow(10, nextInt());
	            }
	            if (c < '0' || c > '9')
	            {
	            	throw new InputMismatchException();
	            }
	            res *= 10;
	            res += c & 15;
	            c = read();
	        }
	        if (c == '.')
	        {
	            c = read();
	            double m = 1;
	            while (!isSpaceChar(c))
	            {
	                if (c == 'e' || c == 'E')
	                {
	                	return res * Math.pow(10, nextInt());
	                }
	                if (c < '0' || c > '9')
	                {
	                	throw new InputMismatchException();
	                }
	                m /= 10;
	                res += (c & 15) * m;
	                c = read();
	            }
	        }
	        return res * sgn;
	    }

	    public String next()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        StringBuilder res = new StringBuilder();
	        do
	        {
	            res.appendCodePoint(c);
	            c = read();
	        } while (!isSpaceChar(c));
	        return res.toString();
	    }

	    private boolean isSpaceChar(int c)
	    {
	        return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c;
	    }
	}
}


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main
{
	/********************************************** a list of common variables **********************************************/

	private MyScanner scan = new MyScanner();
	private PrintWriter out = new PrintWriter(System.out);
	private final double PI = Math.acos(-1.0);
	private final int SIZEN = (int)(1e7);
	private final int MOD = (int)(1e9 + 7);

	public void foo()
	{
		int n = scan.nextInt();
		int[] a = new int[n];
		for(int i = 0;i < n;++i)
		{
			a[i] = scan.nextInt();
		}
		int[] left = new int[n];
		int[] right = new int[n];
		Stack<int[]> s = new Stack<int[]>();
		s.push(new int[]{-1, -1}); //p[0]:num, p[1]:index
		for(int i = 0;i < n;++i)
		{
			while(!s.isEmpty())
			{
				int[] p = s.peek();
				if(a[i] >= p[0])
				{
					left[i] = a[i] > p[0] ? p[1] + 1 : left[p[1]];
					break;
				}
				else
				{
					right[p[1]] = i - 1;
					s.pop();
				}
			}
			s.push(new int[]{a[i], i});
		}
		while(s.size() > 1)
		{
			int[] p = s.pop();
			right[p[1]] = n - 1;
		}
		int[] ans = new int[n + 1];
		for(int i = 0;i < n;++i)
		{
			out.println(left[i] + " " + right[i]);
			int d = right[i] - left[i] + 1;
			ans[d] = Math.max(ans[d], a[i]);
		}
		for(int i = n - 1;i >= 1;--i)
		{
			ans[i] = Math.max(ans[i], ans[i + 1]);
		}
		for(int i = 1;i <= n;++i)
		{
			out.print(ans[i] + " ");
		}
	}

	public static void main(String[] args)
	{
		Main m = new Main();
		m.foo();
		m.out.close();
	}

	/********************************************** a list of common algorithms **********************************************/

	/**
	 * KMP match, i.e. kmpMatch("abcd", "bcd") = 1, kmpMatch("abcd", "bfcd") = -1.
	 *
	 * @param t: String to match.
	 * @param p: String to be matched.
	 * @return if can match, first index; otherwise -1.
	 */
	public int kmpMatch(char[] t, char[] p)
	{
		int n = t.length;
		int m = p.length;
		int[] next = new int[m + 1];
		next[0] = -1;
		int j = -1;
		for(int i = 1;i < m;++i)
		{
			while(j >= 0 && p[i] != p[j + 1])
			{
				j = next[j];
			}
			if(p[i] == p[j + 1])
			{
				++j;
			}
			next[i] = j;
		}
		j = -1;
		for(int i = 0;i < n;++i)
		{
			while(j >= 0 && t[i] != p[j + 1])
			{
				j = next[j];
			}
			if(t[i] == p[j + 1])
			{
				++j;
			}
			if(j == m - 1)
			{
				return i - m + 1;
			}
		}
		return -1;
	}

	class MyScanner
	{
	    private byte[] buf = new byte[1024];
	    private int curChar;
	    private int numChars;
	    BufferedInputStream bis = new BufferedInputStream(System.in);

	    public int read()
	    {
	        if (-1 == numChars)
	        {
	        	throw new InputMismatchException();
	        }
	        if (curChar >= numChars)
	        {
	            curChar = 0;
	            try
	            {
	                numChars = bis.read(buf);
	            }
	            catch (IOException e)
	            {
	                throw new InputMismatchException();
	            }
	            if (numChars <= 0)
	            {
	            	return -1;
	            }
	        }
	        return buf[curChar++];
	    }

	    public int nextInt()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        int sgn = 1;
	        if (c == '-')
	        {
	            sgn = -1;
	            c = read();
	        }
	        int res = 0;
	        do
	        {
	            if (c < '0' || c > '9')
	            {
	            	throw new InputMismatchException();
	            }
	            res *= 10;
	            res += c - '0';
	            c = read();
	        } while (!isSpaceChar(c));
	        return res * sgn;
	    }

	    public long nextLong()
	    {
	    	int c = read();
	    	while (isSpaceChar(c))
	    	{
	    		c = read();
	    	}
	    	int sgn = 1;
	    	if (c == '-')
	    	{
	    		sgn = -1;
	    		c = read();
	    	}
	    	long res = 0;
	    	do
	    	{
	    		if (c < '0' || c > '9')
	    		{
	    			throw new InputMismatchException();
	    		}
	    		res *= 10;
	    		res += c - '0';
	    		c = read();
	    	} while (!isSpaceChar(c));
	    	return res * sgn;
	    }

	    public double nextDouble()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        int sgn = 1;
	        if (c == '-')
	        {
	            sgn = -1;
	            c = read();
	        }
	        double res = 0;
	        while (!isSpaceChar(c) && c != '.')
	        {
	            if (c == 'e' || c == 'E')
	            {
	            	return res * Math.pow(10, nextInt());
	            }
	            if (c < '0' || c > '9')
	            {
	            	throw new InputMismatchException();
	            }
	            res *= 10;
	            res += c & 15;
	            c = read();
	        }
	        if (c == '.')
	        {
	            c = read();
	            double m = 1;
	            while (!isSpaceChar(c))
	            {
	                if (c == 'e' || c == 'E')
	                {
	                	return res * Math.pow(10, nextInt());
	                }
	                if (c < '0' || c > '9')
	                {
	                	throw new InputMismatchException();
	                }
	                m /= 10;
	                res += (c & 15) * m;
	                c = read();
	            }
	        }
	        return res * sgn;
	    }

	    public String next()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        StringBuilder res = new StringBuilder();
	        do
	        {
	            res.appendCodePoint(c);
	            c = read();
	        } while (!isSpaceChar(c));
	        return res.toString();
	    }

	    private boolean isSpaceChar(int c)
	    {
	        return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c;
	    }
	}
}


#include <bits/stdc++.h>

using namespace std;

bool sortt(pair<int, int> a, pair<int, int> b) {
    if (a.second != b.second) return a.second > b.second;
    else {
        return a.first > b.first;
    }
}

int main()
{
    int n;

    while (scanf("%d", &n) == 1) {
        int arr[n];

        for (int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
        }

        int parent[n];

        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }

        vector<pair<int, int> > ans;
        ans.push_back(make_pair(1, arr[0]));

        for (int i = 1; i < n; i++) {
            int j = i - 1;
            while (j >= 0 && arr[i] <= arr[j]) {
                j = parent[j] - 1;
            }
            j++;
            ans.push_back(make_pair(i - j + 1, arr[i]));
        }

        reverse(arr, arr + n);

        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }

        for (int i = 1; i < n; i++) {
            int j = i - 1;
            while (j >= 0 && arr[i] <= arr[j]) {
                j = parent[j] - 1;
            }
            j++;
            ans.push_back(make_pair(i - j + 1, arr[i]));
        }

        sort(ans.begin(), ans.end(), sortt);

        int ans1[n];
        memset(ans1, -1, sizeof ans1);

        int j = 0;

        for (int i = 0; i < (int) ans.size(); i++) {
            if (j < ans[i].first) {
                for (; j < ans[i].first; j++) {
                    ans1[j] = ans[i].second;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            if (ans1[i] == -1) {
                ans1[i] = ans1[i - 1];
            }
        }

        for (int i = 0; i < n; i++) {
            printf("%d ", ans1[i]);
        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool sortt(pair<int, int> a, pair<int, int> b) {
    if (a.second != b.second) return a.second > b.second;
    else {
        return a.first > b.first;
    }
}

int main()
{
    int n;

    while (scanf("%d", &n) == 1) {
        int arr[n];

        for (int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
        }

        int parent[n];

        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }

        vector<pair<int, int> > ans;
        ans.push_back(make_pair(1, arr[0]));

        for (int i = 1; i < n; i++) {
            int j = i - 1;
            while (j >= 0 && arr[i] <= arr[j]) {
                j = parent[j] - 1;
            }
            j++;
            parent[i] = j;
        }

        ans.push_back(make_pair(1, arr[n - 1]));

        int parent1[n];

        for (int i = 0; i < n; i++) parent1[i] = i;

        for (int i = n - 2; i >= 0; i--) {
            int j = i + 1;
            while (j < n && arr[i] <= arr[j]) {
                j = parent1[j] + 1;
            }
            j--;
            parent1[i] = j;
        }

        for (int i = 0; i < n; i++) {
            ans.push_back(make_pair(parent1[i] - parent[i] + 1, arr[i]));
        }

        sort(ans.begin(), ans.end(), sortt);

        int ans1[n];
        memset(ans1, -1, sizeof ans1);

        int j = 0;

        for (int i = 0; i < (int) ans.size(); i++) {
            if (j < ans[i].first) {
                for (; j < ans[i].first; j++) {
                    ans1[j] = ans[i].second;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            if (ans1[i] == -1) {
                ans1[i] = ans1[i - 1];
            }
        }

        for (int i = 0; i < n; i++) {
            printf("%d ", ans1[i]);
        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool sortt(pair<int, int> a, pair<int, int> b) {
    if (a.second != b.second) return a.second > b.second;
    else {
        return a.first > b.first;
    }
}

int main()
{
    int n;

    while (scanf("%d", &n) == 1) {
        int arr[n];

        for (int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
        }

        int parent[n];

        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }

        vector<pair<int, int> > ans;
        ans.push_back(make_pair(1, arr[0]));

        for (int i = 1; i < n; i++) {
            int j = i - 1;
            while (arr[i] <= arr[j]) {
                j = parent[j] - 1;
            }
            j++;
            ans.push_back(make_pair(i - j + 1, arr[i]));
        }

        sort(ans.begin(), ans.end(), sortt);

        int ans1[n];
        memset(ans1, -1, sizeof ans1);

        int j = 0;

        for (int i = 0; i < (int) ans.size(); i++) {
            if (j < ans[i].first) {
                for (; j < ans[i].first; j++) {
                    ans1[j] = ans[i].second;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            if (ans1[i] == -1) {
                ans1[i] = ans1[i - 1];
            }
        }

        for (int i = 0; i < n; i++) {
            printf("%d ", ans1[i]);
        }
        printf("\n");
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool sortt(pair<int, int> a, pair<int, int> b) {
    if (a.second != b.second) return a.second > b.second;
    else {
        return a.first > b.first;
    }
}

int main()
{
    int n;

    while (scanf("%d", &n) == 1) {
        int arr[n];

        for (int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
        }

        int parent[n];

        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }

        vector<pair<int, int> > ans;
        ans.push_back(make_pair(1, arr[0]));

        for (int i = 1; i < n; i++) {
            int j = i - 1;
            while (arr[i] <= arr[j] && j >= 0) {
                j = parent[j] - 1;
            }
            j++;
            ans.push_back(make_pair(i - j + 1, arr[i]));
        }

        sort(ans.begin(), ans.end(), sortt);

        int ans1[n];
        memset(ans1, -1, sizeof ans1);

        int j = 0;

        for (int i = 0; i < (int) ans.size(); i++) {
            if (j < ans[i].first) {
                for (; j < ans[i].first; j++) {
                    ans1[j] = ans[i].second;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            if (ans1[i] == -1) {
                ans1[i] = ans1[i - 1];
            }
        }

        for (int i = 0; i < n; i++) {
            printf("%d ", ans1[i]);
        }
        printf("\n");
    }
    return 0;
}


///  http://codeforces.com/contest/548/problem/D
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int,int>
#define V(x) vector<x>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back

#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());

#define FORLL(i,a,n) for(long long int i=(a); i<(n); ++i)
#define FORDLL(i,a,n) for(long long int i=(a);i>=(n);--i)
#define FOR(c, f, t) for(int c=f;c<t;c++)
#define FORD(c, f, t) for(int c=f;c>=t;c--)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define mem(a,k) memset(a,k,sizeof(a))
#define M 1000000007
#define isactive(x,k) (x&(1<<k))

#define SIN(x) cin>>x
#define DIN(x, y) cin>>x>>y
#define TIN(x, y, z) cin>>x>>y>>z
#define FIN(w, x, y, z) cin>>w>>x>>y>>z

#define SOP(x) cout<<x<<" "
#define DOP(x, y) cout<<x<<" "<<y<<"\n"
#define TOP(x, y, z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define FOP(w, x, y, z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<"\n"

#define PRESENT(container, x) container.find(x) != container.end()
#define fi first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
#define MAX INT_MAX
#define MIN INT_MIN
using namespace std;
/*
#define cin fin
#define cout fout

ifstream fin("input.txt");
ofstream fout("output.txt");
*/
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
/*inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}*/
int inp[200005];
map<int,int> power;
int ans[200005];
int main()
{
    int n;
    SIN(n);
    vector<pii> left(n),right(n);  /// value,power
    FOR(i,0,n){
    SIN(inp[i]);
    left[i].fi=inp[i];
    left[i].se=0;
    right[i].se=0;
    right[i].fi=inp[i];
    }

    stack<pii> store; ///  value,index
    store.push({inp[0],0});
    int ind=1,cnt=0;
    while(!store.empty() && ind<n){
        if(inp[ind] < store.top().fi){
            while(!store.empty() && store.top().fi >= inp[ind] ){
                left[ind].se+=(left[store.top().se].se+1);
                store.pop();
            }
            store.push({inp[ind],ind});
        }
        else
            store.push({inp[ind],ind});
        ind++;
    }
    while(!store.empty()){
        store.pop();
    }
    store.push({inp[n-1],n-1});
    ind=n-2;
    while(!store.empty() && ind>=0){
        if(inp[ind] < store.top().fi){

            while(!store.empty() && store.top().fi >= inp[ind]){
                right[ind].se+=(right[store.top().se].se+1);
                store.pop();
            }
           // cout<<inp[ind]<<" "<<right[ind].se<<endl;
            store.push({inp[ind],ind});
        }
        else
        store.push({inp[ind],ind});
        ind--;
    }
    /*FOR(i,0,n)
    DOP(left[i].fi,left[i].se);
    cout<<endl<<endl;
    FOR(i,0,n)
    DOP(right[i].fi,right[i].se);
    cout<<endl<<endl;*/
    FOR(i,0,n)
        power[left[i].fi]=max(power[left[i].fi],left[i].se+right[i].se+1);
    map<int,int> :: reverse_iterator it;
    it=power.rbegin();
    cnt=0;
    while(it!=power.rend() && cnt !=n){
        //DOP(it->fi,it->se);
        int len=it->second;
        while(len){
            if(!ans[len]){
            ans[len]=it->first;
            cnt++;
            }
            len--;
        }
        it++;
    }
    FOR(i,1,n+1)
    SOP(ans[i]);
    return 0;
}


import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.math.BigInteger;

public class Main{

static PrintWriter out;
static InputReader ir;

static void solve(){
 final int[] d={-1,0,1,0};
 int n=ir.nextInt();
 int m=ir.nextInt();
 char[][] map=ir.nextCharMap(n,m);
 UnionFindTree uf=new UnionFindTree(n*m);
 boolean[][] visited=new boolean[n][m];
 int[][] cnt=new int[n][m];
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   if(map[i][j]=='.'){
    for(int k=0;k<4;k++){
     int nx=i+d[k],ny=j+d[k^1];
     if(nx>=0&&nx<n&&ny>=0&&ny<m&&map[nx][ny]=='.'&&!uf.same(i*m+j,nx*m+ny)) uf.unite(i*m+j,nx*m+ny);
    }
   }
  }
 }
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   par[i][j]=uf.find(i*m+j);
  }
 }
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   if(map[i][j]=='*'||visited[i][j]) continue;
   HashSet<int[]> st=new HashSet<>();
   st.add(new int[]{i,j});
   for(int k=i*m+j+1;k<n*m;k++){
    if(visited[(int)(k/m)][k%m]) continue;
    if(par[i][j]==par[(int)(k/m)][k%m]){
     st.add(new int[]{(int)(k/m),k%m});
     visited[(int)(k/m)][k%m]=true;
    }
   }
   for(int[] p : st){
    cnt[p[0]][p[1]]=st.size();
   }
  }
 }
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   if(map[i][j]=='.') out.print(".");
   else{
    int now=1;
    outer:
    for(int k=0;k<4;k++){
     int nx=i+d[k],ny=j+d[k^1];
     if(nx>=0&&nx<n&&ny>=0&&ny<m){
      for(int l=0;l<k;l++){
       int nnx=i+d[l],nny=j+d[l^1];
       if(nnx>=0&&nnx<n&&nny>=0&&nny<m){
        if(uf.same(nx*m+ny,nnx*m+nny)) continue outer;
       }
      }
      now+=cnt[nx][ny];
     }
    }
    out.print(now%10);
   }
  }
  out.println();
 }
}

static class UnionFindTree{

 private int[] par;
 private int[] rank;

 public UnionFindTree(int size){
  par=new int[size];
  rank=new int[size];
  for(int i=0;i<size;i++){
   par[i]=i;
   rank[i]=0;
  }
 }

 public int find(int x){
  if(par[x]==x) return x;
  else return par[x]=find(par[x]);
 }

 public void unite(int x,int y){
  x=find(x);
  y=find(y);
  if(x==y) return;
  else if(rank[x]>rank[y]) par[y]=x;
  else{
   par[x]=y;
   if(rank[x]==rank[y]) rank[y]++;
  }
 }

 public boolean same(int x,int y){
  return find(x)==find(y);
 }
}

public static void main(String[] args) throws Exception{
 ir=new InputReader(System.in);
 out=new PrintWriter(System.out);
 solve();
 out.flush();
}

static class InputReader {
 private InputStream in;
 private byte[] buffer=new byte[1024];
 private int curbuf;
 private int lenbuf;

 public InputReader(InputStream in) {this.in=in; this.curbuf=this.lenbuf=0;}

 public boolean hasNextByte() {
  if(curbuf>=lenbuf){
   curbuf= 0;
   try{
    lenbuf=in.read(buffer);
   }catch(IOException e) {
    throw new InputMismatchException();
   }
   if(lenbuf<=0) return false;
  }
  return true;
 }

 private int readByte(){if(hasNextByte()) return buffer[curbuf++]; else return -1;}

 private boolean isSpaceChar(int c){return !(c>=33&&c<=126);}

 private void skip(){while(hasNextByte()&&isSpaceChar(buffer[curbuf])) curbuf++;}

 public boolean hasNext(){skip(); return hasNextByte();}

 public String next(){
  if(!hasNext()) throw new NoSuchElementException();
  StringBuilder sb=new StringBuilder();
  int b=readByte();
  while(!isSpaceChar(b)){
   sb.appendCodePoint(b);
   b=readByte();
  }
  return sb.toString();
 }

 public int nextInt() {
  if(!hasNext()) throw new NoSuchElementException();
  int c=readByte();
  while (isSpaceChar(c)) c=readByte();
  boolean minus=false;
  if (c=='-') {
   minus=true;
   c=readByte();
  }
  int res=0;
  do{
   if(c<'0'||c>'9') throw new InputMismatchException();
   res=res*10+c-'0';
   c=readByte();
  }while(!isSpaceChar(c));
  return (minus)?-res:res;
 }

 public long nextLong() {
  if(!hasNext()) throw new NoSuchElementException();
  int c=readByte();
  while (isSpaceChar(c)) c=readByte();
  boolean minus=false;
  if (c=='-') {
   minus=true;
   c=readByte();
  }
  long res = 0;
  do{
   if(c<'0'||c>'9') throw new InputMismatchException();
   res=res*10+c-'0';
   c=readByte();
  }while(!isSpaceChar(c));
  return (minus)?-res:res;
 }

 public double nextDouble(){return Double.parseDouble(next());}

 public BigInteger nextBigInteger(){return new BigInteger(next());}

 public int[] nextIntArray(int n){
  int[] a=new int[n];
  for(int i=0;i<n;i++) a[i]=nextInt();
  return a;
 }

 public long[] nextLongArray(int n){
  long[] a=new long[n];
  for(int i=0;i<n;i++) a[i]=nextLong();
  return a;
 }

 public char[][] nextCharMap(int n,int m){
  char[][] map=new char[n][m];
  for(int i=0;i<n;i++) map[i]=next().toCharArray();
  return map;
 }
}
}


import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.math.BigInteger;

public class Main{

static PrintWriter out;
static InputReader ir;

static void solve(){
 final int[] d={-1,0,1,0};
 int n=ir.nextInt();
 int m=ir.nextInt();
 char[][] map=ir.nextCharMap(n,m);
 UnionFindTree uf=new UnionFindTree(n*m);
 boolean[][] visited=new boolean[n][m];
 int[][] cnt=new int[n][m];
 Deque<int[]> que=new ArrayDeque<>();
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   if(map[i][j]=='*'&&!visited[i][j]) continue;
   HashSet<int[]> st=new HashSet<>();
   visited[i][j]=true;
   que.offerLast(new int[]{i,j});
   while(!que.isEmpty()){
    int[] p=que.pollFirst();
    st.add(p);
    int pp=p[0]*n+p[1];
    for(int k=0;k<4;k++){
     int nx=p[0]+d[k];
     int ny=p[1]+d[k^1];
     if(nx>=0&&nx<n&&ny>=0&&ny<m&&!visited[nx][ny]&&map[nx][ny]=='.'){
      que.offerLast(new int[]{nx,ny});
      visited[nx][ny]=true;
     }
    }
   }
   for(int[] p : st) cnt[p[0]][p[1]]=st.size();
  }
 }
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   if(map[i][j]=='.'){
    for(int k=0;k<4;k++){
     int nx=i+d[k],ny=j+d[k^1];
     if(nx>=0&&nx<n&&ny>=0&&ny<m&&map[nx][ny]=='.'&&!uf.same(i*n+j,nx,ny)) uf.unite(i*n+j,nx,ny);
    }
   }
  }
 }
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   if(map[i][j]=='.') out.print(".");
   else{
    int now=1;
    outer:
    for(int k=0;k<4;k++){
     int nx=i+d[k],ny=j+d[k^1];
     if(nx>=0&&nx<n&&ny>=0&&ny<m){
      for(int l=0;l<k;l++){
       int nnx=i+d[l],nny=j+d[l^1];
       if(nnx>=0&&nnx<n&&nny>=0&&nny<m){
        if(uf.same(nx*n+ny,nnx*n+nny)) continue outer;
       }
      }
      now+=cnt[nx][ny];
     }
    }
    out.print(now%10);
   }
  }
  out.println();
 }
}

static class UnionFindTree{

 private int[] par;
 private int[] rank;

 public UnionFindTree(int size){
  par=new int[size];
  rank=new int[size];
  for(int i=0;i<size;i++){
   par[i]=i;
   rank[i]=0;
  }
 }

 private int find(int x){
  if(par[x]==x) return x;
  else return par[x]=find(par[x]);
 }

 public void unite(int x,int y){
  x=find(x);
  y=find(y);
  if(x==y) return;
  else if(rank[x]>rank[y]) par[y]=x;
  else{
   par[x]=y;
   if(rank[x]==rank[y]) rank[y]++;
  }
 }

 public boolean same(int x,int y){
  return find(x)==find(y);
 }
}

public static void main(String[] args) throws Exception{
 ir=new InputReader(System.in);
 out=new PrintWriter(System.out);
 solve();
 out.flush();
}

static class InputReader {
 private InputStream in;
 private byte[] buffer=new byte[1024];
 private int curbuf;
 private int lenbuf;

 public InputReader(InputStream in) {this.in=in; this.curbuf=this.lenbuf=0;}

 public boolean hasNextByte() {
  if(curbuf>=lenbuf){
   curbuf= 0;
   try{
    lenbuf=in.read(buffer);
   }catch(IOException e) {
    throw new InputMismatchException();
   }
   if(lenbuf<=0) return false;
  }
  return true;
 }

 private int readByte(){if(hasNextByte()) return buffer[curbuf++]; else return -1;}

 private boolean isSpaceChar(int c){return !(c>=33&&c<=126);}

 private void skip(){while(hasNextByte()&&isSpaceChar(buffer[curbuf])) curbuf++;}

 public boolean hasNext(){skip(); return hasNextByte();}

 public String next(){
  if(!hasNext()) throw new NoSuchElementException();
  StringBuilder sb=new StringBuilder();
  int b=readByte();
  while(!isSpaceChar(b)){
   sb.appendCodePoint(b);
   b=readByte();
  }
  return sb.toString();
 }

 public int nextInt() {
  if(!hasNext()) throw new NoSuchElementException();
  int c=readByte();
  while (isSpaceChar(c)) c=readByte();
  boolean minus=false;
  if (c=='-') {
   minus=true;
   c=readByte();
  }
  int res=0;
  do{
   if(c<'0'||c>'9') throw new InputMismatchException();
   res=res*10+c-'0';
   c=readByte();
  }while(!isSpaceChar(c));
  return (minus)?-res:res;
 }

 public long nextLong() {
  if(!hasNext()) throw new NoSuchElementException();
  int c=readByte();
  while (isSpaceChar(c)) c=readByte();
  boolean minus=false;
  if (c=='-') {
   minus=true;
   c=readByte();
  }
  long res = 0;
  do{
   if(c<'0'||c>'9') throw new InputMismatchException();
   res=res*10+c-'0';
   c=readByte();
  }while(!isSpaceChar(c));
  return (minus)?-res:res;
 }

 public double nextDouble(){return Double.parseDouble(next());}

 public BigInteger nextBigInteger(){return new BigInteger(next());}

 public int[] nextIntArray(int n){
  int[] a=new int[n];
  for(int i=0;i<n;i++) a[i]=nextInt();
  return a;
 }

 public long[] nextLongArray(int n){
  long[] a=new long[n];
  for(int i=0;i<n;i++) a[i]=nextLong();
  return a;
 }

 public char[][] nextCharMap(int n,int m){
  char[][] map=new char[n][m];
  for(int i=0;i<n;i++) map[i]=next().toCharArray();
  return map;
 }
}
}


import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.math.BigInteger;

public class Main{

static PrintWriter out;
static InputReader ir;

static void solve(){
 final int[] d={-1,0,1,0};
 int n=ir.nextInt();
 int m=ir.nextInt();
 char[][] map=ir.nextCharMap(n,m);
 UnionFindTree uf=new UnionFindTree(n*m);
 boolean[][] visited=new boolean[n][m];
 int[][] cnt=new int[n][m];
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   if(map[i][j]=='.'){
    for(int k=0;k<4;k++){
     int nx=i+d[k],ny=j+d[k^1];
     if(nx>=0&&nx<n&&ny>=0&&ny<m&&map[nx][ny]=='.'&&!uf.same(i*m+j,nx*m+ny)) uf.unite(i*m+j,nx*m+ny);
    }
   }
  }
 }
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   if(map[i][j]=='*'||visited[i][j]) continue;
   HashSet<int[]> st=new HashSet<>();
   st.add(new int[]{i,j});
   for(int k=i*m+j+1;k<n*m;k++){
    if(uf.same(i*m+j,k)) st.add(new int[]{(int)(k/m),k%m});
    visited[(int)(k/m)][k%m]=true;
   }
   for(int[] p : st){
    cnt[p[0]][p[1]]=st.size();
   }
  }
 }
 for(int i=0;i<n;i++){
  for(int j=0;j<m;j++){
   if(map[i][j]=='.') out.print(".");
   else{
    int now=1;
    outer:
    for(int k=0;k<4;k++){
     int nx=i+d[k],ny=j+d[k^1];
     if(nx>=0&&nx<n&&ny>=0&&ny<m){
      for(int l=0;l<k;l++){
       int nnx=i+d[l],nny=j+d[l^1];
       if(nnx>=0&&nnx<n&&nny>=0&&nny<m){
        if(uf.same(nx*m+ny,nnx*m+nny)) continue outer;
       }
      }
      now+=cnt[nx][ny];
     }
    }
    out.print(now%10);
   }
  }
  out.println();
 }
}

static class UnionFindTree{

 private int[] par;
 private int[] rank;

 public UnionFindTree(int size){
  par=new int[size];
  rank=new int[size];
  for(int i=0;i<size;i++){
   par[i]=i;
   rank[i]=0;
  }
 }

 private int find(int x){
  if(par[x]==x) return x;
  else return par[x]=find(par[x]);
 }

 public void unite(int x,int y){
  x=find(x);
  y=find(y);
  if(x==y) return;
  else if(rank[x]>rank[y]) par[y]=x;
  else{
   par[x]=y;
   if(rank[x]==rank[y]) rank[y]++;
  }
 }

 public boolean same(int x,int y){
  return find(x)==find(y);
 }
}

public static void main(String[] args) throws Exception{
 ir=new InputReader(System.in);
 out=new PrintWriter(System.out);
 solve();
 out.flush();
}

static class InputReader {
 private InputStream in;
 private byte[] buffer=new byte[1024];
 private int curbuf;
 private int lenbuf;

 public InputReader(InputStream in) {this.in=in; this.curbuf=this.lenbuf=0;}

 public boolean hasNextByte() {
  if(curbuf>=lenbuf){
   curbuf= 0;
   try{
    lenbuf=in.read(buffer);
   }catch(IOException e) {
    throw new InputMismatchException();
   }
   if(lenbuf<=0) return false;
  }
  return true;
 }

 private int readByte(){if(hasNextByte()) return buffer[curbuf++]; else return -1;}

 private boolean isSpaceChar(int c){return !(c>=33&&c<=126);}

 private void skip(){while(hasNextByte()&&isSpaceChar(buffer[curbuf])) curbuf++;}

 public boolean hasNext(){skip(); return hasNextByte();}

 public String next(){
  if(!hasNext()) throw new NoSuchElementException();
  StringBuilder sb=new StringBuilder();
  int b=readByte();
  while(!isSpaceChar(b)){
   sb.appendCodePoint(b);
   b=readByte();
  }
  return sb.toString();
 }

 public int nextInt() {
  if(!hasNext()) throw new NoSuchElementException();
  int c=readByte();
  while (isSpaceChar(c)) c=readByte();
  boolean minus=false;
  if (c=='-') {
   minus=true;
   c=readByte();
  }
  int res=0;
  do{
   if(c<'0'||c>'9') throw new InputMismatchException();
   res=res*10+c-'0';
   c=readByte();
  }while(!isSpaceChar(c));
  return (minus)?-res:res;
 }

 public long nextLong() {
  if(!hasNext()) throw new NoSuchElementException();
  int c=readByte();
  while (isSpaceChar(c)) c=readByte();
  boolean minus=false;
  if (c=='-') {
   minus=true;
   c=readByte();
  }
  long res = 0;
  do{
   if(c<'0'||c>'9') throw new InputMismatchException();
   res=res*10+c-'0';
   c=readByte();
  }while(!isSpaceChar(c));
  return (minus)?-res:res;
 }

 public double nextDouble(){return Double.parseDouble(next());}

 public BigInteger nextBigInteger(){return new BigInteger(next());}

 public int[] nextIntArray(int n){
  int[] a=new int[n];
  for(int i=0;i<n;i++) a[i]=nextInt();
  return a;
 }

 public long[] nextLongArray(int n){
  long[] a=new long[n];
  for(int i=0;i<n;i++) a[i]=nextLong();
  return a;
 }

 public char[][] nextCharMap(int n,int m){
  char[][] map=new char[n][m];
  for(int i=0;i<n;i++) map[i]=next().toCharArray();
  return map;
 }
}
}