#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long i, j, k, l, m, n;
  cin >> n;
  vector<long long> arr(n + 1);
  for (i = 1; i <= n; i++) cin >> arr[i];
  long long sprs[n + 1][22];
  for (i = 1; i <= n; i++) sprs[i][0] = arr[i];
  for (j = 1; j <= 21; j++) {
    for (i = 1; i <= n; i++) {
      sprs[i][j] = sprs[i][j - 1];
      if (i + pow(2, j - 1) <= n)
        sprs[i][j] = max(sprs[i][j], sprs[i + (long long)pow(2, j - 1)][j - 1]);
    }
  }
  stack<pair<long long, long long> > st;
  for (i = 1; i <= n; i++) {
    if (st.empty())
      st.push({arr[i], i});
    else {
      if (arr[i] == st.top().first) {
        long long i1 = st.top().second, j1 = i;
        long long ln = (j1 - i1 + 1);
        long long cn = 0;
        while (ln > 1) ln /= 2, cn++;
        long long a1 =
            max(sprs[i1][cn], sprs[j1 - (long long)(pow(2, cn)) + 1][cn]);
        if (a1 <= arr[i])
          st.pop();
        else
          st.push({arr[i], i});
      } else
        st.push({arr[i], i});
    }
  }
  if ((long long)st.size() > 1)
    cout << "NO\n";
  else {
    long long fl = 1;
    if ((long long)st.size() == 1) {
      long long a1 = sprs[1][21];
      if (st.top().first < a1) fl = 0;
    }
    if (!fl)
      cout << "NO\n";
    else
      cout << "YES\n";
  }
  return 0;
}
