#include <bits/stdc++.h>
using namespace std;
int64_t sum, vval[200005], dps[200005], mxx = 0, mm, mx;
int64_t poww(int64_t a, int64_t e) {
  if (e == 0) return 1;
  if (e == 1) return a % 998244353;
  if ((e % 2) == 0)
    return poww(((a % 998244353) * (a % 998244353)) % 998244353, e / 2) %
           998244353;
  else
    return (a % 998244353) *
           poww(((a % 998244353) * (a % 998244353)) % 998244353, (e - 1) / 2) %
           998244353;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int64_t xx, yy, dd, mm, ans = 0, n = 1, m, r = 0, c = 0, x, y, mn = 1e9 + 5;
  while (n--) {
    int64_t a, b, s = INT_MAX, f = 1, jj = 0, mxxx = 0;
    cin >> a;
    for (int64_t i = 0; i < a; ++i) {
      cin >> vval[i];
      mxxx = max(mxxx, vval[i]);
    }
    int64_t em = 0;
    for (int64_t i = 0; i < a; ++i) {
      if (em > 0) {
        int64_t first = dps[--em];
        if (vval[i] < first) {
          dps[em++] = first;
          dps[em++] = vval[i];
        } else if (vval[i] > first)
          return cout << "NO", 0;
      } else {
        dps[em++] = vval[i];
      }
    }
    if (em <= 0)
      cout << "YES\n";
    else {
      int64_t adi = dps[--em];
      if (adi == mxxx && em == 0)
        cout << "YES\n";
      else
        cout << "NO";
    }
  }
}
