def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 P = int(input[idx]) idx += 1 H = list(map(int, input[idx:idx+N])) idx += N if P == 0: print(0) return # Compute sum1: sum1[i] is the cost to move from 1 to (i+1)th location (0-based) sum1 = [0] * N for i in range(1, N): sum1[i] = sum1[i-1] + max(0, H[i] - H[i-1]) # Compute sum2: sum2[i] is the cost to move from (i+1)th location to N-1th location (0-based) sum2 = [0] * N for i in range(N-2, -1, -1): sum2[i] = sum2[i+1] + max(0, H[i] - H[i+1]) # Compute the minimal cost when using exactly one teleport min_cost = float('inf') for k in range(N): current = sum1[k] + sum2[k] + P if current < min_cost: min_cost = current # Compare with the cost of not teleporting no_teleport_cost = sum1[-1] print(min(min_cost, no_teleport_cost)) if __name__ == "__main__": main()