Title: つらら

Problem:
JOI, who lives in Canada, has icicles hanging from the eaves of his house. He decides to investigate them. There are N icicles (2 ≤ N ≤ 100,000) hanging from the eaves of JOI's house. These icicles are lined up in a straight line, with the i-th icicle hanging at a position i cm from the left end of the eaves. The initial length of the i-th icicle is ai cm (where ai is an integer greater than or equal to 1). These icicles grow according to the following rules:

- The i-th icicle grows 1 cm per hour only if it is longer than both the (i-1)-th and (i+1)-th icicles (except for the icicles at the ends).
- All icicles break at the moment they reach a length of L cm (2 ≤ L ≤ 50,000), and are considered to have a length of 0 cm thereafter.

At the initial stage, the lengths of adjacent pairs of icicles are all different. JOI wants to know the time it takes for all the icicles to break and become 0 cm in length.

Input:
The first line of input contains two integers N and L, representing the number of icicles and the maximum length of the icicles, respectively. The following N lines (1 ≤ i ≤ N) contain integers ai (1 ≤ ai < L), representing the initial length of the i-th icicle.

Output:
Output a single integer representing the time it takes for all the icicles to break.

Example:
Input
4 6
4
2
3
5

Output
8

In this case, the icicles break after 2, 8, 4, and 1 hours, respectively. Therefore, the time it takes for all the icicles to break is 8 hours.

# Buggy version:
def orderN(N,L,ices):
    upPeak = L - ices[0]
    downPeak = L - ices[0]
    peaks = []
    for i in xrange(len(ices)):
        if i < N-1:
            if ices[i] < ices[i+1]:
                peaks.append(downPeak)
                downPeak = L - ices[i+1]
                upPeak += L - ices[i+1]
            else:
                peaks.append(upPeak)
                upPeak = L - ices[i+1]
                downPeak += L - ices[i+1]
        else:
            peaks.append(upPeak)
            peaks.append(downPeak)
    print(max(peaks))

N,L = map(int,rawinput().strip().split())
ices = []
while True:
    ice = int(rawinput().strip())
    ices.append(ice)
    if len(ices) == N:
        break

orderN(N,L,ices)

# Fixed version:
def orderN(N,L,ices):
    upPeak = L - ices[0]
    downPeak = L - ices[0]
    peaks = []
    for i in range(len(ices)):
        if i < N-1:
            if ices[i] < ices[i+1]:
                peaks.append(downPeak)
                downPeak = L - ices[i+1]
                upPeak += L - ices[i+1]
            else:
                peaks.append(upPeak)
                upPeak = L - ices[i+1]
                downPeak += L - ices[i+1]
        else:
            peaks.append(upPeak)
            peaks.append(downPeak)
    print(max(peaks))

N,L = map(int,input().strip().split())
ices = []
while True:
    ice = int(input().strip())
    ices.append(ice)
    if len(ices) == N:
        break

orderN(N,L,ices)

# Buggy version:
def orderN(N,L,ices):
    upPeak = L - ices[0]
    downPeak = L - ices[0]
    peaks = []
    for i in range(len(ices)):
        if i < N-1:
            if ices[i] < ices[i+1]:
                peaks.append(downPeak)
                downPeak = L - ices[i+1]
                upPeak += L - ices[i+1]
            elif ices[i] > ices[i+1]:
                peaks.append(upPeak)
                upPeak = L - ices[i+1]
                downPeak += L - ices[i+1]
        elif i == N-1:
            peaks.append(upPeak)
            peaks.append(downPeak)
    print(max(peaks))

N,L = map(int,input().strip().split())
ices = [int(raw_input().strip()) for _ in xrange(N)]

orderN(N,L,ices)

# Fixed version:
def orderN(N,L,ices):
    upPeak = L - ices[0]
    downPeak = L - ices[0]
    peaks = []
    for i in range(len(ices)):
        if i < N-1:
            if ices[i] < ices[i+1]:
                peaks.append(downPeak)
                downPeak = L - ices[i+1]
                upPeak += L - ices[i+1]
            elif ices[i] > ices[i+1]:
                peaks.append(upPeak)
                upPeak = L - ices[i+1]
                downPeak += L - ices[i+1]
        elif i == N-1:
            peaks.append(upPeak)
            peaks.append(downPeak)
    print(max(peaks))

N,L = map(int,input().strip().split())
ices = [int(input().strip()) for _ in range(N)]


orderN(N,L,ices)

# Buggy version:
def solve():
  n,l = map(int,input().split())
  
  length = [int(input()) for i in range(n)]
  pare = [(length[i], i) for i in range(n)]
  pare.sort(reverse=True)
  dp = [0] * (n + 1)
  for p in pare:
    i = p[0]
    if i == 0:
      dp[i] = dp[i + 1] + (l - length[i])
    else:
      dp[i] = max(dp[i - 1], dp[i + 1]) + (l - length[i])
  print(max(dp))
solve()


# Fixed version:
def solve():
  n,l = map(int,input().split())
  
  length = [int(input()) for i in range(n)]
  pare = [(length[i], i) for i in range(n)]
  pare.sort(reverse=True)
  dp = [0] * (n + 1)
  for p in pare:
    i = p[1]
    if i == 0:
      dp[i] = dp[i + 1] + (l - length[i])
    else:
      dp[i] = max(dp[i - 1], dp[i + 1]) + (l - length[i])
  print(max(dp))
solve()