#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int dx[] = {-1, 1, 0, 0, -1, 1, -1, 1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
long long gcd(long long x, long long y) {
  if (!y) return x;
  return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return (x / gcd(x, y)) * y; }
long long fastpow(long long a, long long b) {
  long long res = 1;
  int last;
  while (b) {
    if (b % 2) res *= a;
    b /= 2, a *= a;
  }
  return res;
}
bool isprime(long long n) {
  if (n == 1 || n == 0) return 0;
  for (int i = 2; i <= sqrt(n); i++)
    if (n % i == 0) return 0;
  return 1;
}
int main() {
  fast();
  int ma = INT_MIN;
  int n, a;
  cin >> n;
  bool found = 1;
  stack<int> stk;
  for (int i = 0; i < n; i++) {
    cin >> a;
    ma = max(ma, a);
    if (!stk.empty() && a > stk.top()) found = 0;
    if (stk.empty() || a != stk.top())
      stk.push(a);
    else
      stk.pop();
  }
  if (!found || (stk.size() == 1 && stk.top() < ma))
    cout << "NO\n";
  else
    cout << (stk.size() > 1 ? "NO\n" : "YES\n");
}
