Gathering Sharks
시간 제한2초메모리 제한2048 MB
서로 다른 번호가 붙은 n마리의 상어가 일렬로 있을 때, 번호 b인 그룹을 b보다 작은 번호 중 가장 큰 그룹으로 합치는 명령을 반복해 모두 한 점에 모으는 최소 시간을 구한다.
문제
You are the leader of a swarm of sharks living in a one-dimensional ocean. The sharks are positioned from left to right, with each adjacent pair separated by a distance of one unit.
As the leader, you want all the sharks to gather at a common point to form a single group. Initially, no two sharks belong to the same group; for each , the -th shark from the left forms its own group, uniquely numbered , consisting of only itself.
To achieve your goal, you can command the sharks to perform the following actions times.
-
You shout out an integer that meets both conditions:
- There exists a group numbered .
- There exists at least one group numbered strictly smaller than .
-
Afterward, letting be the largest existing group number strictly smaller than , all the sharks in the group numbered simultaneously move to the position of the group numbered , and the two groups merge.
-
The merged group is numbered , and the group numbered ceases to exist.
All sharks move at a constant speed of one unit distance per unit time. Commands must be executed sequentially, with no overlap in execution. Once a command is completed, the next one can begin immediately.
Compute the minimum time required for all the sharks to gather at a common point by commanding the sharks times optimally.
입력
The first line of input contains an integer (). The second line contains pairwise distinct integers ().
출력
Output the minimum time required for all the sharks to gather at a common point.