#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long int a[n], mx = -1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }
  stack<long long int> s;
  bool poss = true;
  for (int i = 0; i < n; i++) {
    if (s.empty() == true) {
      s.push(a[i]);
    } else {
      if (a[i] > s.top()) {
        poss = false;
      } else {
        if (a[i] == s.top()) {
          s.pop();
        } else {
          s.push(a[i]);
        }
      }
    }
  }
  string ans;
  if (poss == false) {
    ans = "NO";
  } else if (s.size() == 0 || (s.size() == 1 && s.top() == mx)) {
    ans = "YES";
  } else {
    ans = "NO";
  }
  cout << ans << endl;
  return 0;
}
