#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, a[N], mx = 0;
int top = 0, b[N], pre;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    if (a[i] > mx) mx = a[i];
    if (!top || b[top] > a[i]) {
      ++top;
      b[top] = a[i];
    } else if (b[top] == a[i]) {
      --top;
    } else {
      printf("NO\n");
      return 0;
    }
  }
  if (top > 1)
    printf("NO\n");
  else if (top == 1) {
    if (mx > b[top])
      printf("NO\n");
    else
      printf("YES\n");
  } else
    printf("YES\n");
  return 0;
}
