#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long a[n + 1];
  list<long long> b;
  long long m = -1;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    m = max(m, a[i]);
    if (b.size() == 0)
      b.push_front(a[i]);
    else if (b.front() == a[i])
      b.pop_front();
    else if (b.front() < a[i]) {
      cout << "NO\n";
      return 0;
    } else
      b.push_front(a[i]);
  }
  if ((b.size() == 1 && b.front() == m) || b.size() == 0)
    cout << "YES\n";
  else
    cout << "NO\n";
  return 0;
}
