#include <bits/stdc++.h>
using namespace std;
int val[200005];
int nxt[200005];
vector<pair<int, int> > vec;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, big = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> val[i];
    nxt[i] = (i + 1);
    big = max(big, val[i]);
    vec.push_back(make_pair(val[i], i));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < vec.size(); i++) {
    pair<int, int> temp = vec[i];
    int tval = temp.first;
    int tidx = temp.second;
    int trav = nxt[tidx];
    if (val[tidx] == -1 || tval == big) continue;
    val[tidx] = -1;
    while ((val[trav] == -1 || val[trav] == tval) && trav <= n) {
      val[trav] = -1;
      trav = nxt[trav];
    }
    int cnt = trav - tidx;
    if (cnt % 2 == 0) {
      nxt[tidx] = trav;
      val[tidx] = -1;
    } else {
      cout << "NO\n";
      exit(0);
    }
  }
  cout << "YES\n";
  return 0;
}
