#include <bits/stdc++.h>
using namespace std;
int DEBUGGING = 0;
const int INF = 1000000000;
const int MOD = 1000000007;
const long long LINF = 1e18;
const double PI = 3.1415926535897932;
const double EPS = 1e-7;
long long gcdEx(long long a, long long b, long long *x, long long *y) {
  if (!a) {
    *x = 0;
    *y = 1;
    return b;
  }
  long long x1, y1, gcd = gcdEx(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
long long modI(long long b, long long m) {
  long long x, y;
  gcdEx(b, m, &x, &y);
  return (x % m + m) % m;
}
long long modD(long long a, long long b) {
  return (modI(b, MOD) * (a % MOD)) % MOD;
}
long long modS(long long a, long long b) {
  return ((a % MOD) - (b % MOD) + MOD) % MOD;
}
long long modP(long long x, long long y) {
  long long r = 1;
  x %= MOD;
  while (y > 0) {
    if (y & 1) {
      r = (r * x) % MOD;
    }
    y = y >> 1;
    x = (x * x) % MOD;
  }
  return r;
}
long long modA(long long a, long long b) {
  return ((a % MOD) + (b % MOD)) % MOD;
}
long long modM(long long a, long long b) {
  return ((a % MOD) * (b % MOD)) % MOD;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int a[200005];
stack<pair<int, int> > st;
void solve(int TEST_CASE) {
  int n, mx = -1;
  cin >> n;
  for (int i = 0; i < (n); ++i) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }
  a[n] = mx;
  st.push({mx, -1});
  int dst, rm, rmq;
  for (int i = 0; i < (n + 1); ++i) {
    rm = 0, rmq = 0;
    while (!st.empty() && st.top().first <= a[i]) {
      dst = i - st.top().second - 1;
      if (DEBUGGING)
        cout << i << ": " << (st.top().second) << ", " << dst << "\n";
      if (st.top().first == a[i]) rmq = 1;
      st.pop();
      rm = 1;
      if (dst & 1) {
        cout << "NO";
        return;
      }
    }
    if (!st.empty()) {
      dst = i - st.top().second - 1;
      if (rm && !rmq && dst & 1) {
        cout << "NO";
        return;
      }
    }
    st.push({a[i], i});
  }
  cout << "YES";
}
int main(int argc, char *argv[]) {
  if (argc == 2 && argv[1][1] == 'v') DEBUGGING = 1;
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin.exceptions(cin.failbit);
  int nTestCases = 1;
  for (int i = 1; i <= (nTestCases); ++i) solve(i);
  return 0;
}
