Under construction

Still being built. Things may change

Pandora's Box

Time limit2sMemory limit1024 MB

Summary
Given n mountain heights, find the fewest segment operations on equal-height endpoints that make the heights nondecreasing, and list the segments in order.
Level

Hard9 of 10

Topics
Dynamic programming, Hash map, Intervals
Solved
No attempts yet

Problem

To defeat Ares, the god of war, Kratos must reach Pandora's box, which can grant its owner truly divine power. Unfortunately for the Spartan, the box lies in the depths of the Temple of Pandora, and nn mountains stand on the way to the temple. The height of the ii-th mountain is aia_i meters.

The only thing the mighty Kratos fears is heights. That is why he never descends or jumps down, since large drops in height frighten him. He does jump very well, and he has a divine skill: if the height of the ii-th mountain equals the height of the jj-th, Kratos can make all mountains on the segment from ii to jj inclusive have height aia_i in a single action.

To reach the temple, Kratos must apply his skill to some segments so that he never has to go down, that is, the condition ai≤ai+1a_i \le a_{i+1} holds. Kratos is in a hurry and does not want to be noticed by Ares, so he cannot change the heights too often. Help Kratos reach the temple with the minimum number of actions.

Input

The first line contains an integer nn, the number of mountains on the way to the temple (1≤n≤1061 \le n \le 10^6).

The second line contains nn integers aia_i, the heights of the mountains (1≤ai≤1061 \le a_i \le 10^6).

Output

In the first line, print pp, the minimum number of actions Kratos must perform to reach the temple.

In each of the next pp lines, print two numbers ll and rr, the bounds of the segment for the next leveling action. Print the actions in the order Kratos must perform them.

If there is no solution, print -1 on a single line.

Examples2

  1. Example 1

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

    Input
    10
    1 2 1 3 1 5 6 5 6 6
    
    Expected output
    2
    1 5
    6 8