#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, maxi = 0;
  cin >> n;
  std::stack<int> stk;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    maxi = max(maxi, a);
    if (!stk.empty()) {
      if (stk.top() < a) {
        cout << "NO";
        return 0;
      }
      if (stk.top() == a)
        stk.pop();
      else
        stk.push(a);
    } else
      stk.push(a);
  }
  if (stk.empty())
    cout << "YES";
  else if (stk.size() == 1 && stk.top() == maxi)
    cout << "YES";
  else
    cout << "NO";
  return 0;
}
