#include <bits/stdc++.h>
int arr[200010];
int group[200010][4];
int order[200010];
int memory[200010];
int compare(int l, int r) {
  if (group[order[l]][1] < group[order[r]][1]) return 1;
  return 0;
}
void copyArrToMemory(int idx, int midx) { memory[midx] = order[idx]; }
void copyMemoryToArr(int idx, int midx) { order[idx] = memory[midx]; }
void merge(int l, int r) {
  int m = (l + r) / 2;
  int lidx = l;
  int ridx = m + 1;
  int midx = 0;
  while (lidx <= m && ridx <= r) {
    if (compare(lidx, ridx))
      copyArrToMemory(lidx++, midx++);
    else
      copyArrToMemory(ridx++, midx++);
  }
  while (lidx <= m) copyArrToMemory(lidx++, midx++);
  while (ridx <= r) copyArrToMemory(ridx++, midx++);
  for (int i = 0; i < midx; i++) copyMemoryToArr(l + i, i);
}
void mergeSort(int l, int r) {
  if (l >= r) return;
  int m = (l + r) / 2;
  mergeSort(l, m);
  mergeSort(m + 1, r);
  merge(l, r);
}
int main(void) {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < 200010 - 1; i++) group[i + 1][0] = i;
  for (int i = 0; i < 200010 - 1; i++) group[i][3] = i + 1;
  for (int i = 0; i < N; i++) scanf("%d", &arr[i]);
  group[0][1] = 1000000007;
  group[0][2] = 1;
  int groupIdx = 1;
  for (int i = 0; i < N; i++) {
    int start = i;
    while (arr[start] == arr[i]) i++;
    group[groupIdx][1] = arr[start];
    group[groupIdx++][2] = i - start;
    i--;
  }
  group[groupIdx][1] = 1000000007;
  group[groupIdx++][2] = 1;
  for (int i = 0; i < groupIdx; i++) order[i] = i;
  mergeSort(0, groupIdx - 1);
  for (int i = 0; i < groupIdx; i++) {
    int current = order[i];
    if (group[current][1] < 0) continue;
    if (group[current][2] == N) {
      printf("YES\n");
      return 0;
    }
    if (group[current][2] % 2) {
      printf("NO\n");
      return 0;
    }
    int prev = group[current][0];
    int next = group[current][3];
    if (group[prev][1] < group[next][1]) {
      group[prev][3] = next;
      group[next][0] = prev;
      group[prev][2] += group[current][2];
    } else if (group[prev][1] > group[next][1]) {
      group[prev][3] = next;
      group[next][0] = prev;
      group[next][2] += group[current][2];
    } else {
      group[next][1] = -1;
      group[prev][3] = group[next][3];
      group[group[prev][3]][0] = prev;
      group[prev][2] += group[current][2];
      group[prev][2] += group[next][2];
    }
  }
  return 0;
}
