Under construction

Still being built. Things may change

Maximal Subsequence

Time limit1sMemory limit256 MB

Summary
Given an array, find the longest subsequence whose LIS length is strictly smaller than the LIS length of the whole array.
Level

Medium7 of 10

Topics
Dynamic programming, Binary search, Graph
Solved
No attempts yet

Problem

Let the beauty of a sequence be the length of its longest increasing subsequence.

You are given an array aa consisting of nn integers. Find the maximum length of a subsequence of array aa such that the beauty of this subsequence is less than the beauty of the whole array aa.

Input

The first line contains a single integer nn, the number of elements in array aa (1≤n≤5⋅1051 \le n \le 5 \cdot 10^5).

The second line contains nn space-separated integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9).

Output

Print one integer: the maximum length of a subsequence of array aa such that its beauty is less than the beauty of the whole array aa.

Examples5

  1. Example 1

    Input
    3
    2 1 3
    
    Expected output
    2
    
  2. Example 2

    Input
    4
    4 3 2 1
    
    Expected output
    0
    
  3. Example 3

    Input
    4
    2 1 4 3
    
    Expected output
    2
    
  4. Example 4

    Input
    6
    4 6 5 2 1 3
    
    Expected output
    4
    
  5. Example 5

    Input
    4
    3 4 1 2
    
    Expected output
    2