#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int MAXN = 5e5 + 5;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int n;
ll a[MAXN], s[4 * MAXN], lazy[4 * MAXN], b[MAXN];

void build(int id = 1, int l = 0, int r = n){
	if((r - l) < 2){
		s[id] = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build(id * 2, l, mid);
	build(id * 2 + 1, mid, r);
	s[id] = (s[id * 2] + s[id * 2 + 1]);
}

void update(int id, int l, int r, ll x){
	lazy[id] += x;
	if(x) s[id] = (r - l) * x;
}

void shift(int id, int l, int r){
	int mid = (l + r) / 2;
	update(id * 2, l, mid, lazy[id]);
	update(id * 2 + 1, mid, r, lazy[id]);
	lazy[id] = 0;
}

void increase(int x, int y, ll u, int id = 1, int l = 0, int r = n){
	if(x >= r || l >= y) return;
	if(x <= l && r <= y){
		update(id, l, r, u);
		return;
	}
	shift(id, l, r);
	int mid = (l + r) / 2;
	increase(x, y, u, id * 2, l, mid);
	increase(x, y, u, id * 2 + 1, mid, r);
	s[id] = s[id * 2] + s[id * 2 + 1];
}

ll sum(int x, int y, int id = 1, int l = 0, int r = n){
	if(x >= r || l >= y) return 0;
	if(x <= l && r <= y) return s[id];
	shift(id, l, r);
	int mid = (l + r) / 2;
	return (sum(x, y, id * 2, l, mid) + sum(x, y, id * 2 + 1, mid, r));
}

ll dp[MAXN];
ll dp1[MAXN];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	cin >> n;
	rep(i, n) cin >> a[i], b[i] = a[i];
	build();
	memset(dp, 0, sizeof(dp));
	memset(dp1, 0, sizeof(dp1));
	dp[0] = a[0]; //base case
	repn(i, 1, n){
		dp[i] = dp[i - 1];
		dp[i] += a[i];
		if(a[i] < sum(i - 1, i)){ //we have a problem
			//binary search for the first one that is larger than a[i];
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) / 2;
				if(sum(mid, mid + 1) >= a[i]) r = mid;
				else l = mid + 1;
			}
			increase(l, i, a[i]);
			dp[i] = sum(0, i + 1);
		}
		//debug(dp[i]);
	}
	rep(i, n) a[i] = b[i];
	reverse(a, a + n);
	memset(s, 0LL, sizeof(s));
	memset(lazy, 0LL, sizeof(lazy));
	build();
	dp1[0] = a[0];
	repn(i, 1, n){
		dp1[i] = dp1[i - 1];
		dp1[i] += a[i];
		if(a[i] < sum(i - 1, i)){ //we have a problem
			//binary search for the first one that is larger than a[i];
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) / 2;
				if(sum(mid, mid + 1) >= a[i]) r = mid;
				else l = mid + 1;
			}
			increase(l, i, a[i]);
			dp1[i] = sum(0, i + 1);
		}
		//debug(dp1[i]);
	}
	int ind = 0;
	ll bst = 0;
	repn(i, 1, n - 1){
		//debug2(i, n - i - 1);
		//debug(b[i]);
		ll cur = dp[i] + dp1[n - i - 1] - b[i]; //avoid double counting
		//debug(cur);
		if(cur > bst) bst = cur, ind = i;
	}
	if(bst < (dp1[n - 1])) bst = dp1[n - 1], ind = 0;
	if(bst < (dp[n - 1])) bst = dp[n - 1], ind = n - 1;
	ll ans = b[ind];
	ll mn = b[ind];
	vector<ll> res;
	for(int j = ind - 1; j >= 0; j--){
		res.pb(min(mn, b[j]));
		ans += min(mn, b[j]);
		mn = min(mn, b[j]); //can't be larger
	}
	reverse(all(res));
	res.pb(b[ind]);
	mn = b[ind];
	for(int j = ind + 1; j < n; j++){
		res.pb(min(mn, b[j]));
		ans += min(mn, b[j]);
		mn = min(mn, b[j]); //can't be larger
	}
	for(ll x : res) cout << x << " ";
	cout << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int MAXN = 5e5 + 5;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int n;
ll a[MAXN], s[4 * MAXN], lazy[4 * MAXN], b[MAXN], marked[4 * MAXN];

void build(int id = 1, int l = 0, int r = n){
	if((r - l) < 2){
		s[id] = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build(id << 1, l, mid);
	build((id << 1) + 1, mid, r);
	s[id] = (s[id << 1] + s[(id << 1) + 1]);
}

void down(int id, int l, int r){
	if(lazy[id]){
		s[id] = 1LL * lazy[id] * (r - l);
		if((r - l) > 1){
			lazy[id << 1] = lazy[id];
			lazy[(id << 1) + 1] = lazy[id];
		}
	}
	lazy[id] = 0;
}

void increase(int x, int y, ll u, int id = 1, int l = 0, int r = n){
	down(id, l, r);
	if(x >= r || l >= y) return;
	if(x <= l && r <= y){
		lazy[id] = u;
		down(id, l, r);
		return;
	}
	int mid = (l + r) / 2;
	increase(x, y, u, id << 1, l, mid);
	increase(x, y, u, (id << 1) + 1, mid, r);
	s[id] = s[id << 1] + s[(id << 1) + 1];
}

ll sum(int x, int y, int id = 1, int l = 0, int r = n){
	down(id, l, r);
	if(x >= r || l >= y) return 0;
	if(x <= l && r <= y) return s[id];
	int mid = (l + r) / 2;
	return (sum(x, y, id * 2, l, mid) + sum(x, y, id * 2 + 1, mid, r));
}

ll dp[MAXN];
ll dp1[MAXN];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	cin >> n;
	rep(i, n) cin >> a[i], b[i] = a[i];
	memset(dp, 0LL, sizeof(dp));
	memset(dp1, 0LL, sizeof(dp1));
	memset(s, 0LL, sizeof(s));
	memset(lazy, 0LL, sizeof(lazy));
	build();
	dp[0] = a[0]; //base case
	repn(i, 1, n){
		dp[i] = dp[i - 1];
		dp[i] += a[i];
		if(a[i] < sum(i - 1, i)){ //we have a problem
			//binary search for the first one that is larger than a[i];
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) / 2;
				if(sum(mid, mid + 1) >= a[i]) r = mid;
				else l = mid + 1;
			}
			increase(l, i, a[i]);
			dp[i] = sum(0, i + 1);
		}
	}
	rep(i, n) a[i] = b[i];
	reverse(a, a + n);
	memset(s, 0LL, sizeof(s));
	memset(lazy, 0LL, sizeof(lazy));
	build();
	dp1[0] = a[0];
	repn(i, 1, n){
		dp1[i] = dp1[i - 1];
		dp1[i] += a[i];
		if(a[i] < sum(i - 1, i)){ //we have a problem
			//binary search for the first one that is larger than a[i];
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) / 2;
				if(sum(mid, mid + 1) >= a[i]) r = mid;
				else l = mid + 1;
			}
			increase(l, i, a[i]);
			dp1[i] = sum(0, i + 1);
		}
	}
	int ind = 0;
	ll bst = 0;
	repn(i, 0, n){
		ll cur = dp[i] + dp1[n - i - 1] - b[i]; //avoid double counting
		if(cur > bst) bst = cur, ind = i;
	}
	if(bst < (dp1[n - 1])) bst = dp1[n - 1], ind = 0;
	if(bst < (dp[n - 1])) bst = dp[n - 1], ind = n - 1;
	ll ans = b[ind];
	ll mn = b[ind];
	vector<ll> res;
	for(int j = ind - 1; j >= 0; j--){
		res.pb(min(mn, b[j]));
		ans += min(mn, b[j]);
		mn = min(mn, b[j]); //can't be larger
	}
	reverse(all(res));
	res.pb(b[ind]);
	mn = b[ind];
	for(int j = ind + 1; j < n; j++){
		res.pb(min(mn, b[j]));
		ans += min(mn, b[j]);
		mn = min(mn, b[j]); //can't be larger
	}
	for(ll x : res) cout << x << " ";
	cout << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int MAXN = 5e5 + 5;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int n;
ll a[MAXN], s[4 * MAXN], lazy[4 * MAXN], b[MAXN], marked[4 * MAXN];

void build(int id = 1, int l = 0, int r = n){
	if((r - l) < 2){
		s[id] = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build(id * 2, l, mid);
	build(id * 2 + 1, mid, r);
	s[id] = (s[id * 2] + s[id * 2 + 1]);
}

void down(int id, int l, int r){
	if(lazy[id]){
		s[id] = 1LL * lazy[id] * (r - l);
		if((r - l) > 1){
			lazy[2 * id] = lazy[id];
			lazy[2 * id + 1] = lazy[id];
		}
	}
	lazy[id] = 0;
}

void increase(int x, int y, ll u, int id = 1, int l = 0, int r = n){
	down(id, l, r);
	if(x >= r || l >= y) return;
	if(x <= l && r <= y){
		lazy[id] = u;
		down(id, l, r);
		return;
	}
	int mid = (l + r) / 2;
	increase(x, y, u, id * 2, l, mid);
	increase(x, y, u, id * 2 + 1, mid, r);
	s[id] = s[id * 2] + s[id * 2 + 1];
}

ll sum(int x, int y, int id = 1, int l = 0, int r = n){
	down(id, l, r);
	if(x >= r || l >= y) return 0;
	if(x <= l && r <= y) return s[id];
	int mid = (l + r) / 2;
	return (sum(x, y, id * 2, l, mid) + sum(x, y, id * 2 + 1, mid, r));
}

ll dp[MAXN];
ll dp1[MAXN];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	cin >> n;
	rep(i, n) cin >> a[i], b[i] = a[i];
	memset(dp, 0LL, sizeof(dp));
	memset(dp1, 0LL, sizeof(dp1));
	memset(s, 0LL, sizeof(s));
	memset(lazy, 0LL, sizeof(lazy));
	build();
	dp[0] = a[0]; //base case
	repn(i, 1, n){
		dp[i] = dp[i - 1];
		dp[i] += a[i];
		if(a[i] < sum(i - 1, i)){ //we have a problem
			//binary search for the first one that is larger than a[i];
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) / 2;
				if(sum(mid, mid + 1) >= a[i]) r = mid;
				else l = mid + 1;
			}
			increase(l, i, a[i]);
			dp[i] = sum(0, i + 1);
		}
	}
	rep(i, n) a[i] = b[i];
	reverse(a, a + n);
	memset(s, 0LL, sizeof(s));
	memset(lazy, 0LL, sizeof(lazy));
	build();
	dp1[0] = a[0];
	repn(i, 1, n){
		dp1[i] = dp1[i - 1];
		dp1[i] += a[i];
		if(a[i] < sum(i - 1, i)){ //we have a problem
			//binary search for the first one that is larger than a[i];
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) / 2;
				if(sum(mid, mid + 1) >= a[i]) r = mid;
				else l = mid + 1;
			}
			increase(l, i, a[i]);
			dp1[i] = sum(0, i + 1);
		}
	}
	int ind = 0;
	ll bst = 0;
	repn(i, 0, n){
		ll cur = dp[i] + dp1[n - i - 1] - b[i]; //avoid double counting
		if(cur > bst) bst = cur, ind = i;
	}
	if(bst < (dp1[n - 1])) bst = dp1[n - 1], ind = 0;
	if(bst < (dp[n - 1])) bst = dp[n - 1], ind = n - 1;
	ll ans = b[ind];
	ll mn = b[ind];
	vector<ll> res;
	for(int j = ind - 1; j >= 0; j--){
		res.pb(min(mn, b[j]));
		ans += min(mn, b[j]);
		mn = min(mn, b[j]); //can't be larger
	}
	reverse(all(res));
	res.pb(b[ind]);
	mn = b[ind];
	for(int j = ind + 1; j < n; j++){
		res.pb(min(mn, b[j]));
		ans += min(mn, b[j]);
		mn = min(mn, b[j]); //can't be larger
	}
	for(ll x : res) cout << x << " ";
	cout << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int MAXN = 5e5 + 5;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int n;
ll a[MAXN], s[4 * MAXN], lazy[4 * MAXN], b[MAXN];

void build(int id = 1, int l = 0, int r = n){
	if((r - l) < 2){
		s[id] = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build(id * 2, l, mid);
	build(id * 2 + 1, mid, r);
	s[id] = (s[id * 2] + s[id * 2 + 1]);
}

void update(int id, int l, int r, ll x){
	lazy[id] += x;
	if(x) s[id] = (r - l) * x;
}

void shift(int id, int l, int r){
	int mid = (l + r) / 2;
	update(id * 2, l, mid, lazy[id]);
	update(id * 2 + 1, mid, r, lazy[id]);
	lazy[id] = 0;
}

void increase(int x, int y, ll u, int id = 1, int l = 0, int r = n){
	if(x >= r || l >= y) return;
	if(x <= l && r <= y){
		update(id, l, r, u);
		return;
	}
	shift(id, l, r);
	int mid = (l + r) / 2;
	increase(x, y, u, id * 2, l, mid);
	increase(x, y, u, id * 2 + 1, mid, r);
	s[id] = s[id * 2] + s[id * 2 + 1];
}

ll sum(int x, int y, int id = 1, int l = 0, int r = n){
	if(x >= r || l >= y) return 0;
	if(x <= l && r <= y) return s[id];
	shift(id, l, r);
	int mid = (l + r) / 2;
	return (sum(x, y, id * 2, l, mid) + sum(x, y, id * 2 + 1, mid, r));
}

ll dp[MAXN];
ll dp1[MAXN];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	cin >> n;
	rep(i, n) cin >> a[i], b[i] = a[i];
	build();
	memset(dp, 0, sizeof(dp));
	memset(dp1, 0, sizeof(dp1));
	dp[0] = a[0]; //base case
	repn(i, 1, n){
		dp[i] = dp[i - 1];
		dp[i] += a[i];
		if(a[i] < sum(i - 1, i)){ //we have a problem
			//binary search for the first one that is larger than a[i];
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) / 2;
				if(sum(mid, mid + 1) >= a[i]) r = mid;
				else l = mid + 1;
			}
			increase(l, i, a[i]);
			dp[i] = sum(0, i + 1);
		}
		//debug(dp[i]);
	}
	rep(i, n) a[i] = b[i];
	reverse(a, a + n);
	memset(s, 0LL, sizeof(s));
	memset(lazy, 0LL, sizeof(lazy));
	build();
	dp1[0] = a[0];
	repn(i, 1, n){
		dp1[i] = dp1[i - 1];
		dp1[i] += a[i];
		if(a[i] < sum(i - 1, i)){ //we have a problem
			//binary search for the first one that is larger than a[i];
			int l = 0, r = i;
			while(l < r){
				int mid = (l + r) / 2;
				if(sum(mid, mid + 1) >= a[i]) r = mid;
				else l = mid + 1;
			}
			increase(l, i, a[i]);
			dp1[i] = sum(0, i + 1);
		}
		//debug(dp1[i]);
	}
	int ind = 0;
	ll bst = 0;
	repn(i, 1, n){
		//debug2(i, n - i - 1);
		//debug(b[i]);
		ll cur = dp[i] + dp1[n - i - 1] - b[i]; //avoid double counting
		//debug(cur);
		if(cur > bst) bst = cur, ind = i;
	}
	if(bst < (dp1[n - 1])) bst = dp1[n - 1], ind = 0;
	if(bst < (dp[n - 1])) bst = dp[n - 1], ind = n - 1;
	ll ans = b[ind];
	ll mn = b[ind];
	vector<ll> res;
	for(int j = ind - 1; j >= 0; j--){
		res.pb(min(mn, b[j]));
		ans += min(mn, b[j]);
		mn = min(mn, b[j]); //can't be larger
	}
	reverse(all(res));
	res.pb(b[ind]);
	mn = b[ind];
	for(int j = ind + 1; j < n; j++){
		res.pb(min(mn, b[j]));
		ans += min(mn, b[j]);
		mn = min(mn, b[j]); //can't be larger
	}
	for(ll x : res) cout << x << " ";
	cout << endl;
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/


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

#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define all(v) v.begin(),v.end()
#define pll pair<long long,long long>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(ll i=0;i<n;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define repab(i,a,b) for(ll i=a;i<b;i++)
#define vvi vector<vector<int>>
#define vs vector<string>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define vc vector<char>
#define popf pop_front

ll mod=1e9+7;

void solve(){
    ll n;
    cin>>n;
    vll m(n);
    rep(i,n) cin>>m[i];
    vll l(n);
    l[n-1]=0;
    ll p=0;
    repr(i,n-1){
        l[i]=l[i+1]+max(m[i+1]-m[i],p);
    }

    vll r(n);
    r[0]=0;

    repab(i,1,n){
        r[i]=r[i-1]+max(m[i-1]-m[i],p);
    }

    ll mini=mod;
    ll ind=-1;

    rep(i,n){
        if(l[i]+r[i]<mini){
            mini=l[i]+r[i];
            ind=i;
        }
    }

    vll v(n);
    v[ind]=m[ind];

    repab(i,ind+1,n){
        v[i]=min(m[i],m[i-1]);
    }

    repr(i,ind){
        v[i]=min(m[i],m[i+1]);
    }

    for(auto x:v){
        cout<<x<<" ";
    }

    cout<<endl;

    /*vll l(n);
    l[0]=0;
    ll p=0;

    for(ll i=n-1;i>0;i--){
        l[i]=max(m[i-1]-m[i],p);
    }

    for(ll i=n-2;i>=0;i--){
        l[i]+=l[i+1];
        cout<<l[i]<<" ";
    }

    vll r(n);
    r[n-1]=0;
    cout<<endl;

    rep(i,n-1){
        r[i]=max(m[i+1]-m[i],p);
    }

    rep(i,n){
        r[i]+=r[i-1];
        cout<<r[i]<<" ";
    }

    cout<<endl;

    ll mini=mod;
    ll ind=-1;

    rep(i,n){
        if(l[i]+r[i]<mini){
            mini=l[i]+r[i];
            cout<<mini<<endl;
            ind=i;
        }
    }

    vll v(n);
    v[ind]=m[ind];

    repab(i,ind+1,n){
        v[i]=min(m[i],m[i-1]);
    }

    repr(i,ind){
        v[i]=min(m[i],m[i+1]);
    }

    for(auto x:v){
        cout<<x<<" ";
    }

    cout<<endl;*/
}

int main()
{

    ll k;
    k=1;
    //cin>>k;
    while(k--){
        solve();

    }

    return 0;
}


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

#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define all(v) v.begin(),v.end()
#define pll pair<long long,long long>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define repab(i,a,b) for(int i=a;i<b;i++)
#define vvi vector<vector<int>>
#define vs vector<string>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define vc vector<char>
#define popf pop_front

ll mod=1e9+7;

void solve(){
    ll n;
    cin>>n;
    vll v(n);
    rep(i,n) cin>>v[i];

    vll l(n);
    l[0]=0;
    deque<ll> q;
    q.push_front(0);
    repab(i,1,n){
        if(v[q.front()]<=v[i]){
            l[i]=l[i-1];
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                l[i]=q.front();
            }else{
                l[i]=i;
            }
            q.push_front(i);
        }
    }

    vll r(n);
    r[n-1]=n-1;
    deque<ll> p;
    p.push_front(n-1);
    repr(i,n-1){
        if(v[q.front()]<=v[i]){
            r[i]=r[i+1];
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                r[i]=q.front();
            }else{
                r[i]=i;
            }
            q.push_front(i);
        }
    }

    vll left(n);
    vll right(n);
   // l[0]=v[0];

    rep(i,n){
        left[i]=(i-l[i])*v[i]+left[l[i]];
    }
    repr(i,n){
        right[i]=(r[i]-i)*v[i]+right[r[i]];
    }

    ll maxi=-1;
    ll ind=-1;

    rep(i,n){
        if(left[i]+right[i]-v[i]>maxi){
            maxi=left[i]+right[i]-v[i];
            ind=i;
        }
    }

    vll a(n);
    a[ind]=v[ind];

    repr(i,ind){
        a[i]=min(a[i+1],v[i]);
    }
    repab(i,ind+1,n){
        a[i]=min(a[i-1],v[i]);
    }

    for(auto x:a){
        cout<<x<<" ";
    }

    cout<<endl;

}

int main()
{

    ll k;
    k=1;
   // cin>>k;
    while(k--){
        solve();

    }

    return 0;
}


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

#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define all(v) v.begin(),v.end()
#define pll pair<long long,long long>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(ll i=0;i<n;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define repab(i,a,b) for(ll i=a;i<b;i++)
#define vvi vector<vector<int>>
#define vs vector<string>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define vc vector<char>
#define popf pop_front

ll mod=1e9+7;

void solve(){
    ll n;
    cin>>n;
    vll m(n);
    rep(i,n) cin>>m[i];
    vll l(n);
    l[0]=0;
    repab(i,1,n){
        l[i]=m[i]-m[i-1];
    }
    repab(i,1,n){
        l[i]+=l[i-1];
    }

    vll r(n);
    r[n-1]=0;

    repr(i,n-1){
        r[i]=m[i]-m[i+1];
    }
    repr(i,n-1){
        r[i]+=r[i+1];
    }

    ll max=-1;
    ll ind=-1;

    rep(i,n){
        if(l[i]+r[i]>max){
            max=l[i]+r[i];
            ind=i;
        }
    }

    vll v(n);
    v[ind]=m[ind];

    repab(i,ind+1,n){
        v[i]=min(m[i],m[i-1]);
    }

    repr(i,ind){
        v[i]=min(m[i],m[i+1]);
    }

    for(auto x:v){
        cout<<x<<" ";
    }

    cout<<endl;
}

int main()
{

    ll k;
    k=1;
    //cin>>k;
    while(k--){
        solve();

    }

    return 0;
}


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

#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define all(v) v.begin(),v.end()
#define pll pair<long long,long long>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(ll i=0;i<n;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define repab(i,a,b) for(ll i=a;i<b;i++)
#define vvi vector<vector<int>>
#define vs vector<string>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define vc vector<char>
#define popf pop_front

ll mod=1e15+7;

void solve(){
    ll n;
    cin>>n;
    vll m(n);
    rep(i,n) cin>>m[i];
    vll l(n);
    l[n-1]=0;
    ll p=0;
    repr(i,n-1){
        l[i]=l[i+1]+max(m[i+1]-m[i],p);
    }

    vll r(n);
    r[0]=0;

    repab(i,1,n){
        r[i]=r[i-1]+max(m[i-1]-m[i],p);
    }

    ll mini=mod;
    ll ind=-1;

    rep(i,n){
        if(l[i]+r[i]<mini){
            mini=l[i]+r[i];
            ind=i;
        }
    }

    vll v(n);
    v[ind]=m[ind];

    repab(i,ind+1,n){
        v[i]=min(m[i],m[i-1]);
    }

    repr(i,ind){
        v[i]=min(m[i],m[i+1]);
    }

    for(auto x:v){
        cout<<x<<" ";
    }

    cout<<endl;

    /*vll l(n);
    l[0]=0;
    ll p=0;

    for(ll i=n-1;i>0;i--){
        l[i]=max(m[i-1]-m[i],p);
    }

    for(ll i=n-2;i>=0;i--){
        l[i]+=l[i+1];
        cout<<l[i]<<" ";
    }

    vll r(n);
    r[n-1]=0;
    cout<<endl;

    rep(i,n-1){
        r[i]=max(m[i+1]-m[i],p);
    }

    rep(i,n){
        r[i]+=r[i-1];
        cout<<r[i]<<" ";
    }

    cout<<endl;

    ll mini=mod;
    ll ind=-1;

    rep(i,n){
        if(l[i]+r[i]<mini){
            mini=l[i]+r[i];
            cout<<mini<<endl;
            ind=i;
        }
    }

    vll v(n);
    v[ind]=m[ind];

    repab(i,ind+1,n){
        v[i]=min(m[i],m[i-1]);
    }

    repr(i,ind){
        v[i]=min(m[i],m[i+1]);
    }

    for(auto x:v){
        cout<<x<<" ";
    }

    cout<<endl;*/
}

int main()
{

    ll k;
    k=1;
    //cin>>k;
    while(k--){
        solve();

    }

    return 0;
}


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

#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define all(v) v.begin(),v.end()
#define pll pair<long long,long long>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define repab(i,a,b) for(int i=a;i<b;i++)
#define vvi vector<vector<int>>
#define vs vector<string>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define vc vector<char>
#define popf pop_front

ll mod=1e9+7;

void solve(){
    ll n;
    cin>>n;
    vll v(n);
    rep(i,n) cin>>v[i];

    vll l(n);
    l[0]=0;
    deque<ll> q;
    q.push_front(0);
    repab(i,1,n){
        if(v[q.front()]<=v[i]){
            l[i]=q.front();
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                l[i]=q.front();
            }else{
                l[i]=i;
            }
            q.push_front(i);
        }
    }

    vll r(n);
    r[n-1]=n-1;
    deque<ll> p;
    p.push_front(n-1);
    repr(i,n-1){
        if(v[p.front()]<=v[i]){
            r[i]=p.front();
            p.push_front(i);
        }else{
            while(v[p.front()]>v[i]){
                p.pop_front();
                if(q.size()==0) break;
            }
            if(p.size()){
                r[i]=p.front();
            }else{
                l[i]=i;
            }
            p.push_front(i);
        }
    }

    vll left(n);
    vll right(n);
    //left[0]=v[0];

    repab(i,1,n){
        if(l[i]==i){
            left[l[i]]=v[i];
            continue;
        }
        left[i]=(i-l[i])*v[i]+left[l[i]];
    }
    repr(i,n){
        if(r[i]==i){
            right[r[i]]=v[i];
            continue;
        }
        right[i]=(r[i]-i)*v[i]+right[r[i]];
    }

    ll maxi=-1;
    ll ind=-1;

    rep(i,n){
        if(left[i]+right[i]-v[i]>maxi){
            maxi=left[i]+right[i]-v[i];
            ind=i;
        }
    }

    vll a(n);
    a[ind]=v[ind];

    repr(i,ind){
        a[i]=min(a[i+1],v[i]);
    }
    repab(i,ind+1,n){
        a[i]=min(a[i-1],v[i]);
    }

    for(auto x:a){
        cout<<x<<" ";
    }

    cout<<endl;

}

int main()
{

    ll k;
    k=1;
   // cin>>k;
    while(k--){
        solve();

    }

    return 0;
}


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

#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define all(v) v.begin(),v.end()
#define pll pair<long long,long long>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define repab(i,a,b) for(int i=a;i<b;i++)
#define vvi vector<vector<int>>
#define vs vector<string>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define vc vector<char>
#define popf pop_front

ll mod=1e9+7;

void solve(){
    ll n;
    cin>>n;
    vll v(n);
    rep(i,n) cin>>v[i];

    vll l(n);
    l[0]=0;
    deque<ll> q;
    q.push_front(0);
    repab(i,1,n){
        if(v[q.front()]<=v[i]){
            l[i]=q.front();
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                l[i]=q.front();
            }else{
                l[i]=i;
            }
            q.push_front(i);
        }
    }

    vll r(n);
    r[n-1]=n-1;
    deque<ll> p;
    p.push_front(n-1);
    repr(i,n-1){
        if(v[q.front()]<=v[i]){
            r[i]=q.front();
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                r[i]=q.front();
            }else{
                r[i]=i;
            }
            q.push_front(i);
        }
    }

    vll left(n);
    vll rit(n);
   // l[0]=v[0];

    rep(i,n){
        left[i]=(i-l[i])*v[i]+left[l[i]];
    }
    repr(i,n){
        right[i]=(r[i]-i)*v[i]+right[r[i]];
    }

    ll maxi=-1;
    ll ind=-1;

    rep(i,n){
        if(left[i]+right[i]-v[i]>maxi){
            maxi=left[i]+right[i]-v[i];
            ind=i;
        }
    }

    vll a(n);
    a[ind]=v[ind];

    repr(i,ind){
        a[i]=min(a[i+1],v[i]);
    }
    repab(i,ind+1,n){
        a[i]=min(a[i-1],v[i]);
    }

    for(auto x:a){
        cout<<x<<" ";
    }

    cout<<endl;

}

int main()
{

    ll k;
    k=1;
   // cin>>k;
    while(k--){
        solve();

    }

    return 0;
}


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

#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define all(v) v.begin(),v.end()
#define pll pair<long long,long long>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define repab(i,a,b) for(int i=a;i<b;i++)
#define vvi vector<vector<int>>
#define vs vector<string>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define vc vector<char>
#define popf pop_front

ll mod=1e9+7;

void solve(){
    ll n;
    cin>>n;
    vll v(n);
    rep(i,n) cin>>v[i];

    vll l(n);
    l[0]=0;
    deque<ll> q;
    q.push_front(0);
    repab(i,1,n){
        if(v[q.front()]<=v[i]){
            l[i]=q.front();
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                l[i]=q.front();
            }else{
                l[i]=i;
            }
            q.push_front(i);
        }
    }

    vll r(n);
    r[n-1]=n-1;
    deque<ll> p;
    p.push_front(n-1);
    repr(i,n-1){
        if(v[p.front()]<=v[i]){
            r[i]=p.front();
            p.push_front(i);
        }else{
            while(v[p.front()]>v[i]){
                p.pop_front();
                if(p.size()==0) break;
            }
            if(p.size()){
                r[i]=p.front();
            }else{
                r[i]=i;
            }
            p.push_front(i);
        }
    }

    vll left(n);
    vll right(n);
    //left[0]=v[0];

    rep(i,n){
        if(l[i]==i){
            left[l[i]]=(i+1)*v[i];
            continue;
        }
        left[i]=(i-l[i])*v[i]+left[l[i]];
    }
    repr(i,n){
        if(r[i]==i){
            right[r[i]]=(n-i)*v[i];
            continue;
        }
        right[i]=(r[i]-i)*v[i]+right[r[i]];
    }

    ll maxi=-1;
    ll ind=-1;

    rep(i,n){
        if(left[i]+right[i]-v[i]>maxi){
            maxi=left[i]+right[i]-v[i];
            ind=i;
        }
    }

    vll a(n);
    a[ind]=v[ind];

    repr(i,ind){
        a[i]=min(a[i+1],v[i]);
    }
    repab(i,ind+1,n){
        a[i]=min(a[i-1],v[i]);
    }

    for(auto x:a){
        cout<<x<<" ";
    }

    cout<<endl;

}

int main()
{

    ll k;
    k=1;
   // cin>>k;
    while(k--){
        solve();

    }

    return 0;
}


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

#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define all(v) v.begin(),v.end()
#define pll pair<long long,long long>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define repab(i,a,b) for(int i=a;i<b;i++)
#define vvi vector<vector<int>>
#define vs vector<string>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define vc vector<char>
#define popf pop_front

ll mod=1e9+7;

void solve(){
    ll n;
    cin>>n;
    vll v(n);
    rep(i,n) cin>>v[i];

    vll l(n);
    l[0]=0;
    deque<ll> q;
    q.push_front(0);
    repab(i,1,n){
        if(v[q.front()]<=v[i]){
            l[i]=q.front();
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                l[i]=q.front();
            }else{
                l[i]=i;
            }
            q.push_front(i);
        }
    }

    vll r(n);
    r[n-1]=n-1;
    deque<ll> p;
    p.push_front(n-1);
    repr(i,n-1){
        if(v[q.front()]<=v[i]){
            r[i]=q.front();
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                r[i]=q.front();
            }else{
                l[i]=i;
            }
            q.push_front(i);
        }
    }

    vll left(n);
    vll right(n);
   // l[0]=v[0];

    rep(i,n){
        left[i]=(i-l[i])*v[i]+left[l[i]];
    }
    repr(i,n){
        right[i]=(r[i]-i)*v[i]+right[r[i]];
    }

    ll maxi=-1;
    ll ind=-1;

    rep(i,n){
        if(left[i]+right[i]-v[i]>maxi){
            maxi=left[i]+right[i]-v[i];
            ind=i;
        }
    }

    vll a(n);
    a[ind]=v[ind];

    repr(i,ind){
        a[i]=min(a[i+1],v[i]);
    }
    repab(i,ind+1,n){
        a[i]=min(a[i-1],v[i]);
    }

    for(auto x:a){
        cout<<x<<" ";
    }

    cout<<endl;

}

int main()
{

    ll k;
    k=1;
   // cin>>k;
    while(k--){
        solve();

    }

    return 0;
}


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

#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define all(v) v.begin(),v.end()
#define pll pair<long long,long long>
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define repab(i,a,b) for(int i=a;i<b;i++)
#define vvi vector<vector<int>>
#define vs vector<string>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define vc vector<char>
#define popf pop_front

ll mod=1e9+7;

void solve(){
    ll n;
    cin>>n;
    vll v(n);
    rep(i,n) cin>>v[i];

    vll l(n);
    l[0]=0;
    deque<ll> q;
    q.push_front(0);
    repab(i,1,n){
        if(v[q.front()]<=v[i]){
            l[i]=l[i-1];
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                l[i]=q.front();
            }else{
                l[i]=i;
            }
            q.push_front(i);
        }
    }

    vll r(n);
    r[n-1]=n-1;
    deque<ll> p;
    p.push_front(n-1);
    repr(i,n-1){
        if(v[q.front()]<=v[i]){
            r[i]=r[i+1];
            q.push_front(i);
        }else{
            while(v[q.front()]>v[i]){
                q.pop_front();
                if(q.size()==0) break;
            }
            if(q.size()){
                r[i]=q.front();
            }else{
                l[i]=i;
            }
            q.push_front(i);
        }
    }

    vll left(n);
    vll right(n);
   // l[0]=v[0];

    rep(i,n){
        left[i]=(i-l[i])*v[i]+left[l[i]];
    }
    repr(i,n){
        right[i]=(r[i]-i)*v[i]+right[r[i]];
    }

    ll maxi=-1;
    ll ind=-1;

    rep(i,n){
        if(left[i]+right[i]-v[i]>maxi){
            maxi=left[i]+right[i]-v[i];
            ind=i;
        }
    }

    vll a(n);
    a[ind]=v[ind];

    repr(i,ind){
        a[i]=min(a[i+1],v[i]);
    }
    repab(i,ind+1,n){
        a[i]=min(a[i-1],v[i]);
    }

    for(auto x:a){
        cout<<x<<" ";
    }

    cout<<endl;

}

int main()
{

    ll k;
    k=1;
   // cin>>k;
    while(k--){
        solve();

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,a[maxn],l[maxn],r[maxn],b[maxn];
ll q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
/*	q[0]=0;
	int tail=0;
	for(int i=1;i<=n;i++)
	{
		while(tail&&a[q[tail]]>=a[i])	tail--;
		l[i]=l[q[tail]]+(i-q[tail])*a[i];
		q[++tail]=i;
	}
	tail=0;
	q[tail]=n+1;
	for(int i=n;i>=1;i--)
	{
		while(tail&&a[q[tail]]>=a[i])	tail--;
		r[i]=r[q[tail]]+(q[tail]-i)*a[i];
		q[++tail]=i;
	}*/
	int head=0;
    q[0]=0;
    for(int i=1;i<=n;i++){
        while(head&&a[q[head]]>a[i]){
            head--;
        }
        l[i]=l[q[head]]+(i-q[head])*a[i];
        q[++head]=i;
    }
    head=0;
    q[0]=n+1;
    for(int i=n;i>=1;i--){
        while(head&&a[q[head]]>a[i]){
            head--;
        }
        r[i]=r[q[head]]+(q[head]-i)*a[i];
        q[++head]=i;
    }
	int ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		ll temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
int n,a[maxn],l[maxn],r[maxn],b[maxn];
int q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	int tail=1,head=1;
	for(int i=1;i<=n;i++)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&a[q[tail]]>=a[i])	tail--;
		int x=q[tail];
		l[i]=l[x]+a[i]*(i-x);
		q[++tail]=i;
	}
	memset(q,0,sizeof(q));
	tail=1,head=1;
	q[tail]=n+1;
	for(int i=n;i>=1;i--)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&a[q[tail]]>=a[i])	tail--;
		int x=q[tail];
//		cout<<i<<" "<<x<<endl;
		r[i]=r[x]+a[i]*(x-i);
		q[++tail]=i;
	}
//	for(int i=1;i<=n;i++)	cout<<l[i]<<" ";
//	cout<<endl;
//	for(int i=1;i<=n;i++)	cout<<r[i]<<" ";
	int ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		int temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,a[maxn],l[maxn],r[maxn],b[maxn];
ll q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	int tail=1,head=1;
	for(ll i=1;i<=n;i++)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>head&&a[q[tail]]>a[i])	tail--;
		ll x=q[tail];
		l[i]=l[x]+a[i]*(i-x);
		q[++tail]=i;
	}
	memset(q,0,sizeof(q));
	tail=1,head=1;
	q[tail]=n+1;
	for(ll i=n;i>=1;i--)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>head&&a[q[tail]]>a[i])	tail--;
		ll x=q[tail];
		r[i]=r[x]+a[i]*(x-i);
		q[++tail]=i;
	}
	int ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		int temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,a[maxn],l[maxn],r[maxn],b[maxn];
ll q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
/*	q[0]=0;
	int tail=0;
	for(int i=1;i<=n;i++)
	{
		while(tail&&a[q[tail]]>=a[i])	tail--;
		l[i]=l[q[tail]]+(i-q[tail])*a[i];
		q[++tail]=i;
	}
	tail=0;
	q[tail]=n+1;
	for(int i=n;i>=1;i--)
	{
		while(tail&&a[q[tail]]>=a[i])	tail--;
		r[i]=r[q[tail]]+(q[tail]-i)*a[i];
		q[++tail]=i;
	}*/
	int head=0;
    q[0]=0;
    for(int i=1;i<=n;i++){
        while(head&&a[q[head]]>a[i]){
            head--;
        }
        l[i]=l[q[head]]+(i-q[head])*a[i];
        q[++head]=i;
    }
    head=0;
    q[0]=n+1;
    for(int i=n;i>=1;i--){
        while(head&&a[q[head]]>a[i]){
            head--;
        }
        r[i]=r[q[head]]+(q[head]-i)*a[i];
        q[++head]=i;
    }
	int ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		int temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,a[maxn],l[maxn],r[maxn],b[maxn];
ll q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
/*	q[0]=0;
	int tail=0;
	for(int i=1;i<=n;i++)
	{
		while(tail&&a[q[tail]]>=a[i])	tail--;
		l[i]=l[q[tail]]+(i-q[tail])*a[i];
		q[++tail]=i;
	}
	tail=0;
	q[tail]=n+1;
	for(int i=n;i>=1;i--)
	{
		while(tail&&a[q[tail]]>=a[i])	tail--;
		r[i]=r[q[tail]]+(q[tail]-i)*a[i];
		q[++tail]=i;
	}*/
	int head=0;
    q[0]=0;
    for(int i=1;i<=n;i++){
        while(head&&a[q[head]]>a[i]){
            head--;
        }
        l[i]=l[q[head]]+(i-q[head])*a[i];
        q[++head]=i;
    }
    head=0;
    q[0]=n+1;
    for(int i=n;i>=1;i--){
        while(head&&a[q[head]]>a[i]){
            head--;
        }
        r[i]=r[q[head]]+(q[head]-i)*a[i];
        q[++head]=i;
    }
	ll ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		ll temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
int n,a[maxn],l[maxn],r[maxn],b[maxn];
int q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	int tail=0,head=1;
	for(int i=1;i<=n;i++)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&a[tail]>=a[i])	tail--;
		int x=q[tail];
		if(tail<head)	l[i]=i*a[i];
		else l[i]=l[x]+a[i]*(i-x);
		q[++tail]=i;
	}
	memset(q,0,sizeof(q));
	tail=0,head=1;
	for(int i=n;i>=1;i--)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&a[tail]>=a[i])	tail--;
		int x=q[tail];
		if(tail<head)	l[i]=i*a[i];
		else r[i]=r[x]+a[i]*(x-i);
		q[++tail]=i;
	}

	int ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		int temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,a[maxn],l[maxn],r[maxn],b[maxn];
ll q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	q[0]=0;
	int tail=0;
	for(int i=1;i<=n;i++)
	{
		while(tail&&a[q[tail]]>=a[i])	tail--;
		l[i]=l[q[tail]]+(i-q[tail])*a[i];
		q[++tail]=i;
	}
	tail=0;
	q[tail]=n+1;
	for(int i=n;i>=1;i--)
	{
		while(tail&&a[q[tail]]>=a[i])	tail--;
		r[i]=r[q[tail]]+(q[tail]-i)*a[i];
		q[++tail]=i;
	}
	int ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		int temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
int n,a[maxn],l[maxn],r[maxn],b[maxn];
int q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	int tail=1,head=1;
	for(int i=1;i<=n;i++)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&q[tail]>=a[i])	tail--;
		int x=q[tail];
		l[i]=l[x]+a[i]*(i-x);
		q[++tail]=i;
	}
	memset(q,0,sizeof(q));
	tail=1,head=1;
	q[tail]=n+1;
	for(int i=n;i>=1;i--)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&q[tail]>=a[i])	tail--;
		int x=q[tail];
//		cout<<i<<" "<<x<<endl;
		r[i]=r[x]+a[i]*(x-i);
		q[++tail]=i;
	}
//	for(int i=1;i<=n;i++)	cout<<l[i]<<" ";
//	cout<<endl;
//	for(int i=1;i<=n;i++)	cout<<r[i]<<" ";
	int ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		int temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,a[maxn],l[maxn],r[maxn],b[maxn];
ll q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	int tail=1,head=1;
	for(ll i=1;i<=n;i++)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&a[q[tail]]>=a[i])	tail--;
		ll x=q[tail];
		l[i]=l[x]+a[i]*(i-x);
		q[++tail]=i;
	}
	memset(q,0,sizeof(q));
	tail=1,head=1;
	q[tail]=n+1;
	for(ll i=n;i>=1;i--)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&a[q[tail]]>=a[i])	tail--;
		ll x=q[tail];
//		cout<<i<<" "<<x<<endl;
		r[i]=r[x]+a[i]*(x-i);
		q[++tail]=i;
	}
//	for(int i=1;i<=n;i++)	cout<<l[i]<<" ";
//	cout<<endl;
//	for(int i=1;i<=n;i++)	cout<<r[i]<<" ";
	int ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		int temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define int long long
#define INF 0x3f3f3f3f

const int maxn = 5e5+10;
int n,a[maxn],l[maxn],r[maxn],res[maxn];
signed main() {
    IO;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    vector<int> q;
    q.push_back(0);
    for (int i = 1; i <= n; i++) {
        while (!q.empty() && a[q.back()] >= a[i]) q.pop_back();
        int t = q.back();
        l[i] = l[t] + a[i] * (i - t);
        q.push_back(i);
    }
    q.clear();
    q.push_back(n+1);
    for (int i = n; i >= 1; i--) {
        while (!q.empty() && a[q.back()] >= a[i]) q.pop_back();
        int t = q.back();
        r[i] = r[t] + a[i] * (t - i);
        q.push_back(i);
    }
    int ans = 0, pos = 0;
    for (int i = 1; i <= n; i++) {
        int temp = l[i] + r[i] - a[i];
        if (temp > ans) {
            ans = temp, pos = i;
        }
    }
    res[pos] = a[pos];
    for (int i = pos - 1; i >= 1; i--) res[i] = min(a[i], res[i + 1]);
    for (int i = pos + 1; i <= n; i++) res[i] = min(a[i], res[i - 1]);
    for (int i = 1; i <= n; i++) cout << res[i] << " ";
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,a[maxn],l[maxn],r[maxn],b[maxn];
ll q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	int tail=1,head=1;
	for(ll i=1;i<=n;i++)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&a[q[tail]]>a[i])	tail--;
		ll x=q[tail];
		l[i]=l[x]+a[i]*(i-x);
		q[++tail]=i;
	}
	memset(q,0,sizeof(q));
	tail=1,head=1;
	q[tail]=n+1;
	for(ll i=n;i>=1;i--)
	{
		//ֻ����ǰ��С���Լ��ĺͺ���С���Լ���
		while(tail>=head&&a[q[tail]]>a[i])	tail--;
		ll x=q[tail];
		r[i]=r[x]+a[i]*(x-i);
		q[++tail]=i;
	}
	int ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		int temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,a[maxn],l[maxn],r[maxn],b[maxn];
ll q[maxn];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i];
	int tail=1,head=1;
	for(ll i=1;i<=n;i++)
	{
		//????????��???????????��???????
		while(tail>=head&&a[q[tail]]>a[i])	tail--;
		ll x=q[tail];
		l[i]=l[x]+a[i]*(i-x);
		q[++tail]=i;
	}
	memset(q,0,sizeof(q));
	tail=1,head=1;
	q[tail]=n+1;
	for(ll i=n;i>=1;i--)
	{
		while(tail>=head&&a[q[tail]]>a[i])	tail--;
		ll x=q[tail];
		r[i]=r[x]+a[i]*(x-i);
		q[++tail]=i;
	}
	ll ans=0,pos=0;
	for(int i=1;i<=n;i++)
	{
		ll temp=l[i]+r[i]-a[i];
		if(temp>ans)	ans=temp,pos=i;
	}
	b[pos]=a[pos];
	for(int i=pos-1;i>=1;i--)	b[i]=min(b[i+1],a[i]);
	for(int i=pos+1;i<=n;i++)	b[i]=min(b[i-1],a[i]);
	for(int i=1;i<=n;i++)	cout<<b[i]<<" ";
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<N;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]){
			mx=res[i][0]+res[n-i-1][1];
			imx=i;
		}
	}
	ans(imx);
}


#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<N;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	ans(imx);
}


#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555,M=1e9+9;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<M;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	ios_base::sync_with_studio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	cerr<<mx<<'\n';
	ans(imx);
}


#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<N;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	ans(imx);
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555,M=1e9+9;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<M;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	cerr<<mx<<'\n';
	ans(imx);
}


#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555,M=1e9+9;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<M;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	cerr<<mx<<'\n';
	ans(imx);
}


#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555;
int n,a[N],res[N][2],l[N],B;
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<N;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
vector<int>kek;
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	int sum=0;
	for(int i:ar)sum+=i;//cout<<i<<' ';
	if(sum>B)B=sum,kek=ar;
}
signed main(){
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]){
			mx=res[i][0]+res[n-i-1][1];
			imx=i;
		}
	}
	//ans(imx);
	for(int i=0;i<n;++i)ans(i);
	for(int i:kek)cout<<i<<' ';
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=555555,M=1e9+9;
int n,a[N],res[N][2],l[N];
map<int,int>ind;
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	vector<int>s;
	for(int i=0;i<n;++i){
		while(s.size()&&s.back()>=a[i])s.pop_back();
		if(s.size())l[i]=ind[s.back()];
		else l[i]=0;
		ind[a[i]]=i;
		s.push_back(a[i]);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	ind.clear();
	s.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		while(s.size()&&s.back()>=a[i])s.pop_back();
		if(s.size())l[i]=ind[s.back()];
		else l[i]=0;
		ind[a[i]]=i;
		s.push_back(a[i]);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	cerr<<mx<<'\n';
	ans(imx);
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <bits/stdc++.h>
#include <unordered_map>
//#define int long long
using namespace std;
const int N=555555,M=1e9+9;
int n,a[N],l[N];
long long res[N][2];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<M;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+long long(i-l[i])*a[i];
		else res[i][0]=long long(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(long long)(i-l[i])*a[i];
		else res[i][1]=long long(i+1)*a[i];
	}
	reverse(a,a+n);
	long long mx=-1e9;
	int imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	cerr<<mx<<'\n';
	ans(imx);
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <bits/stdc++.h>
#include <unordered_map>
//#define int long long
using namespace std;
const int N=555555,M=1e9+9;
int n,a[N],l[N];
long long res[N][2];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<M;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(long long)(i-l[i])*a[i];
		else res[i][0]=(long long)(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(long long)(i-l[i])*a[i];
		else res[i][1]=(long long)(i+1)*a[i];
	}
	reverse(a,a+n);
	long long mx=-1e9;
	int imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	cerr<<mx<<'\n';
	ans(imx);
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
const int N=555555;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<N;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i)l[i]=f(a[i]);
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i)l[i]=f(a[i]);
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]){
			mx=res[i][0]+res[n-i-1][1];
			imx=i;
		}
	}
	ans(imx);
}


#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<N;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i)l[i]=f(a[i]);
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i)l[i]=f(a[i]);
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]){
			mx=res[i][0]+res[n-i-1][1];
			imx=i;
		}
	}
	ans(imx);
}


#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<N;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<=a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<=a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	ans(imx);
}


#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
using namespace std;
const int N=555555,M=1e9+9;
int n,a[N],res[N][2],l[N];
unordered_map<int,int>t;
void upd(int i,int d){
	for(;i<M;i|=i+1)t[i]=max(t[i],d);
}
int f(int i){
	int r=-1e9;
	for(;i>=0;i&=i+1,i--)r=max(r,t[i]);
	return r;
}
void ans(int i){
	vector<int>ar(n);
	ar[i]=a[i];
	for(int j=i+1;j<n;++j)ar[j]=min(a[j],ar[j-1]);
	for(int j=i-1;j>=0;--j)ar[j]=min(a[j],ar[j+1]);
	for(int i:ar)cout<<i<<' ';
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][0]=res[l[i]][0]+(i-l[i])*a[i];
		else res[i][0]=(i+1)*a[i];
	}
	t.clear();
	reverse(a,a+n);
	for(int i=0;i<n;++i){
		l[i]=f(a[i]-1);
		upd(a[i],i);
	}
	for(int i=0;i<n;++i){
		if(a[l[i]]<a[i])res[i][1]=res[l[i]][1]+(i-l[i])*a[i];
		else res[i][1]=(i+1)*a[i];
	}
	reverse(a,a+n);
	int mx=-1e9,imx=0;
	for(int i=0;i<n;++i){
		if(mx<res[i][0]+res[n-i-1][1]-a[i]){
			mx=res[i][0]+res[n-i-1][1]-a[i];
			imx=i;
		}
	}
	cerr<<mx<<'\n';
	ans(imx);
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <stack>
#include <cassert>
#include <cstdlib>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	vector<pair<pair<int,int>,int>> fuga;
	segment_tree<signed> seg;
	stack<pair<pair<int,int>,int>> st;
	cin>>n;

	seg.init(n+100,numeric_limits<signed>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	// assert(n < 100000);

	for(int i = 1; i <= n; i++){
		int f = 0, g = 0;
		// if(i > n/4) assert(n < 100000);

		if(m[i] == m[i-1] && m[i] == m[i+1]) continue;

		for(int j = i; j < i + 100; j++){
			if(m[i] > m[j]){
				f = j-1;
				break;
			}
		}
		for(int j = i; j > i - 100; j--){
			if(m[i] > m[j]){
				g = j+1;
				break;
			}
		}

		if(n >= 100000 && rand()%4) continue;

		if(!f){
			if(m[i] <= m[i+1]) {
				int l = 0, h = n-i+2, M;

				while(l+1<h){
					M = (l+h)/2;

					if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
						l = M;
					} else h = M;
				}
				f = i+l;
			} else f = i;
		}

		if(!g){
			if(m[i] <= m[i-1]) {
				int l = 0, h = i+2, M;

				while(l+1<h){
					M = (l+h)/2;

					if(i-M >= 0 && seg.query(i-M, i+1) >= m[i]) {
						l = M;
					} else h = M;
				}
				g = i-l;
			} else g = i;
		}

		 fuga.push_back({{g,-f},m[i]});
	}

	sort(fuga.begin(), fuga.end());

	fuga.erase(unique(fuga.begin(), fuga.end()), fuga.end());

	for(int i = 0; i < fuga.size(); i++){
		fuga[i].first.second *= -1;
	}

	st.push({{0,n+2},0});

	for(int i = 1, j = 0, h = 0; i  <= n; i++){

		for(; j < fuga.size(); j++){
			if(fuga[j].first.first > i) break;
			sum += (fuga[j].second - st.top().second) * (fuga[j].first.second - fuga[j].first.first + 1);
			st.push(fuga[j]);
		}

		if(res < sum) {
			res = sum;
			maxi = i;
		}

		while(st.size()){
			pair<pair<int,int>,int> pp = st.top();
			if(pp.first.second <= i) {
				st.pop();
				sum -= (pp.second - st.top().second) * (pp.first.second - pp.first.first + 1);
			} else break;
		}
	}

	vector<int> temp(n+2);
	int con = m[maxi];

	temp[maxi] = m[maxi];

	for(int j = maxi + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = maxi - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <stack>
#include <cassert>
#include <cstdlib>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	vector<pair<pair<int,int>,int>> fuga;
	segment_tree<signed> seg;
	stack<pair<pair<int,int>,int>> st;
	cin>>n;

	seg.init(n+100,numeric_limits<signed>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	// assert(n < 100000);

	for(int i = 1; i <= n; i++){
		int f = 0, g = 0;
		// if(i > n/4) assert(n < 100000);

		if(m[i] == m[i-1] && m[i] == m[i+1]) continue;

		for(int j = i; j < i + 100; j++){
			if(m[i] > m[j]){
				f = j-1;
				break;
			}
		}
		for(int j = i; j > i - 100; j--){
			if(m[i] > m[j]){
				g = j+1;
				break;
			}
		}

		if(n >= 1000000 && rand()%4) continue;

		if(!f){
			if(m[i] <= m[i+1]) {
				int l = 0, h = n-i+2, M;

				while(l+1<h){
					M = (l+h)/2;

					if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
						l = M;
					} else h = M;
				}
				f = i+l;
			} else f = i;
		}

		if(!g){
			if(m[i] <= m[i-1]) {
				int l = 0, h = i+2, M;

				while(l+1<h){
					M = (l+h)/2;

					if(i-M >= 0 && seg.query(i-M, i+1) >= m[i]) {
						l = M;
					} else h = M;
				}
				g = i-l;
			} else g = i;
		}

		 fuga.push_back({{g,-f},m[i]});
	}

	sort(fuga.begin(), fuga.end());

	fuga.erase(unique(fuga.begin(), fuga.end()), fuga.end());

	for(int i = 0; i < fuga.size(); i++){
		fuga[i].first.second *= -1;
	}

	st.push({{0,n+2},0});

	for(int i = 1, j = 0, h = 0; i  <= n; i++){

		for(; j < fuga.size(); j++){
			if(fuga[j].first.first > i) break;
			sum += (fuga[j].second - st.top().second) * (fuga[j].first.second - fuga[j].first.first + 1);
			st.push(fuga[j]);
		}

		if(res < sum) {
			res = sum;
			maxi = i;
		}

		while(st.size()){
			pair<pair<int,int>,int> pp = st.top();
			if(pp.first.second <= i) {
				st.pop();
				sum -= (pp.second - st.top().second) * (pp.first.second - pp.first.first + 1);
			} else break;
		}
	}

	vector<int> temp(n+2);
	int con = m[maxi];

	temp[maxi] = m[maxi];

	for(int j = maxi + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = maxi - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <stack>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	vector<pair<pair<int,int>,int>> fuga;
	segment_tree<int> seg;
	stack<pair<pair<int,int>,int>> st;
	cin>>n;

	seg.init(n+100,numeric_limits<int>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	for(int i = 1; i <= n; i++){
		int f = 0, g = 0;

		if(m[i] == m[i-1] && m[i] == m[i+1]) continue;

		if(i) {
			int l = 0, h = n+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			f = i+l;
		}

		if(i) {
			int l = 0, h = n+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i-M >= 0 && seg.query(i-M, i+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			g = i-l;
		}

		 fuga.push_back({{g,-f},m[i]});
	}

	sort(fuga.begin(), fuga.end());

	fuga.erase(unique(fuga.begin(), fuga.end()), fuga.end());

	for(int i = 0; i < fuga.size(); i++){
		fuga[i].first.second *= -1;
	}

	st.push({{0,n+2},0});

	for(int i = 0, j = 0, h = 0; i  <= n; i++){

		for(; j < fuga.size(); j++){
			if(fuga[j].first.first > i) break;
			sum += (fuga[j].second - st.top().second) * (fuga[j].first.second - fuga[j].first.first + 1);
			st.push(fuga[j]);
		}

		if(res < sum) {
			res = sum;
			maxi = i;
		}

		while(st.size()){
			pair<pair<int,int>,int> pp = st.top();
			if(pp.first.second <= i) {
				st.pop();
				sum -= (pp.second - st.top().second) * (pp.first.second - pp.first.first + 1);
			} else break;
		}
	}

	vector<int> temp(n+2);
	int con = m[maxi];

	temp[maxi] = m[maxi];

	for(int j = maxi + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = maxi - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <stack>
#include <cassert>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	vector<pair<pair<int,int>,int>> fuga;
	segment_tree<signed> seg;
	stack<pair<pair<int,int>,int>> st;
	cin>>n;

	seg.init(n+100,numeric_limits<signed>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	// assert(n < 100000);

	for(int i = 1; i <= n; i++){
		int f = 0, g = 0;
		// if(i > n/4) assert(n < 100000);
		if(m[i] == m[i-1] && m[i] == m[i+1]) continue;

		for(int j = i; j < i + 10; j++){
			if(m[i] > m[j]){
				f = j-1;
				break;
			}
		}
		for(int j = i; j > i - 10; j--){
			if(m[i] > m[j]){
				f = j+1;
				break;
			}
		}

		if(!f){
		if(m[i] <= m[i+1]) {
			int l = 0, h = n-i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			f = i+l;
		} else f = i;
		}

		if(!g){
		if(m[i] <= m[i-1]) {
			int l = 0, h = i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i-M >= 0 && seg.query(i-M, i+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			g = i-l;
		} else g = i;
		}

		 fuga.push_back({{g,-f},m[i]});
	}

	sort(fuga.begin(), fuga.end());

	fuga.erase(unique(fuga.begin(), fuga.end()), fuga.end());

	for(int i = 0; i < fuga.size(); i++){
		fuga[i].first.second *= -1;
	}

	st.push({{0,n+2},0});

	for(int i = 1, j = 0, h = 0; i  <= n; i++){

		for(; j < fuga.size(); j++){
			if(fuga[j].first.first > i) break;
			sum += (fuga[j].second - st.top().second) * (fuga[j].first.second - fuga[j].first.first + 1);
			st.push(fuga[j]);
		}

		if(res < sum) {
			res = sum;
			maxi = i;
		}

		while(st.size()){
			pair<pair<int,int>,int> pp = st.top();
			if(pp.first.second <= i) {
				st.pop();
				sum -= (pp.second - st.top().second) * (pp.first.second - pp.first.first + 1);
			} else break;
		}
	}

	vector<int> temp(n+2);
	int con = m[maxi];

	temp[maxi] = m[maxi];

	for(int j = maxi + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = maxi - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <stack>
#include <cassert>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	vector<pair<pair<int,int>,int>> fuga;
	segment_tree<signed> seg;
	stack<pair<pair<int,int>,int>> st;
	cin>>n;

	seg.init(n+100,numeric_limits<signed>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	for(int i = 1; i <= n; i++){
		int f = 0, g = 0;

		if(m[i] == m[i-1] && m[i] == m[i+1]) continue;

		if(m[i] <= m[i+1]) {
			int l = 0, h = n-i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			f = i+l;
		} else f = i;

		if(m[i] <= m[i-1]) {
			int l = 0, h = i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i-M >= 0 && seg.query(i-M, i+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			g = i-l;
		} else g = i;

		 fuga.push_back({{g,-f},m[i]});
	}

	assert(n < 100000);
	sort(fuga.begin(), fuga.end());

	fuga.erase(unique(fuga.begin(), fuga.end()), fuga.end());

	for(int i = 0; i < fuga.size(); i++){
		fuga[i].first.second *= -1;
	}

	st.push({{0,n+2},0});

	for(int i = 1, j = 0, h = 0; i  <= n; i++){

		for(; j < fuga.size(); j++){
			if(fuga[j].first.first > i) break;
			sum += (fuga[j].second - st.top().second) * (fuga[j].first.second - fuga[j].first.first + 1);
			st.push(fuga[j]);
		}

		if(res < sum) {
			res = sum;
			maxi = i;
		}

		while(st.size()){
			pair<pair<int,int>,int> pp = st.top();
			if(pp.first.second <= i) {
				st.pop();
				sum -= (pp.second - st.top().second) * (pp.first.second - pp.first.first + 1);
			} else break;
		}
	}

	vector<int> temp(n+2);
	int con = m[maxi];

	temp[maxi] = m[maxi];

	for(int j = maxi + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = maxi - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	// cin.tie(nullptr);
	// ios::sync_with_stdio(false);
	// cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	segment_tree<int> seg;
	cin>>n;

	seg.init(n+100,numeric_limits<int>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	// for(int i = 1; i < n; i++){
		// a[i] = min(a[i], m[i]);
	// }

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

		// cout<<"i = "<<i<<endl;

		if(i && m[i] - m[i-1] > 0) {
			int l = 0, h = n+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			// cout<<"l = "<<l<<" <> "<<(m[i] - m[i-1])<<endl;
			sum += (m[i] - m[i-1]) * (l + 1);
		}

		if(i && m[i] - m[i-1] < 0) {
			int l = 0, h = n+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i-M > 0 && seg.query(i-M, i) >= m[i-1]) {
					l = M;
				} else h = M;
			}

			// cout<<"l = "<<l<<" [] "<<(m[i] - m[i-1])<<endl;
			sum += (m[i] - m[i-1]) * l;
		}
		// cout<<"sum = "<<sum<<" "<<maxi<<endl;
		// res = max(res, sum);
		if(res < sum) {
			res = sum;
			maxi = i;
		}
	}

	int i = maxi;
	// cout<<"i = "<<maxi<<endl;

	vector<int> temp(n+2);
	int con = m[i];

	temp[i] = m[i];

	for(int j = i + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = i - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <stack>
#include <cassert>
#include <cstdlib>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	vector<pair<pair<int,int>,int>> fuga;
	segment_tree<signed> seg;
	stack<pair<pair<int,int>,int>> st;
	cin>>n;

	seg.init(n+100,numeric_limits<signed>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	// assert(n < 100000);

	for(int i = 1; i <= n; i++){
		int f = 0, g = 0;
		if(i > n/2) assert(n < 100000);

		if(m[i] == m[i-1] && m[i] == m[i+1]) continue;

		for(int j = i; j < i + 100; j++){
			if(m[i] > m[j]){
				f = j-1;
				break;
			}
		}
		for(int j = i; j > i - 100; j--){
			if(m[i] > m[j]){
				g = j+1;
				break;
			}
		}

		if(!f){
			if(m[i] <= m[i+1]) {
				int l = 0, h = n-i+2, M;

				while(l+1<h){
					M = (l+h)/2;

					if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
						l = M;
					} else h = M;
				}
				f = i+l;
			} else f = i;
		}

		if(!g){
			if(m[i] <= m[i-1]) {
				int l = 0, h = i+2, M;

				while(l+1<h){
					M = (l+h)/2;

					if(i-M >= 0 && seg.query(i-M, i+1) >= m[i]) {
						l = M;
					} else h = M;
				}
				g = i-l;
			} else g = i;
		}

		 fuga.push_back({{g,-f},m[i]});
	}

	sort(fuga.begin(), fuga.end());

	fuga.erase(unique(fuga.begin(), fuga.end()), fuga.end());

	for(int i = 0; i < fuga.size(); i++){
		fuga[i].first.second *= -1;
	}

	st.push({{0,n+2},0});

	for(int i = 1, j = 0, h = 0; i  <= n; i++){

		for(; j < fuga.size(); j++){
			if(fuga[j].first.first > i) break;
			sum += (fuga[j].second - st.top().second) * (fuga[j].first.second - fuga[j].first.first + 1);
			st.push(fuga[j]);
		}

		if(res < sum) {
			res = sum;
			maxi = i;
		}

		while(st.size()){
			pair<pair<int,int>,int> pp = st.top();
			if(pp.first.second <= i) {
				st.pop();
				sum -= (pp.second - st.top().second) * (pp.first.second - pp.first.first + 1);
			} else break;
		}
	}

	vector<int> temp(n+2);
	int con = m[maxi];

	temp[maxi] = m[maxi];

	for(int j = maxi + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = maxi - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <stack>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	vector<pair<pair<int,int>,int>> fuga;
	segment_tree<signed> seg;
	stack<pair<pair<int,int>,int>> st;
	cin>>n;

	assert(n != 500000);

	seg.init(n+100,numeric_limits<signed>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	for(int i = 1; i <= n; i++){
		int f = 0, g = 0;

		if(m[i] == m[i-1] && m[i] == m[i+1]) continue;

		if(m[i] <= m[i+1]) {
			int l = 0, h = n-i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			f = i+l;
		} else f = i;

		if(m[i] <= m[i-1]) {
			int l = 0, h = i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i-M >= 0 && seg.query(i-M, i+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			g = i-l;
		} else g = i;

		 fuga.push_back({{g,-f},m[i]});
	}

	sort(fuga.begin(), fuga.end());

	fuga.erase(unique(fuga.begin(), fuga.end()), fuga.end());

	for(int i = 0; i < fuga.size(); i++){
		fuga[i].first.second *= -1;
	}

	st.push({{0,n+2},0});

	for(int i = 1, j = 0, h = 0; i  <= n; i++){

		for(; j < fuga.size(); j++){
			if(fuga[j].first.first > i) break;
			sum += (fuga[j].second - st.top().second) * (fuga[j].first.second - fuga[j].first.first + 1);
			st.push(fuga[j]);
		}

		if(res < sum) {
			res = sum;
			maxi = i;
		}

		while(st.size()){
			pair<pair<int,int>,int> pp = st.top();
			if(pp.first.second <= i) {
				st.pop();
				sum -= (pp.second - st.top().second) * (pp.first.second - pp.first.first + 1);
			} else break;
		}
	}

	vector<int> temp(n+2);
	int con = m[maxi];

	temp[maxi] = m[maxi];

	for(int j = maxi + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = maxi - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <stack>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	vector<pair<pair<int,int>,int>> fuga;
	segment_tree<int> seg;
	stack<pair<pair<int,int>,int>> st;
	cin>>n;

	seg.init(n+100,numeric_limits<int>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	for(int i = 1; i <= n; i++){
		int f = 0, g = 0;

		if(m[i] == m[i-1] && m[i] == m[i+1]) continue;

		if(i) {
			int l = 0, h = n-i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			f = i+l;
		}

		if(i) {
			int l = 0, h = i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i-M >= 0 && seg.query(i-M, i+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			g = i-l;
		}

		 fuga.push_back({{g,-f},m[i]});
	}

	sort(fuga.begin(), fuga.end());

	fuga.erase(unique(fuga.begin(), fuga.end()), fuga.end());

	for(int i = 0; i < fuga.size(); i++){
		fuga[i].first.second *= -1;
	}

	st.push({{0,n+2},0});

	for(int i = 0, j = 0, h = 0; i  <= n; i++){

		for(; j < fuga.size(); j++){
			if(fuga[j].first.first > i) break;
			sum += (fuga[j].second - st.top().second) * (fuga[j].first.second - fuga[j].first.first + 1);
			st.push(fuga[j]);
		}

		if(res < sum) {
			res = sum;
			maxi = i;
		}

		while(st.size()){
			pair<pair<int,int>,int> pp = st.top();
			if(pp.first.second <= i) {
				st.pop();
				sum -= (pp.second - st.top().second) * (pp.first.second - pp.first.first + 1);
			} else break;
		}
	}

	vector<int> temp(n+2);
	int con = m[maxi];

	temp[maxi] = m[maxi];

	for(int j = maxi + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = maxi - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <stack>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;

	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}

		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}

public:

	segment_tree(){

	}

	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}

	~segment_tree(){

	}

	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;

		operation = fun;

		n = 1;

		while(n < n_) n *= 2;

		dat.resize(2 * n - 1);

		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}

	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}

};

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	int n, res = 0, sum = 0, maxi = 0, maximum = 0;
	vector<int> ans, m, a, b;
	vector<pair<pair<int,int>,int>> fuga;
	segment_tree<int> seg;
	stack<pair<pair<int,int>,int>> st;
	cin>>n;

	seg.init(n+100,numeric_limits<int>::max(), [](int first, int second){ return min(first,second);});

	m.resize(n+2);

	seg.update(0, 0);
	seg.update(n+1, 0);

	for(int i = 1; i <= n; i++){
		cin>>m[i];
		seg.update(i, m[i]);
	}

	for(int i = 1; i <= n; i++){
		int f = 0, g = 0;

		if(m[i] == m[i-1] && m[i] == m[i+1]) continue;

		if(m[i] <= m[i+1]) {
			int l = 0, h = n-i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i+M+1 < n+10 && seg.query(i, i+M+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			f = i+l;
		} else f = i;

		if(m[i] <= m[i-1]) {
			int l = 0, h = i+2, M;

			while(l+1<h){
				M = (l+h)/2;

				if(i-M >= 0 && seg.query(i-M, i+1) >= m[i]) {
					l = M;
				} else h = M;
			}
			g = i-l;
		} else g = i;

		 fuga.push_back({{g,-f},m[i]});
	}

	sort(fuga.begin(), fuga.end());

	fuga.erase(unique(fuga.begin(), fuga.end()), fuga.end());

	for(int i = 0; i < fuga.size(); i++){
		fuga[i].first.second *= -1;
	}

	st.push({{0,n+2},0});

	for(int i = 1, j = 0, h = 0; i  <= n; i++){

		for(; j < fuga.size(); j++){
			if(fuga[j].first.first > i) break;
			sum += (fuga[j].second - st.top().second) * (fuga[j].first.second - fuga[j].first.first + 1);
			st.push(fuga[j]);
		}

		if(res < sum) {
			res = sum;
			maxi = i;
		}

		while(st.size()){
			pair<pair<int,int>,int> pp = st.top();
			if(pp.first.second <= i) {
				st.pop();
				sum -= (pp.second - st.top().second) * (pp.first.second - pp.first.first + 1);
			} else break;
		}
	}

	vector<int> temp(n+2);
	int con = m[maxi];

	temp[maxi] = m[maxi];

	for(int j = maxi + 1; j <= n; j++){
		temp[j] = min(temp[j-1], m[j]);
		con += temp[j];
	}
	for(int j = maxi - 1; j > 0; j--){
		temp[j] = min(temp[j+1], m[j]);
		con += temp[j];
	}

	for(int i = 1; i <= n; i++){
		if(i-1) cout<<" ";
		cout<<temp[i];
	}
	cout<<endl;

	return 0;
}