#include <bits/stdc++.h>
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
int x[200010];
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) a[i] = in();
  int s = 0;
  for (int i = 0; i < n; ++i) {
    if (s <= 1) {
      x[s++] = a[i];
    } else if (x[s - 2] == x[s - 1] && x[s - 1] <= a[i]) {
      s -= 2;
      x[s++] = a[i];
    } else if (x[s - 1] == a[i] && a[i] <= x[s - 2]) {
      s -= 1;
    } else {
      x[s++] = a[i];
    }
  }
  if (s <= 1 || (s == 2 && x[0] == x[1])) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}
