#include <bits/stdc++.h>
using namespace std;
const int DR[] = {-1, 0, 1, 0};
const int DL[] = {0, -1, 0, -1};
const double PI = acos(-1.0);
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = 1073741823;
const long long INFLL = 4e18;
const int max = 1e5;
int n;
vector<pair<int, int> > vec;
void add(int tmp) {
  if (vec.empty() || vec.back().second != tmp)
    vec.push_back({1, tmp});
  else
    vec.back().first++;
}
void check() {
  int num = (int)vec.size();
  if (num > 1) {
    if (vec[num - 2].second < vec.back().second &&
        vec[num - 2].first % 2 == 0) {
      vec[num - 2].second = vec.back().second;
      vec[num - 2].first += vec.back().first;
      vec.pop_back();
    }
    if (vec[num - 2].second > vec.back().second && vec.back().first % 2 == 0) {
      vec[num - 2].first += vec.back().first;
      vec.pop_back();
    }
    num = (int)vec.size();
  }
}
void read() { cin >> n; }
void solve() {
  int tmp;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    add(tmp);
    check();
  }
  int height = (int)vec.size() ? vec[0].second : 1;
  for (auto p : vec) {
    if (p.second != height) {
      cout << "NO" << endl;
      return;
    }
  }
  cout << "YES" << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int TC = 1;
  while (TC--) {
    read();
    solve();
  }
}
