#include <bits/stdc++.h>
using namespace std;
long long ara[200009];
int main() {
  long long i, j, k, l, m, n, mx, ch;
  cin >> n;
  mx = 0;
  for (i = 1; i <= n; i++) {
    cin >> ara[i];
    mx = max(mx, ara[i]);
  }
  vector<long long> stk;
  ch = 1;
  for (i = 1; i <= n; i++) {
    if (!stk.empty() && stk.back() == ara[i])
      stk.pop_back();
    else {
      if (!stk.empty() && stk.back() < ara[i]) ch = 0;
      stk.push_back(ara[i]);
    }
  }
  if (ch && stk.size() <= 1 &&
      (stk.empty() || (!stk.empty() && stk.back() == mx)))
    cout << "YES\n";
  else
    cout << "NO\n";
  return 0;
}
