#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long arr[n + 1];
  arr[n] = 1e9 + 5;
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  string ans = "YES";
  priority_queue<
      pair<pair<long long, long long>, pair<long long, long long> >,
      vector<pair<pair<long long, long long>, pair<long long, long long> > >,
      greater<pair<pair<long long, long long>, pair<long long, long long> > > >
      comp;
  long long left[n], right[n];
  bool visited[n];
  memset(visited, 0, sizeof(visited));
  long long id[n];
  long long curr = 0;
  for (long long i = 0; i < n; i++) {
    long long x = arr[i];
    long long start = i;
    while (x == arr[i]) {
      i++;
    }
    i--;
    comp.push({{arr[i], curr}, {start, i}});
    id[start] = curr;
    id[i] = curr;
    right[start] = i;
    left[i] = start;
    curr++;
  }
  while (comp.size() > 1) {
    pair<pair<long long, long long>, pair<long long, long long> > p =
        comp.top();
    comp.pop();
    if (visited[p.first.second]) continue;
    long long l = p.second.first;
    long long r = p.second.second;
    if (l == 0 && r == n - 1) {
      break;
    }
    if ((r - l + 1) % 2 != 0) {
      ans = "NO";
      break;
    }
    if (r == n - 1 || (l != 0 && arr[l - 1] < arr[r + 1])) {
      long long newleft = left[l - 1];
      arr[r] = arr[newleft];
      right[newleft] = r;
      left[r] = newleft;
      visited[id[newleft]] = 1;
      id[newleft] = p.first.second;
      comp.push({{arr[newleft], p.first.second}, {newleft, r}});
    } else if (l == 0 || (r != n - 1 && arr[l - 1] > arr[r + 1])) {
      long long newright = right[r + 1];
      arr[l] = arr[newright];
      left[newright] = l;
      right[l] = newright;
      visited[id[newright]] = 1;
      id[newright] = p.first.second;
      comp.push({{arr[newright], p.first.second}, {l, newright}});
    } else {
      long long newleft = left[l - 1];
      long long newright = right[r + 1];
      left[newright] = newleft;
      right[newleft] = newright;
      visited[id[newright]] = 1;
      id[newright] = p.first.second;
      right[newleft] = newright;
      left[newright] = newleft;
      visited[id[newleft]] = 1;
      id[newleft] = p.first.second;
      comp.push({{arr[newright], p.first.second}, {newleft, newright}});
    }
  }
  cout << ans << '\n';
  return 0;
}
