#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e6 + 10;
const int inf = (int)2e9;
int arr[N], n, mx = 0;
stack<int> stk;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
    mx = max(mx, arr[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (arr[i] == mx) continue;
    int j = i;
    while (j <= n && arr[j] != mx) {
      if (!stk.empty() && stk.top() == arr[j]) {
        stk.pop();
      } else if (stk.empty() || stk.top() > arr[j]) {
        stk.push(arr[j]);
      } else {
        printf("NO\n");
        return 0;
      }
      j++;
    }
    if (!stk.empty()) {
      printf("NO\n");
      return 0;
    }
    i = j;
  }
  printf("YES\n");
  return 0;
}
