#include <bits/stdc++.h>
using namespace std;
long long segTree[4000002];
long long a[1000002];
void build(long long l, long long r, long long id) {
  segTree[id] = 0;
  if (l == r) {
    return;
  }
  long long mid;
  mid = (l + r) / 2;
  build(l, mid, 2 * id + 1);
  build(mid + 1, r, 2 * id + 2);
  return;
}
long long query(long long l, long long r, long long id, long long queryLeft,
                long long queryRight) {
  if (queryLeft <= l && queryRight >= r) return segTree[id];
  if (queryLeft > r || queryRight < l) return 1;
  long long mid = (l + r) / 2;
  if (segTree[id] == 1) {
    return 1;
  }
  return query(l, mid, 2 * id + 1, queryLeft, queryRight) &
         query(mid + 1, r, 2 * id + 2, queryLeft, queryRight);
}
void update(long long l, long long r, long long id, long long queryLeft,
            long long queryRight) {
  if (queryLeft <= l && queryRight >= r) {
    segTree[id] = 1;
    return;
  }
  if (queryLeft > r || queryRight < l) return;
  long long mid = (l + r) / 2;
  update(l, mid, 2 * id + 1, queryLeft, queryRight);
  update(mid + 1, r, 2 * id + 2, queryLeft, queryRight);
  segTree[id] = segTree[2 * id + 1] & segTree[2 * id + 2];
}
vector<pair<long long, long long> > adj[1000002];
map<long long, long long> m;
set<long long> s;
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  long long n, r, maxxa = 0;
  cin >> n;
  r = 1;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    maxxa = max(maxxa, a[i]);
    s.insert(a[i]);
  }
  for (auto f : s) {
    m[f] = r++;
  }
  build(0, n - 1, 0);
  long long i = 0;
  while (i < n) {
    long long c = 0, x = a[i], y = i;
    while (i < n && a[i] == x) {
      ++i;
      ++c;
    }
    if (maxxa == x) {
      continue;
    }
    if (c % 2 == 1) {
      adj[m[x]].push_back(make_pair(y, i - 1));
    } else {
      update(0, n - 1, 0, y, i - 1);
    }
  }
  for (int i = 1; i < r; ++i) {
    if (i == 1) {
      if (!adj[i].empty()) {
        cout << "NO"
             << "\n";
        return 0;
      } else {
        continue;
      }
    }
    for (int j = 0; j < adj[i].size(); ++j) {
      pair<long long, long long> p1, p2;
      if (j == adj[i].size() - 1) {
        cout << "NO"
             << "\n";
        return 0;
      }
      p1 = adj[i][j], p2 = adj[i][j + 1];
      if (query(0, n - 1, 0, p1.second + 1, p2.first - 1) == 1) {
        update(0, n - 1, 0, p1.first, p2.second);
        ++j;
      } else {
        cout << "NO"
             << "\n";
        return 0;
      }
    }
  }
  cout << "YES"
       << "\n";
  return 0;
}
