Description
A straight dirt road connects two fields on FJ’s farm, but it changes elevation more than FJ would like. His cows do not mind climbing up or down a single slope, but they are not fond of an alternating succession of hills and valleys. FJ would like to add and remove dirt from the road so that it becomes one monotonic slope (either sloping up or down).
You are given N N N integers A 1 , ⋯   , A N ( 1 ≤ N ≤ 2 , 000 ) A_1,\cdots, A_N (1 ≤ N ≤ 2,000) A1,⋯,AN(1≤N≤2,000) describing the elevation ( 0 ≤ A i ≤ 1 , 000 , 000 , 000 ) (0 ≤ Ai ≤ 1,000,000,000) (0≤Ai≤1,000,000,000) at each of N N N equally-spaced positions along the road, starting at the first field and ending at the other. FJ would like to adjust these elevations to a new sequence B 1 , ⋯   , B N