#include <bits/stdc++.h>
using namespace std;
stack<int> s;
int main() {
  int n, m, flag = -1, _max = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m;
    _max = max(_max, m);
    if (s.empty())
      s.push(m);
    else {
      if (s.top() < m)
        flag = 0;
      else if (s.top() == m)
        s.pop();
      else
        s.push(m);
    }
  }
  if (flag == 0) {
    cout << "NO" << endl;
    return 0;
  }
  if (s.size() > 1)
    flag = 0;
  else if (s.size() < 1)
    flag = 1;
  else {
    if (_max > s.top())
      flag = 0;
    else
      flag = 1;
  }
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
