#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-9;
const int mod = 1e9 + 7;
const long long INF = 1e12 + 1;
const int N = 2e5 + 9;
int n, mx;
int a[N];
bool b[N];
vector<int> v;
int main() {
  ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
    if ((long long)(v.size()) > 0 && v[(long long)(v.size()) - 1] == a[i]) {
      if ((long long)(v.size()) > 1 &&
          v[(long long)(v.size()) - 2] > v[(long long)(v.size()) - 1]) {
        v.pop_back();
      } else if ((long long)(v.size()) <= 1)
        v.pop_back();
    } else
      v.push_back(a[i]);
  }
  if ((long long)(v.size()) == 0) {
    cout << "YES" << endl;
    exit(0);
  }
  if ((long long)(v.size()) == 1 && v[0] >= mx)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  exit(0);
}
