#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int a[N];
int n;
int stos[N], id = 0;
int main() {
  ios_base::sync_with_stdio(0);
  ;
  cin >> n;
  for (int i = 1; i <= (n); i++) cin >> a[i];
  for (int i = 1; i <= (n); i++) {
    if (id == 0)
      stos[id++] = a[i];
    else if (a[i] > stos[id - 1]) {
      cout << "NO";
      exit(0);
    } else if (a[i] == stos[id - 1])
      id--;
    else if (a[i] < stos[id - 1])
      stos[id++] = a[i];
  }
  if (id == 0 or (stos[0] == *max_element(a + 1, a + n + 1) and id == 1))
    cout << "YES";
  else
    cout << "NO";
  exit(0);
}
