D2. Sage’s Birthday (hard version) SOLUTIONS CODEFORCES

Sage’s Birthday (hard version) SOLUTION

This is the hard form of the issue. The distinction between the forms is that in the simple form all costs ai are unique. You can make hacks if and just on the off chance that you tackled the two renditions of the issue.
Today is Sage’s birthday, and she will go out to shop to purchase ice circles. All n ice circles are put in succession and they are numbered from 1 to n from left to right. Each ice circle has a positive whole number cost. In this form, a few costs can be equivalent.
An ice circle is modest in the event that it costs carefully under two neighboring ice circles: the closest tp the left and the closest to one side. The furthest left and the furthest right ice circles are not modest. Sage will pick all modest ice circles and afterward purchase just them.
You can visit the shop before Sage and reorder the ice circles as you wish. Discover the greatest number of ice circles that Sage can purchase, and show how the ice circles ought to be reordered.
The main line contains a solitary whole number n (1≤n≤105) — the quantity of ice circles in the shop.
The subsequent line contains n whole numbers a1,a2,… ,a (1≤ai≤109) — the costs of ice circles.
In the primary line print the greatest number of ice circles that Sage can purchase.
In the subsequent line print the costs of ice circles in the ideal request. In the event that there are a few right answers, you can print any of them.
1 3 2 4 5 4
3 1 4 2 4 2 5
In the example it’s unrealistic to put the ice circles in any request so Sage would purchase 4 of them. In the event that the circles are submitted in the request (3,1,4,2,4,2,5), at that point Sage will get one circle for 1 and two circles for 2 each.

Leave a Comment

error: Content is protected !!