#include <bits/stdc++.h>
using namespace std;
const long long N = 200011;
vector<long long> v;
vector<pair<long long, long long> > ord;
pair<long long, long long> dir[N];
bool puede(long long x, long long n) {
  if (0 < x && x <= n && 0 < dir[x].second && dir[x].second <= n) {
    if (v[x - 1] == v[dir[x].second - 1] && v[x - 1] >= v[x]) {
      return true;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, x;
  cin >> n;
  long long aux = 0;
  dir[0] = make_pair(-1, 1);
  for (long long i = 1; i <= n; i++) {
    cin >> x;
    v.push_back(x);
    ord.push_back(make_pair(x, i));
    dir[i] = make_pair(i - 1, i + 1);
  }
  dir[n + 1] = make_pair(n, n + 2);
  sort(ord.begin(), ord.end());
  for (long long j = 0; j < n; j++) {
    long long i = ord[j].second;
    if (puede(i, n)) {
      long long ini = i;
      long long fin = dir[i].second;
      while (true) {
        if (puede(ini, n)) {
          long long uu = ini;
          long long vv = fin;
          dir[dir[ini].first] =
              make_pair(dir[dir[ini].first].first, dir[fin].second);
          dir[dir[fin].second] =
              make_pair(dir[ini].first, dir[dir[fin].second].second);
          ini = dir[ini].first;
          fin = dir[fin].second;
          dir[uu] = make_pair(-1, -1);
          dir[vv] = make_pair(-1, -1);
        } else {
          break;
        }
      }
    }
  }
  long long cnt = 0;
  long long mx = 0;
  long long lim = 0;
  for (long long i = 1; i <= n; i++) {
    if (!(dir[i].first == -1 && dir[i].second == -1)) {
      cnt++;
      mx = max(mx, v[i - 1]);
    } else {
      lim = max(lim, v[i - 1]);
    }
  }
  if (cnt == 0) {
    cout << "YES" << endl;
  } else if (cnt == 1) {
    if (lim <= mx) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  } else {
    cout << "NO" << endl;
  }
  return 0;
}
