#include <bits/stdc++.h>
using namespace std;
template <class T>
using VV = vector<vector<T>>;
template <class T>
inline T SMIN(T& a, const T b) {
  return a = (a > b) ? b : a;
}
template <class T>
inline T SMAX(T& a, const T b) {
  return a = (a < b) ? b : a;
}
template <class Iter>
void dumpc(Iter begin, Iter end) {
  if (false) {
    for (; begin != end; ++begin) {
      cerr << *begin << ',';
    }
    cerr << endl;
  }
}
const int MAX_N = 1e6 + 1;
long long A[MAX_N];
int N;
struct UF {
 public:
  int N, G;
  vector<int> L, R, V;
  UF(int N) : N(N) { init(N); }
  void init(int N) {
    par = vector<int>(N), rank = vector<int>(N), cnt = vector<int>(N, 1),
    L = vector<int>(N, -1), R = vector<int>(N, -1), V = vector<int>(N);
    for (int i = 0; i < N; ++i)
      par[i] = i, rank[i] = 0, cnt[i] = 1, L[i] = i, R[i] = i, V[i] = A[i];
    G = N;
  }
  int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }
  int count(int x) { return cnt[find(x)]; }
  bool sameset(int x, int y) { return find(x) == find(y); }
  int unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return x;
    G--, cnt[x] = cnt[y] = cnt[x] + cnt[y];
    int p = x, c = y;
    if (rank[p] > rank[c])
      swap(p, c);
    else
      rank[p] += rank[p] == rank[c];
    par[c] = p;
    L[p] = min(L[p], L[c]), R[p] = max(R[p], R[c]), V[p] = max(V[p], V[c]);
    return p;
  }

 private:
  vector<int> par, rank, cnt;
};
int viz[MAX_N];
void solve_org() {
  memset(viz, 0, sizeof(viz));
  queue<int> Q;
  UF uf(N);
  for (int i = 0; i < (N - 1); ++i)
    if (A[i] == A[i + 1] && viz[i] == 0 && viz[i + 1] == 0) {
      if (false) {
        cerr << "i"
             << " = " << (i) << ", "
             << "i + 1"
             << " = " << (i + 1) << endl;
      }
      int p = uf.unite(i, i + 1);
      Q.emplace(p), viz[i] = viz[i + 1] = 1;
    }
  while (int((Q).size())) {
    int i = Q.front();
    Q.pop();
    int l = uf.L[i], r = uf.R[i];
    if (l - 1 >= 0 && r + 1 < N && viz[l - 1] == 0 && viz[r + 1] == 0 &&
        A[l - 1] == A[r + 1] && A[l - 1] >= uf.V[i]) {
      if (false) {
        cerr << "l - 1"
             << " = " << (l - 1) << ", "
             << "r + 1"
             << " = " << (r + 1) << endl;
      };
      int p = uf.unite(i, l - 1);
      p = uf.unite(p, r + 1);
      Q.emplace(p), viz[l - 1] = viz[r + 1] = 1;
    } else if (l - 1 >= 0 && viz[l - 1]) {
      int p = uf.unite(i, l - 1);
      Q.emplace(p);
    } else if (r + 1 < N && viz[r + 1]) {
      int p = uf.unite(i, r + 1);
      Q.emplace(p);
    }
  }
  map<int, int> cnt;
  for (int i = 0; i < (N); ++i)
    if (viz[i] == 0) cnt[A[i]]++;
  int maxv = 0;
  for (int i = 0; i < (N); ++i)
    if (viz[i]) SMAX(maxv, uf.V[uf.find(i)]);
  bool ok = false;
  if (int((cnt).size()) == 0) ok = true;
  if (int((cnt).size()) == 1) ok = cnt.begin()->first >= maxv;
  cout << (ok ? "YES" : "NO") << endl;
}
void solve() {
  vector<int> S;
  for (int i = 0; i < (N); ++i) {
    if (S.empty())
      S.push_back(A[i]);
    else if (S.back() == A[i]) {
      S.pop_back();
      S.push_back(-A[i]);
    } else if (S.back() < 0) {
      if (int((S).size()) > 1 && S[int((S).size()) - 2] == A[i]) {
        if (-S.back() <= A[i]) {
          S.pop_back(), S.pop_back();
          S.push_back(-A[i]);
        } else {
          cout << "NO" << endl;
          return;
        }
      } else
        S.push_back(A[i]);
    } else {
      S.push_back(A[i]);
    }
    while (int((S).size()) > 1 && S[int((S).size()) - 1] < 0 &&
           S[int((S).size()) - 2] < 0) {
      int a = S[int((S).size()) - 1], b = S[int((S).size()) - 2];
      S.pop_back(), S.pop_back();
      S.push_back(min(a, b));
    }
  }
  unordered_set<int> T;
  int lb = 0;
  for (int i = 0; i < (int((S).size())); ++i) {
    if (S[i] > 0)
      T.emplace(S[i]);
    else
      SMIN(lb, S[i]);
  }
  if (T.empty())
    cout << "YES" << endl;
  else if (int((T).size()) > 1)
    cout << "NO" << endl;
  else {
    int x = *T.begin();
    cout << (x >= abs(lb) ? "YES" : "NO") << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(12) << fixed;
  cin >> N;
  for (int i = 0; i < (N); ++i) cin >> A[i];
  solve();
  return 0;
}
