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