#P8051. Maximum Gliding Moves
Maximum Gliding Moves
Maximum Gliding Moves
A fat bird is flying among n trees arranged from left to right, where the height of the i-th tree is \(a_i\). The bird can only glide (fly) from one tree to another, but it can only glide toward trees with lower heights. Specifically, if the bird is currently on a tree with height \(a_i\), it can glide to another tree (to its left or right) with height strictly less than \(a_i\), and all trees passed over during the glide must also have heights less than \(a_i\).
In addition, the bird has k teleportation cards. The i-th card has a magical power value \(b_i\). At any moment while standing on any tree, the bird may choose to use the next available teleportation card (in the given order) to instantly move (teleport) to any tree whose height is at most \(b_i\). (Note that if the bird is already on a tree with height \(\le b_i\), it could even teleport to the same tree.) Teleportation does not count as a gliding move. It is guaranteed that every teleportation card can be used (i.e. for every card, there exists at least one tree with height \(\le b_i\)).
The bird starts at the first tree. Determine the maximum number of gliding moves (not counting teleportations) that the bird can perform. The bird may revisit the same tree multiple times.
inputFormat
The first line contains two integers \(n\) and \(k\) (the number of trees and the number of teleportation cards).
The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) representing the heights of the trees.
The third line contains \(k\) integers \(b_1, b_2, \dots, b_k\) representing the magical power values of the teleportation cards (in the order they must be used).
outputFormat
Output a single integer: the maximum number of gliding moves the bird can perform.
sample
3 1
3 2 1
2
2