#include <bits/stdc++.h>
using namespace std;
map<long long int, long long int> mp;
set<long long int> st;
set<long long int>::iterator it;
vector<long long int> ad[200009], vec(200009);
int main() {
  long long int a, b, c, d, e, i, j, k, l, n, m, x, y, t, p, mx = 0;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a);
    mx = max(mx, a);
    vec[i] = a;
    st.insert(a);
  }
  vec[0] = vec[n + 1] = 5000000000000000;
  l = st.size();
  if (l == 1) {
    cout << "YES\n";
    return 0;
  }
  x = 1;
  for (it = st.begin(); it != st.end(); it++) {
    a = *it;
    mp[a] = x;
    x++;
  }
  for (i = 1; i <= n; i++) {
    a = vec[i];
    b = mp[a];
    ad[b].push_back(i);
  }
  for (i = 1; i < l; i++) {
    x = ad[i].size();
    if (x % 2 == 0) {
      for (j = 0; j < x; j += 2) {
        if (((ad[i][j + 1] - ad[i][j] + 1) % 2) != 0) {
          cout << "NO\n";
          return 0;
        }
      }
    } else {
      cout << "NO\n";
      return 0;
    }
  }
  for (i = 1; i <= n; i++) {
    if (vec[i] != mx) {
      a = 0;
      b = vec[i];
      j = i - 1;
      while (vec[i] == b && i <= n) {
        a++;
        i++;
      }
      i--;
      if (a % 2 == 1 && vec[i + 1] > b && vec[j] > b) {
        cout << "NO\n";
        return 0;
      }
    }
  }
  cout << "YES\n";
  return 0;
}
