#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int arr[n + 5];
  for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
  stack<pair<int, int> > q;
  for (int i = 1; i <= n; i++) {
    int now = arr[i];
    int cnt = 0;
    while (i <= n && arr[i] == now) {
      cnt++;
      i++;
    }
    if (!q.empty() && q.top().first == now) {
      cnt += q.top().second;
      q.pop();
    }
    if (i <= n) {
      while (!q.empty() && now > q.top().first) {
        if (q.top().second % 2 == 1) {
          printf("NO\n");
          return 0;
        } else {
          cnt += q.top().second;
          q.pop();
        }
      }
      if (!q.empty() && q.top().first == now) {
        cnt += q.top().second;
        q.pop();
      }
      if (now < arr[i]) {
        if (cnt % 2 == 1) {
          printf("NO\n");
          return 0;
        } else {
          if (!q.empty()) {
            if (q.top().first <= arr[i]) {
              pair<int, int> pr = q.top();
              q.pop();
              pr.second += cnt;
              q.push(pr);
            } else
              q.push(make_pair(arr[i], cnt));
          } else
            q.push(make_pair(now, cnt));
        }
      } else {
        q.push(make_pair(now, cnt));
      }
    } else {
      while (!q.empty() && now > q.top().first) {
        if (q.top().second % 2 == 1) {
          printf("NO\n");
          return 0;
        } else {
          cnt += q.top().second;
          q.pop();
        }
      }
      if (!q.empty() && q.top().first == now) {
        cnt += q.top().second;
        q.pop();
      }
      q.push(make_pair(now, cnt));
      while (!q.empty()) {
        pair<int, int> f1 = q.top();
        q.pop();
        if (!q.empty()) {
          if (f1.second % 2 == 1) {
            printf("NO\n");
            return 0;
          } else {
            pair<int, long long> f2 = q.top();
            q.pop();
            f2.second += f1.second;
            q.push(f2);
          }
        }
      }
    }
    i--;
  }
  printf("YES\n");
  return 0;
}
