Problem:
つらら
問題
カナダに住む JOI 君の家の軒下には,立派なつららが出来ている.せっかくなので,JOI 君はつららについて調べてみることにした.
JOI 君の家の軒下には N 本(2 ≤ N ≤ 100000 = 10
5
)のつららが出来ている.これらのつららは一直線上に並んでおり,軒下の左端から i cm(1 ≤ i ≤ N )の位置に i 本目のつららが出来ている.i 本目のつららの長さは最初 ai cm(ai は 1 以上の整数)である.これらのつららは,次のような規則によって伸びていく:
i 本目のつららは,i − 1 本目のつららと i + 1 本目のつららの両方よりも長い場合にのみ,1 時間につき 1 cm ずつ伸びる(ただし,両端のつららに関しては片方の隣のみ考える.すなわち,1 本目のつららは 2 本目のつららより長ければ伸び,N 本目のつららは N − 1 本目のつららより長ければ伸びる).
どのつららも,L cm(2 ≤ L ≤ 50000)に達した瞬間に,根元から折れる(折れたつららは,以後長さ 0 cm のつららとみなす)
.
最初の段階で,隣り合う 2 本のつららの長さはすべて異なっている.このとき,十分な時間が経過すれば,N 本すべてのつららが折れて長さ 0 cm となる.JOI 君は,つららがこのような状態になるまでの時間を知りたくなった.
N 本のつららの最初の長さとつららの限界の長さ L が与えられると,すべてのつららが折れるまでにかかる時間を求めるプログラムを作成せよ.
入力
入力の 1 行目には,つららの本数を表す整数 N とつららの限界の長さを表す整数
L が,空白を区切りとしてこの順に書かれている.入力の i + 1 行目 (1 ≤ i ≤ N) に
は, i 本目のつららの最初の長さを表す整数 ai (1 ≤ ai < L) が書かれている.
採点用データのうち,配点の 30% 分については,N ≤
500 かつ L ≤ 
1000 を満
たす.
出力
出力 は,すべてのつららが折れるまでにかかる時間を表す 1 つの整数のみを含む 1 行からなる.
入出力例
入力例 1
4 6
4
2
3
5
出力例 1
8
例 1 の場合,1, 2, 3, 4 本目のつららは,それぞれ 2, 8, 4, 1 時間後に折れる.した
がって,すべてのつららが折れるまでにかかる時間は 8 時間であるので,8 を出力
する.
入力例 2
6 10
3
4
1
9
5
1
出力例 2
15
上記問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>>Q;
long long dist[200000], a[200000], n, L, maxn;
bool G(int t) { if (a[t] > a[t - 1] && a[t] > a[t + 1])return true; return false; }
int main() {
	cin >> n >> L; for (int i = 1; i <= n; i++)cin >> a[i];
	for (int i = 1; i <= n; i++) {
		if (G(i) == true) { Q.push(make_pair(L - a[i], i)); dist[i] = L - a[i]; }
	}
	while (!Q.empty()) {
		long long a1 = Q.top().first, a2 = Q.top().second; Q.pop(); a[a2] = 0; maxn = a1;
		if (a2 >= 2 && G(a2 - 1) == true) { dist[a2 - 1] = a1 + (L - a[a2 - 1]); Q.push(make_pair(dist[a2 - 1], a2 - 1)); }
		if (a2 <= n - 1 && G(a2 + 1) == true) { dist[a2 + 1] = a1 + (L - a[a2 + 1]); Q.push(make_pair(dist[a2 + 1], a2 + 1)); }
	}
	cout << maxn << endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5864
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include<utility>
#include<set>
#include<deque>
#include <unordered_map>
#include<string>
#include<map>
#include<cmath>
#include <unordered_set>

using namespace std;
int N, L;


vector<int>Li;
vector<int>memo;

//メモ化する
int T(int n,vector<int>&memo) {
	int ans = 0;
	if (n == 0) {
		if (Li[n] < Li[n + 1]) {
			if (memo[n + 1] == -1) {
				ans = L - Li[n] + T(n + 1,memo);
			}
			else {
				ans = L - Li[n] + memo[n+1];
			}
		}
		else {
			memo[n] = L - Li[n];
			return L-Li[n];
		}
	}
	else if (n == N - 1) {
		if (Li[n] < Li[n - 1]) {
			if (memo[n - 1] == -1) {
				ans = L - Li[n] + T(n - 1,memo);
			}
			else {
				ans = L - Li[n] + memo[n-1];
			}
		}
		else {
			memo[n] = L - Li[n];
			return L-Li[n];
		}
	}
	else {
		int z = -1;
		if (Li[n] < Li[n - 1]) {
			z = 0;
			if (memo[n - 1] == -1) {
				z = L - Li[n] + T(n - 1,memo);
			}
			else {
				z = L - Li[n] + memo[n - 1];
			}
			ans = z;
		}
		if (Li[n] < Li[n + 1]) {
			z = 0;
			if (memo[n + 1] == -1) {
				z = L - Li[n] + T(n + 1,memo);
			}
			else {
				z = L - Li[n] + memo[n + 1];
			}
			ans = max(ans,z);
		}
		if (z == -1) {
			memo[n] = L - Li[n];
			return L - Li[n];
		}
		
	}
	memo[n] = ans;
	return ans;

}

int main() {
	
	cin >> N >> L;
	for (int i = 0; i < N; i++) {
		int x;
		cin >> x;
		Li.push_back(x);
		memo.push_back(-1);
		
	}
	
	
	
	int ans = 0;
	
	
	for (int i = 0; i < N; i++) {
		
		ans = max(ans, T(i,memo));
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 5860
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

// イベント用の構造体 (折損時刻 t, 氷柱 idx)
struct Event {
    ll t;
    int idx;
    bool operator<(Event const &e) const {
        // priority_queue は最大ヒープなので逆順に
        return t > e.t;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    ll L;
    cin >> N >> L;

    vector<ll> a(N);
    for(int i = 0; i < N; i++){
        cin >> a[i];
    }

    // 「ピーク化開始時刻」(まだピーク化していなければ -1)
    vector<ll> start(N, -1);
    // 折損済みフラグ
    vector<bool> broken(N, false);

    // 時刻順イベント
    priority_queue<Event> pq;

    // 領域外参照防止のため、長さ0のダミーを両端に設定
    auto length_at = [&](int i, ll now){
        if(i < 0 || i >= N) return 0LL;
        if(broken[i]) return 0LL;
        if(start[i] < 0) return a[i];
        // start[i]から now までずっとピーク成長した分を足す
        return a[i] + (now - start[i]);
    };

    // 「いまピーク化すべきか」を判定
    auto try_peak = [&](int j, ll now){
        if(j < 0 || j >= N) return;
        if(broken[j] || start[j] >= 0) return; // 壊済み or 既にピーク化済み
        ll Lc = length_at(j, now);
        ll Ll = length_at(j-1, now);
        ll Lr = length_at(j+1, now);
        if(Lc > Ll && Lc > Lr){
            // ピーク化開始
            start[j] = now;
            // 折損予定 = now + (L - 初期 a[j])
            ll t_break = now + (L - a[j]);
            pq.push({t_break, j});
        }
    };

    // 初期ピークをスケジュール
    for(int i = 0; i < N; i++){
        // 両隣より長ければピーク
        ll li = (i>0? a[i-1] : 0);
        ll ri = (i+1<N? a[i+1] : 0);
        if(a[i] > li && a[i] > ri){
            start[i] = 0;
            pq.push({L - a[i], i});
        }
    }

    ll ans = 0;
    // イベントを時刻順に消化
    while(!pq.empty()){
        auto ev = pq.top(); pq.pop();
        ll t = ev.t;
        int i = ev.idx;
        if(broken[i]) continue; // 既に壊れていたらスキップ
        // 実際に折損させる
        broken[i] = true;
        ans = max(ans, t);
        // 隣の 2 本を「今、この時刻でピーク化すべきか」チェック
        try_peak(i-1, t);
        try_peak(i+1, t);
    }

    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5704
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main(){
    int n,l;
    cin >> n >> l;
    vector<int> a(n);
    rep(i,n){
        cin >> a[i];
    }

    vector<int> ldp(n),rdp(n);
    ldp[0]=l-a[0];
    for(int i=1;i<n;i++){
        if(a[i-1]<a[i]){
            ldp[i]=l-a[i];
        }
        else{
            ldp[i]=ldp[i-1]+l-a[i];
        }
    }
    rdp[n-1]=l-a[n-1];
    for(int i=n-2;i>=0;i--){
        if(a[i+1]<a[i]){
            rdp[i]=l-a[i];
        }
        else{
            rdp[i]=rdp[i+1]+l-a[i];
        }
    }
    int res=0;
    rep(i,n){
        res = max({res,ldp[i],rdp[i]});
    }
    cout << res << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4316
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < N; i++)
int I[110000] = {};
int r[110000] = {};
int N, L;

int rec(int loc)
{
  if (r[loc] != 0)
    return r[loc];
  else
  {
    if (I[loc - 1] < I[loc] && I[loc] > I[loc + 1])
    {
      r[loc] = L - I[loc];
    }
    else if (I[loc - 1] > I[loc] && I[loc] < I[loc + 1])
    {
      r[loc] = (L - I[loc] + max(rec(loc - 1), rec(loc + 1)));
    }
    else if (I[loc - 1] > I[loc] && I[loc] > I[loc + 1])
    {
      r[loc] = L - I[loc] + rec(loc - 1);
    }
    else if (I[loc - 1] < I[loc] && I[loc] < I[loc + 1])
    {
      r[loc] = L - I[loc] + rec(loc + 1);
    }
    return r[loc];
  }
}

int main()
{
  cin >> N >> L;
  rep(i, N) cin >> I[i + 1]; // 1 - N

  for (int i = 1; i < N + 1; i++)
  {
    rec(i);
  }
  cout << *max_element(r + 1, r + N + 1) << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 5868
final_rank: 17

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  if (x < 0) { cout << (i64)(x); }
  else { cout << (u64)(x); }
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  ll N, L;
  cin >> N >> L;
  vi a(N+2), dp(N+2, 0);
  pq<pi> Q;
  a[0] = a[N+1] = 0;
  rep(i, 1, N+1) cin >> a[i];
  rep(i, 1, N+1) Q.emplace(mp(a[i], i));
  while (!Q.empty()) {
    auto [l, i] = POP(Q);
    if (a[i] < a[i-1]) chmax(dp[i], dp[i-1]);
    if (a[i] < a[i+1]) chmax(dp[i], dp[i+1]);
    dp[i] += L - a[i];
  }
  print(MAX(dp));
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6632
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#define endl "\n"
#define INF INT_MAX
using namespace std;
bool check(char c,string s){
bool f = 0;
	for(int i = 0;i < s.size();i++){
		if(s[i] == c)f = 1;
	}
return f;
}
int main(){
#define int long long
cin.tie(nullptr);ios::sync_with_stdio(false);
int n,m;
cin >>n>>m;
vector<int>a(n+2);
for(int i = 1;i <= n;i++)cin >>a[i];
int c = 0,time = 0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>b;
for(int i = 1;i <= n;i++){
	if(a[i-1] < a[i]&&a[i+1] < a[i])b.push({m-a[i],i});
}
while(!b.empty()){
	int x = b.top().first,y = b.top().second;
	b.pop();
	time = x;
	c++;
	a[y] = 0;
	if(y-1 != 0&&a[y-2] < a[y-1]&&a[y] < a[y-1])b.push({m-a[y-1]+time,y-1});
	if(y+1 != n+1&&a[y+2] < a[y+1]&&a[y] < a[y+1])b.push({m-a[y+1]+time,y+1});
}
cout <<time<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4820
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N,L;
    cin>>N>>L;
    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> Q;
    vll A(N+2,0);
    rep(i,N){
        cin>>A[i+1];
    }
    rep(i,N){
        if(A[i]<A[i+1]&&A[i+1]>A[i+2]){
            Q.push({L-A[i+1],i+1});
        }
    }
    ll an=0;
    vll AN(N);
    while(!Q.empty()){
        auto  tn=Q.top();
        Q.pop();
        ll t=tn.first;
        ll n=tn.second;
        an=t;
        AN[n-1]=t;
        A[n]=0;
        if(n-1!=0&&A[n-1]>0){
            if(A[n-1]>A[n-2]){
                Q.push({t+L-A[n-1],n-1});
            }
        }
        if(n+1!=N+1&&A[n+1]>A[n+2]&&A[n+1]>0){
            Q.push({t+L-A[n+1],n+1});
        }
    }
    cout<<an<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 5456
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int n, l; cin >> n >> l;
  vector<int> a(n); for(auto&e : a) cin >> e;
  queue<int> qu;
  vector<int> seen(n);
  auto f = [&](int i){
    if(i < 0 || n <= i) return false;
    if(i && a[i-1] >= a[i]) return false;
    if(i+1 != n && a[i] <= a[i+1]) return false;
    if(seen[i]) return false;
    return true;
  };
  
  for(int i = 0; i < n; ++i){
    if(!f(i)) continue;
    qu.emplace(i); seen[i] = 1;
  }
  
  int64_t ans = 0;
  vector<int64_t> st(n);
  while(!qu.empty()){
    auto x = qu.front(); qu.pop();
    int64_t t = st[x] + l - a[x];
    ans = max(ans, t);
    a[x] = 0;
    if(f(x-1)){ qu.emplace(x-1); seen[x-1] = 1; st[x-1] = t; }
    if(f(x+1)){ qu.emplace(x+1); seen[x+1] = 1; st[x+1] = t; }
  }
  cout << ans << "\n";
}
accuracy: 1
time_ms: 10
memory_kb: 4940
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
//typedef int ll;
//typedef pair<ll, ll> P;
const long long INF = 1e9;
template <typename T>
void input_arr(vector<T>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i];
	}
}
template <typename T, typename Q>
void input_arr(vector<pair<T, Q>>& A, ll N) {
	for (ll i = 0; i < N; i++) {
		cin >> A[i].first >> A[i].second;
	}
}
template <typename T>
void input_arr(vector<vector<T>>& A, ll H, ll W) {
	for (ll i = 0; i < H; i++) {
		for (ll j = 0; j < W; j++) {
			cin >> A[i][j];
		}
	}
}
int main() {
    ll N, M; cin >> N >> M;
    vector<ll>A(N); input_arr(A, N);
    vector<ll>B = A;
    sort(B.begin(), B.end(), greater<ll>());
    for (ll i = 0; i < B.size(); i++) {
    }
    vector<vector<ll>>C(M + 1);
    for (ll i = 0; i < N; i++) {
        C[A[i]].push_back(i);
    }
    vector<bool>D(N, false);
    vector<ll>T(N, 0);
    for (ll i = 0; i < N; i++) {
        ll pos = C[B[i]].back();
        C[B[i]].pop_back();
        if (pos == 0) {
            if (!D[pos + 1]) {
                T[pos] = M - B[i];
            }
            else {
                T[pos] = T[pos + 1] + M - B[i];
            }
        }
        else if (pos == N - 1) {
            if (!D[pos - 1]) {
                T[pos] = M - B[i];
            }
            else {
                T[pos] = T[pos - 1] + M - B[i];
            }
        }
        else {
            if (!D[pos - 1] && !D[pos + 1]) {
                T[pos] = M - B[i];
            }
            else if (!D[pos - 1] && D[pos + 1]) {
                T[pos] = T[pos + 1] + M - B[i];
            }
            else if (D[pos - 1] && !D[pos + 1]) {
                T[pos] = T[pos - 1] + M - B[i];
            }
            else {
                T[pos] = max(T[pos - 1], T[pos + 1]) + M - B[i];
            }
        }
        D[pos] = true;
    }
    ll ans = -1;
    for (ll i = 0; i < N; i++) {
        ans = max(ans, T[i]);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 8180
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using lint = long long;
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, n) for(int i = int(l); i < int(n); i++)

int main(){
    int n;
    lint l;
    cin >> n >> l;
    lint a[n];
    rep(i, n) cin >> a[i];
    
    auto get = [&](int k){ return (0 <= k && k < n) ? a[k] : 0; };
    
    using P = pair<lint, int>;
    priority_queue<P, vector<P>, greater<P>> que;
    rep(i, n){
        if(a[i] > get(i - 1) && a[i] > get(i + 1)){
            que.push({l - a[i], i});
        }
    }
    
    lint ans = 0;
    while(!que.empty()){
        auto top = que.top();
        que.pop();
        int at = top.second;
        lint t = top.first;
        if(a[at] == 0) continue;
        a[at] = 0;
        
        ans = max(ans, t);
        
        if(at - 1 >= 0){
            int to = at - 1;
            if(a[to] > get(to - 1) && a[to] > get(to + 1)){
                que.push({t + l - a[to], to});
            }
        }
        if(at + 1 < n){
            int to = at + 1;
            if(a[to] > get(to - 1) && a[to] > get(to + 1)){
                que.push({t + l - a[to], to});
            }
        }
    }
    
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4900
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < N; i++)
int I[110000] = {};
int r[110000] = {};
int N, L;

int rec(int loc)
{
  if (r[loc] != 0)
    return r[loc];
  else
  {
    if (I[loc - 1] < I[loc] && I[loc] > I[loc + 1])
    {
      r[loc] = L - I[loc];
    }
    else if (I[loc - 1] > I[loc] && I[loc] < I[loc + 1])
    {
      r[loc] = (L - I[loc] + max(rec(loc - 1), rec(loc + 1)));
    }
    else if (I[loc - 1] > I[loc] && I[loc] > I[loc + 1])
    {
      r[loc] = L - I[loc] + rec(loc - 1);
    }
    else if (I[loc - 1] < I[loc] && I[loc] < I[loc + 1])
    {
      r[loc] = L - I[loc] + rec(loc + 1);
    }
    return r[loc];
  }
}

int main()
{
  cin >> N >> L;
  rep(i, N) cin >> I[i + 1]; // 1 - N

  for (int i = 1; i < N + 1; i++)
  {
    rec(i);
  }
  cout << *max_element(r + 1, r + N + 1) << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 5736
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    int n,l;
    cin >> n >> l;
    vector<int> a(n);
    for(int i=0; i<n; i++){
        cin >> a[i];
    }
    vector<int> ans(n, 0);
    ans[0] = max(ans[0], l-a[0]);
    for(int i=1; i<n; i++){
        if(a[i] < a[i-1]){
            ans[i] = max(ans[i], ans[i-1]+l-a[i]);
        }else{
            ans[i] = max(ans[i], l-a[i]);
        }
    }
    for(int i=n-2; i>=0; i--){
        if(a[i] < a[i+1]){
            ans[i] = max(ans[i], ans[i+1]+l-a[i]);
        }else{
            ans[i] = max(ans[i], l-a[i]);
        }
    }
    cout << *max_element(ans.begin(), ans.end()) << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3864
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n,k; cin >> n >> k;
	vector<int> dp(n,0);
	queue<int> q;
	vector<int> a(n);
	for(int i=0;i<n;i++){
		cin >> a[i];
	}
	for(int i=0;i<n;i++){
		if(!i or a[i-1]<a[i]){
			if(i+1==n or a[i]>a[i+1]){
				q.push(i);
			}
		}
	}
	while(q.size()){
		int p=q.front(); q.pop();
		int mx=0;
		if(p-1>=0){
			mx=dp[p-1];
			if(p-2<0 or dp[p-2]>0 or a[p-1]>a[p-2])if(!dp[p-1])q.push(p-1);
		}
		if(p+1<n){
			mx=max(mx,dp[p+1]);
			if(p+2>=n or dp[p+2]>0 or a[p+1]>a[p+2])if(!dp[p+1])q.push(p+1);
		}
		dp[p]=mx+(k-a[p]);
	}
	sort(dp.begin(), dp.end());
	printf("%d\n",dp.back());
}
accuracy: 1
time_ms: 10
memory_kb: 3980
final_rank: 2

code_snippet: #include<bits/stdc++.h>
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

ll in[12345678], jikan[12345678], used[12345678];
signed main() {
	ll n, l; cin >> n >> l;
	in[0] = 0; in[n + 1] = 0;
	rep2(i, n) { cin >> in[i]; }
	queue<ll> que;
	rep2(i, n) {
		if (in[i - 1] < in[i] && in[i] > in[i + 1]) { 
			jikan[i] = max(jikan[i - 1], jikan[i + 1]) + (l - in[i]); used[i]++; in[i] = 0;
			if (used[i - 1] == 0) { que.push(i - 1); }
			if (used[i + 1] == 0) { que.push(i + 1); }
		}
	}
	while (que.size()) {
		ll a = que.front(); que.pop();
		if (used[a] != 0) { continue; }
		if (a == 0 || a == n + 1) { continue; }
		if (in[a - 1] < in[a] && in[a] > in[a + 1]) {
			jikan[a] = max(jikan[a - 1], jikan[a + 1]) + (l - in[a]); used[a]++; in[a] = 0;
			if (used[a - 1] == 0) { que.push(a - 1); }
			if (used[a + 1] == 0) { que.push(a + 1); }
		}
	}
	ll ans = 0;
	rep2(i, n) { ans = max(ans, jikan[i]); }
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5988
final_rank: 18

code_snippet: #include <bits/stdc++.h>

using namespace std;

int N, L;
vector<pair<int, int> > IciclesInfo;
int DpTime[100002];

void solve() {
	int curId;

	for (int i = 0; i < N; ++i) {
		curId = IciclesInfo[i].second;
		if (DpTime[curId - 1] == 0 && DpTime[curId + 1] == 0) {
			DpTime[curId] = L - IciclesInfo[i].first;
			continue;
		}
		if (DpTime[curId - 1] > DpTime[curId + 1]) {
			DpTime[curId] = DpTime[curId - 1] + L - IciclesInfo[i].first;
		}
		else {
			DpTime[curId] = DpTime[curId + 1] + L - IciclesInfo[i].first;
		}
	}
}

int main() {
	int a;

	memset(DpTime, 0, sizeof(DpTime));
	cin >> N >> L;
	for (int i = 0; i < N; ++i) {
		cin >> a;
		IciclesInfo.push_back({ a,i + 1 });
	}
	sort(IciclesInfo.begin(), IciclesInfo.end(), greater<pair<int,int> >());
	
	solve();
	cout << *max_element(DpTime + 1, DpTime + N + 1) << endl;

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4208
final_rank: 12

code_snippet: #include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

bool ready(int i, vector<pii> &icicle) {
    int N = icicle.size();
    if(i < 0 || i >= N)
        return false;
    if(i == 0) {
        if(icicle[i].first > icicle[i + 1].first) {
            return true;
        }
    } else if(i == N - 1) {
        if(icicle[i].first > icicle[i - 1].first) {
            return true;
        }
    } else {
        if(icicle[i].first > icicle[i + 1].first &&
           icicle[i].first > icicle[i - 1].first) {
            return true;
        }
    }
    return false;
}

int main() {
    int N, L;
    cin >> N >> L;
    vector<pii> icicle(N);
    rep(i, N) {
        int a;
        cin >> a;
        icicle[i] = MP(a, 0);
    }
    queue<pii> que;
    rep(i, N) {
        if(ready(i, icicle)) {
            que.push(MP(i, 0));
        }
    }

    while(!que.empty()) {
        auto p = que.front();
        que.pop();
        int idx = p.first;
        int now = p.second;
        icicle[idx].second = now + (L - icicle[idx].first);
        icicle[idx].first = 0;
        if(ready(idx - 1, icicle)) {
            int ma = 0;
            if(idx - 2 >= 0)
                ma = icicle[idx - 2].second;
            que.push(MP(idx - 1, max(ma, icicle[idx].second)));
        }
        if(ready(idx + 1, icicle)) {
            int ma = 0;
            if(idx + 2 < N)
                ma = icicle[idx + 2].second;
            que.push(MP(idx + 1, max(ma, icicle[idx].second)));
        }
    }
    int res = 0;
    rep(i, N) { res = max(res, icicle[i].second); }
    cout << res << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4044
final_rank: 11

code_snippet: #include<bits/stdc++.h>
#define enld '\n'
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[6] = {1, 0, -1, 0,1,1};
const int dy[6] = {0, 1, 0, -1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ---------------------------------------------------------------------------

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll N,L;
    cin >> N >> L;
    vector<int> A(N);
    for(int i=0; i<N; i++){
        cin >> A[i];
    }
    vector<int> cnt(N,0);
    priority_queue<pair<ll,int>> que;
    if(A[0] > A[1]){
        cnt[0] = 0;
        que.emplace(A[0],0);
    }else{
        cnt[0] = 1;
    }
    if(A[N-1] > A[N-2]){
        cnt[N-1] = 0;
        que.emplace(A[N-1],N-1);
    }else{
        cnt[N-1] = 1;
    }
    for(int i=1; i<N-1; i++){
        if(A[i-1] > A[i]) cnt[i]++;
        if(A[i] < A[i+1]) cnt[i]++;
        if(!cnt[i]) que.emplace(A[i],i);
    }
    ll ans = 0;
    while(que.size()){
        auto v = que.top();
        que.pop();
        v.first += ans;
        ans += L-v.first;
        if(v.second != 0){
            cnt[v.second-1]--;
            if(cnt[v.second-1] == 0){
                que.emplace(A[v.second-1]-ans,v.second-1);
            }
        }
        if(v.second != N-1){
            cnt[v.second+1]--;
            if(cnt[v.second+1] == 0){
                que.emplace(A[v.second+1]-ans,v.second+1);
            }
        }
    }
    cout<< ans << enld;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4684
final_rank: 5

code_snippet: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
int main() {
    int N, L; scanf("%d%d", &N, &L);

    vector<int> A(N+2, -1);
    for(int i=1; i<=N; i++) scanf("%d", &A[i]);

    vector<bool> visited(N+2);
    visited[0] = visited[N+1] = true;

    priority_queue< pair<ll, ll>, vector< pair<ll, ll> >, greater< pair<ll, ll> > > que;
    for(int i=1; i<=N; i++) {
        bool ok = true;
        for(int j=-1; j<=1; j+=2) ok &= A[i] > A[i+j];
        if(ok) que.emplace(0LL, (ll)i);
    }

    ll ans = 0;
    while(que.size()) {
        ll l, k; tie(l, k) = que.top(); que.pop();
        l += L - A[k];
        chmax(ans, l);
        A[k] = 0;
        for(int j=-1; j<=1; j++) {
            if(visited[k+j]) continue;
            if(A[k+j-1] < A[k+j] and A[k+j+1] < A[k+j]) {
                visited[k+j] = true;
                que.emplace(l, k+j);
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4240
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
ll inf =  1e9;


int main(){
 ll n,l;
 cin>>n>>l;

 vector<ll> a(n+2,0);
 vector<ll> d(n+2,0);
 for(ll i=1;i<=n;i++) cin>>a[i];
 for(ll i=1;i<=n;i++){
  if( a[i] < a[i-1] )  d[i]++;
  if( a[i] < a[i+1] )  d[i]++;
 }

 priority_queue<mp,vector<mp>,greater<mp> > q;
 for(ll i=1;i<=n;i++){
  if( d[i] == 0 ) q.push( mp( l-a[i] ,i ) );
 }
 ll res = 0;
 while(!q.empty() ){
  mp now = q.top();
  q.pop();
  ll nn = now.second;
  ll time = now.first;
  res = max( res , time );
  d[nn-1]--;
  d[nn+1]--;
  if( d[nn-1] == 0 ){
   q.push( mp(time + l-a[nn-1] , nn-1 ) );
  }
  if( d[nn+1] == 0 ){
   q.push( mp(time + l-a[nn+1] , nn+1 ) );
  }
 }
 cout<<res<<endl;

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5448
final_rank: 19