#include <bits/stdc++.h>
using namespace std;
stack<int> s;
multiset<int> p;
int main() {
  int n, x, mx = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    mx = max(mx, x);
    if (s.size() && p.size() && x == s.top() && x == *p.begin()) {
      s.pop();
      p.erase(p.lower_bound(x));
    } else {
      s.push(x);
      p.insert(x);
    }
  }
  if (s.size() > 1)
    cout << "NO" << endl;
  else {
    if (s.size() == 0)
      cout << "YES" << endl;
    else if (s.size() == 1 && s.top() == mx)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}
