#K75097. Min Heap Validator

    ID: 34343 Type: Default 1000ms 256MiB

Min Heap Validator

Min Heap Validator

You are given an array \(A\) of \(N\) integers. Your task is to determine whether the array represents a valid min heap.

A binary heap is a complete binary tree. In a min heap, every parent node is less than or equal to its children. In other words, for all valid indices \(i\), if the left child \(A[2i+1]\) and/or right child \(A[2i+2]\) exists, then the following conditions must hold:

  • \(A[i] \leq A[2i+1]\) if \(2i+1 < N\)
  • \(A[i] \leq A[2i+2]\) if \(2i+2 < N\)

An empty array is considered a valid min heap. Use the formulas \(2i+1\) for the left child and \(2i+2\) for the right child. Ensure that your solution reads input from stdin and writes output to stdout.

inputFormat

The input is given as follows:

  1. The first line contains a single integer \(N\), the number of elements in the array.
  2. The second line contains \(N\) space-separated integers representing the elements of the array.

outputFormat

Output a single line containing either "YES" if the given array is a valid min heap, or "NO" if it is not.

## sample
6
3 5 9 6 8 20
YES