Alice and his candy

Alice is a kindergarten teacher. She wants to give some candies to children in her class. All the children sit in a line and each of them has a rating score according to his or her performance in the class. Alice wants to give at least 1 candy to each child. If two children sit next to each other, then one with the higher rating must get more candies. Alice wants to save money, she needs to minimize the total number of candies.

Input – The First line of the input is an integer N, the number of children in Alice’s class. Each of the following N lines contains an integer that indicates the rating of each child.

Output – Output a single line containing the minimum number of candies Alice must buy.

Constraints 1 <= N <= 10^5, 1 <= rating <= 10^5

Sample Input


Sample Output



here 1, 2, 2 is the rating, Note that when two children have the equal rating they are allowed to have the different number of candies. Hence optimal distribution will be 1, 2, 1.

Code –

Related Problem –

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.