#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  vector<long long> stk;
  long long mx = 0;
  for (long long i = 1; i <= n; i++) {
    long long tem;
    cin >> tem;
    mx = max(mx, tem);
    if (stk.size() && stk.back() == tem) {
      if (stk.size() == 1) {
        stk.pop_back();
        continue;
      }
      if (stk[stk.size() - 2] >= tem) {
        stk.pop_back();
        continue;
      }
    }
    stk.push_back(tem);
  }
  if (stk.size() == 0) {
    cout << "YES" << endl;
  } else if (stk.size() == 1 && stk.back() >= mx) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}
