Lunch Line
View as PDFOne day, Lucas is lining up to buy lunch at campus centre when he notices his friend Parsa right behind him. Lucas realizes that instead of waiting and ordering separately, it would be much faster for one of them to place a combined order for both, saving time on the transaction.
Other students in the line notice their strategy, and the idea quickly catches on. The following week, Lucas returns to the food court and sees a new trend: people are merging their orders! Specifically, a person can take on the order of the person immediately in front of or behind them, provided their own current combined order size is greater than or equal to the order size of that adjacent person. Once merged, the person whose order was taken leaves the line.
Watching this unfold, Lucas wonders: given the number of items each person in the line initially intends to order, how many different people could theoretically end up placing the single, final order for the entire line?
More formally, given an array of
positive integers, where
is the number of items the
person in the line initially is ordering, how many distinct people can end up being the final person ordering, if a person who is responsible for ordering items
,
can merge their order with:
- the person responsible for ordering items
,
if
- the person responsible for ordering items
,
if
Input
The first line contains an integer , the number of people initially in the line.
The second line contains integers:
.
Output
The number of different people who can end up ordering for the whole line.
Constraints
Example 1
Input
5
1 1 2 1 6
Output
1
Explanation
Only person can end up ordering for the whole line.
Example 2
Input
5
3 1 3 3 2
Output
3
Explanation
People can end up ordering for the whole line.
Example 3
Input
10
1 5 5 2 4 1 4 1 2 3
Output
6
Explanation
People can end up ordering for the whole line.
Comments