#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, a[maxn], top, mx;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[++top]);
    if (top > 1 && a[top] > a[top - 1]) {
      puts("NO");
      return 0;
    }
    while (top > 1 && a[top] == a[top - 1]) {
      mx = max(mx, a[top]);
      top -= 2;
    }
  }
  if (top > 1 || (top == 1 && a[top] < mx))
    puts("NO");
  else
    puts("YES");
  return 0;
}
