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