#P7041. King's Heir Selection

    ID: 20248 Type: Default 1000ms 256MiB

King's Heir Selection

King's Heir Selection

After the sudden death of King Fert XIII, the ministers of the Flatland Kingdom are in a dilemma. King Fert had n sons and, after the birth of every son, he updated his testament to name the newly born as his heir. However, according to the law, only a son who is at least \(18\) years old at the time of the king's death is eligible to become king.

Your task is to help the ministers decide who the new king should be. Given the ages of the n sons in their order of birth (from the oldest to the youngest), the correct candidate is the one born latest (i.e. the one with the highest index) with an age of at least \(18\). If no son meets this condition, output \(-1\).

Input Format:

  • The first line contains an integer \(n\) \,(1 \le n \le 10^5) representing the number of sons.
  • The second line contains \(n\) space-separated integers, where each integer denotes the age of a son in order of birth.

Output Format:

  • Output a single integer: the 1-indexed position of the son who will become the king, or \(-1\) if no eligible son exists.

inputFormat

The first line contains an integer \(n\) (number of sons).
The second line contains \(n\) space-separated integers representing the ages of the sons in order of birth (oldest to youngest).

outputFormat

Output one integer which is the 1-indexed position of the eligible son chosen as king. If no son is eligible, output \(-1\).

sample

6
16 18 17 22 15 14
4