Skyscrapers

Time limit: 1s Memory limit: 256MB Input: Output:

You are not allowed to search for problem "skyscrapers" on Codeforces.

The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along a highway. It is known that the company has already bought nn plots along the highway and is preparing to build nn skyscrapers, one on each plot.

Architects must consider several requirements when planning the skyscrapers:

  • Each plot has a limit on the maximum number of floors due to land properties.
  • According to the city’s design code, it is unacceptable for a skyscraper to have both a taller skyscraper on its left and a taller skyscraper on its right.

Formally, label the plots from 11 to nn. Let aia_i be the number of floors of the skyscraper on plot ii. Then:

  • 1aimi1 \le a_i \le m_i, for all 1in1 \le i \le n, and
  • There must not exist an index ii, 1<i<n1 < i < n, such that ai1>ai<ai+1a_{i−1} > a_i < a_{i+1}.

The company wants to maximize the total number of floors, i.e. a1+a2++ana_1 + a_2 + \ldots + a_n.

Task

Find any valid configuration a1,a2,,ana_1, a_2, \ldots , a_n that satisfies all constraints and maximizes the total number of floors.

Input data

The first line contains a single integer nn.

The second line contains nn integers m1,m2,,mnm_1, m_2, \ldots , m_n, where mim_i is the maximum allowed number of floors on plot ii.

Output data

Print nn integers a1,a2,,ana_1, a_2, \ldots , a_n — the number of floors for each skyscraper, such that all constraints are satisfied and the total number of floors is maximized.

If there are multiple valid answers, print any of them.

Constraints and clarifications

  • 1n51051 \le n \le 5 \cdot 10^5
  • 1mi1091 \le m_i \le 10^9

Example 1

stdin

5
1 2 3 2 1

stdout

1 2 3 2 1

Explanation

Assigning maximum heights to all skyscrapers is valid.

Example 2

stdin

3
10 6 8

stdout

10 6 6

Explanation

Assigning maximum heights to all plots violates the constraint. The configuration [10,6,6][10, 6, 6] is optimal. Another valid configuration is [6,6,8][6, 6, 8], but it is not optimal since the total sum is smaller.

Log in or sign up to be able to send submissions!