#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int Max = 0;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    a.push_back(x);
    Max = max(Max, x);
  }
  vector<pair<int, int> > st = {{a[0], 1}};
  for (int i = 1; i < n; i++) {
    int x = st.back().first;
    if (x == a[i])
      st.back().second++;
    else if (x < a[i]) {
      int c = 0;
      while (st.size() && a[i] > st.back().first) {
        c += st.back().second;
        st.pop_back();
      }
      if (c & 1) {
        return 0 * puts("NO");
      } else {
        x = a[i];
        c++;
        if (st.size() && st.back().first == a[i])
          st.back().second += c;
        else
          st.push_back({x, c});
      }
    } else {
      st.push_back({a[i], 1});
    }
  }
  for (auto x : st)
    if (x.first != Max && x.second % 2 != 0) return 0 * puts("NO");
  puts("YES");
  return 0;
}
