Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.
The current state of the wall can be respresented by a sequence aa of nn integers, with aiai being the height of the ii-th part of the wall.
Vova can only use 2×12×1 bricks to put in the wall (he has infinite supply of them, however).
Vova can put bricks only horizontally on the neighbouring parts of the wall of equal height. It means that if for some ii the current height of part ii is the same as for part i+1i+1, then Vova can put a brick there and thus increase both heights by 1. Obviously, Vova can't put bricks in such a way that its parts turn out to be off the borders (to the left of part 11 of the wall or to the right of part nn of it).
Note that Vova can't put bricks vertically.
Vova is a perfectionist, so he considers the wall completed when:
- all parts of the wall has the same height;
- the wall has no empty spaces inside it.
Can Vova complete the wall using any amount of bricks (possibly zero)?
Input
The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of parts in the wall.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial heights of the parts of the wall.
Output
Print "YES" if Vova can complete the wall using any amount of bricks (possibly zero).
Print "NO" otherwise.
Examples
input
Copy
5 2 1 1 2 5
output
Copy
YES
input
Copy
3 4 5 3
output
Copy
NO
input
Copy
2 10 10
output
Copy
YES
Note
In the first example Vova can put a brick on parts 2 and 3 to make the wall [2,2,2,2,5][2,2,2,2,5] and then put 3 bricks on parts 1 and 2 and 3 bricks on parts 3 and 4 to make it [5,5,5,5,5][5,5,5,5,5].
In the second example Vova can put no bricks in the wall.
In the third example the wall is already complete.
题意:在前一题的基础上只能横放
分析见代码,仔细想想。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
stack<int>sta;
int flag=0,maxx=0,h;
for(int i=1;i<=n;i++)
{
scanf("%d",&h);
maxx=max(h,maxx);
if(!sta.empty())
{
if(h>sta.top())///前面的比后面的低,横放情况下无法对前面的进行操作
{
flag=1;
}
if(h==sta.top())///相等时一定可以使使最后的一样高
{
sta.pop();
}
else///后面的低于前面的,可能通过增加后面的高度使其同高的
{
sta.push(h);
}
}
else
{
sta.push(h);
}
}
/*剩余的多余1个或者剩余1个低于最高的情况都是不能使其最后同高的*/
if(sta.size()>1||(sta.size()==1&&sta.top()<maxx))
{
flag=1;
}
if(flag==1) printf("NO\n");
else printf("YES\n");
return 0;
}