#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 1e5 + 5;
int main() {
  int n, a[maxn], maxm = -1;
  stack<int> s;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) {
    if (a[i] > maxm) maxm = a[i];
    if (!s.empty()) {
      if (a[i] > s.top()) {
        cout << "NO" << endl;
        return 0;
      }
      (a[i] == s.top()) ? s.pop() : s.push(a[i]);
    } else
      s.push(a[i]);
  }
  if (s.size() <= 1) {
    if (s.empty())
      cout << "YES" << endl;
    else if (s.top() != maxm)
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
  } else
    cout << "NO" << endl;
  return 0;
}
