#include <bits/stdc++.h>
using namespace std;
int n, a[200010], br, stek[200010];
int main() {
  scanf("%d", &n);
  int maxx = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    maxx = max(maxx, a[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (stek[br] == a[i] && a[i - 1] <= a[i])
      br--;
    else
      stek[++br] = a[i];
  }
  if (br > 1 || (stek[br] < maxx && br == 1))
    printf("NO\n");
  else
    printf("YES\n");
  return 0;
}
