#include <bits/stdc++.h>
using namespace std;
struct node {
  node() {}
  node(int _l, int _r, int _id, int _h) { l = _l, r = _r, id = _id, h = _h; }
  bool operator<(const node& other) const { return h > other.h; }
  int l, r, id, h;
};
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  vector<int> belong(n);
  vector<int> left_point(n), right_point(n);
  vector<int> height(n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  priority_queue<node> pq;
  int l = 0, id = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] != a[l]) {
      pq.push(node(l, i - 1, ++id, a[i - 1]));
      belong[l] = id;
      left_point[i - 1] = l;
      right_point[l] = i - 1;
      height[l] = a[i - 1];
      l = i;
    }
  }
  pq.push(node(l, n - 1, ++id, a[n - 1]));
  belong[l] = id;
  left_point[n - 1] = l;
  right_point[l] = n - 1;
  height[l] = a[n - 1];
  bool ok = true;
  while (!pq.empty()) {
    node u = pq.top();
    pq.pop();
    int l = u.l, r = u.r;
    if (belong[l] != u.id) continue;
    if (l == 0 && r == n - 1) break;
    if ((r - l + 1) % 2) {
      ok = false;
      break;
    }
    int h1 = 1 << 30, h2 = 1 << 30, ll = -1, rr = -1;
    if (l > 0) {
      h1 = height[left_point[l - 1]];
      ll = left_point[l - 1];
    }
    if (r + 1 < n) {
      h2 = height[r + 1];
      rr = right_point[r + 1];
    }
    if (h1 < h2) {
      left_point[r] = ll;
      right_point[ll] = r;
      pq.push(node(ll, r, ++id, h1));
      belong[left_point[l - 1]] = id;
    } else if (h1 == h2) {
      right_point[ll] = rr;
      left_point[rr] = ll;
      pq.push(node(ll, rr, ++id, h1));
      belong[ll] = id;
      belong[r + 1] = -1;
    } else {
      right_point[l] = rr;
      left_point[rr] = l;
      height[l] = h2;
      pq.push(node(l, rr, ++id, h2));
      belong[l] = id;
      belong[r + 1] = -1;
    }
  }
  puts(ok ? "YES" : "NO");
  return 0;
}
