#include <bits/stdc++.h>
using namespace std;
struct Seg {
  int st, dr, h;
};
int n, vf, nr, maxi, hMax[200005];
Seg seg[200005], st[200005];
int main() {
  int h, i, j;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> h;
    maxi = max(maxi, h);
    if (nr && seg[nr].h == h) {
      seg[nr].dr = i;
    } else {
      ++nr;
      seg[nr].h = h;
      seg[nr].st = seg[nr].dr = i;
    }
  }
  for (int i = 1; i <= nr; ++i)
    if ((seg[i].dr - seg[i].st + 1) % 2 == 1) {
      if (!vf || st[vf].h != seg[i].h || seg[i].h < hMax[vf]) {
        st[++vf] = seg[i];
        hMax[vf] = 0;
      } else
        --vf;
    } else
      hMax[vf] = max(hMax[vf], seg[i].h);
  if (!vf || (vf == 1 && st[vf].h == maxi))
    cout << "YES";
  else
    cout << "NO";
  return 0;
}
