#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, mod = 1000000007;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const double pi = acosl(-1.), eps = 1e-9;
const int NN = 200020;
int pre[NN];
int nxt[NN];
int a[NN];
int b[NN];
vector<int> pos[NN];
pair<int, int> p[NN];
int main() {
  int n, m, hei, mm;
  int st, ed;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    b[i - 1] = a[i];
  }
  sort(b, b + n);
  hei = unique(b, b + n) - b;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(b, b + hei, a[i]) - b;
    pos[a[i]].push_back(i);
  }
  memset(pre, -1, sizeof(pre));
  memset(nxt, -1, sizeof(nxt));
  hei--;
  for (int h = 0; h < hei; h++) {
    m = 0;
    for (auto id : pos[h]) p[m++] = pair<int, int>(id, id);
    sort(p, p + m);
    mm = 0;
    for (int i = 1; i < m; i++) {
      if (p[i].first == p[mm].second + 1)
        p[mm].second++;
      else
        p[++mm] = p[i];
    }
    m = mm + 1;
    for (int i = 0, j; i < m; i = j + 1) {
      st = p[i].first;
      if (~pre[st - 1]) st = pre[st - 1];
      for (j = i; j < m; j++) {
        ed = p[j].second;
        if (j + 1 < m && ~nxt[ed + 1] && nxt[ed + 1] + 1 == p[j + 1].first)
          continue;
        if (~nxt[ed + 1]) {
          ed = nxt[ed + 1];
        }
        break;
      }
      if (ed - st + 1 & 1) return puts("NO"), 0;
      nxt[st] = ed;
      pre[ed] = st;
    }
  }
  puts("YES");
  return 0;
}
