alexjercan/bugnet huggingface dataset:
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

#define INF 1000000000

int main() {
  int n, T;
  cin >> n >> T;

  int trr[110000] = {0};
  rep(i, n) cin >> trr[i];
  int ans = 0;

  int sum = T - trr[0];
  for (int i = 1; i <= n; i++) {
    if (trr[i - 1] > trr[i] || i == n) {
      ans = max(ans, sum);
      sum = T - trr[i];
    } else {
      sum += T - trr[i];
    }
  }

  sum = T - trr[0];
  for (int i = 1; i <= n; i++) {
    if (trr[i - 1] < trr[i] || i == n) {
      ans = max(ans, sum);
      sum = T - trr[i];
    } else {
      sum += T - trr[i];
    }
  }

  printf("%d\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

int main() {
  while (1) {
    int n, d;
    cin >> n >> d;
    if (n == 0)
      break;

    int t[111111];
    rep(i, n) { cin >> t[i]; }

    int ans = 0;

    int flg = 0;
    int sum = 0;
    rep(i, n) {
      if (flg == 0) {
        sum += d - t[i];
        if (t[i] > t[i + 1]) {
          flg = 1;
        } else if (t[i] < t[i + 1]) {
          flg = -1;
        }
      } else if (flg == 1) {
        sum += d - t[i];
        if (t[i] > t[i + 1]) {
          flg = 1;
        } else {
          ans = max(ans, sum);
          sum = 0;
          flg = 0;
          i--;
        }
      } else if (flg == -1) {
        sum += d - t[i];
        if (t[i] < t[i + 1]) {
          flg = -1;
        } else {
          ans = max(ans, sum);
          sum = 0;
          flg = 0;
          i--;
        }
      }
    }

    printf("%d\\n", ans);
  }
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  vector<int> icicle(n + 2, 0);
  for (int i = 1; i <= n; ++i)
    cin >> icicle[i];

  multimap<int, int> mm;
  for (int i = 1; i <= n; ++i) {
    if (icicle[i] > icicle[i - 1] && icicle[i] > icicle[i + 1])
      mm.insert(make_pair(l - icicle[i], i));
  }

  for (;;) {
    multimap<int, int>::iterator it = mm.begin();
    int time = it->first;
    int i = it->second;
    mm.erase(it);
    icicle[i] = 0;
    if (i - 1 >= 1 && icicle[i - 1] > icicle[i - 2])
      mm.insert(make_pair(time + l - icicle[i - 1], i - 1));
    if (i + 1 <= n && icicle[i + 1] > icicle[i + 2])
      mm.insert(make_pair(time + l - icicle[i + 1], i + 1));
    if (mm.empty()) {
      cout << time << endl;
      return 0;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  vector<int> icicle(n + 2, 0);
  for (int i = 1; i <= n; ++i)
    cin >> icicle[i];

  int ret = 0;
  int rest = n;
  while (rest > 0) {
    int grow = INT_MAX;
    vector<bool> check(n + 2, false);
    for (int i = 1; i <= n; ++i) {
      if (icicle[i] > icicle[i - 1] && icicle[i] > icicle[i + 1]) {
        grow = min(grow, l - icicle[i]);
        check[i] = true;
      }
    }
    for (int i = 1; i <= n; ++i) {
      if (check[i]) {
        icicle[i] += grow;
        if (icicle[i] == l) {
          icicle[i] = 0;
          --rest;
        }
      }
    }
    ret += grow;
  }
  cout << ret << endl;

  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

#define PII pair<int, int>

priority_queue<PII, vector<PII>, greater<PII>> Q;

int main(void) {
  int N, L;
  while (scanf("%d%d", &N, &L) != EOF) {
    int a[100005] = {0};
    int temp;
    int ans = 0;

    for (int i = 1; i <= N; i++) {
      scanf("%d", &temp);
      Q.push(PII(temp, i));
    }

    while (!Q.empty()) {
      int leng = Q.top().first;
      int soej = Q.top().second;

      Q.pop();
      int time = a[soej] + L - leng;

      a[soej - 1] = max(time, a[soej - 1]);
      a[soej + 1] = max(time, a[soej + 1]);
    }

    for (int i = 1; i <= N; i++)
      ans = max(a[i], ans);

    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

#define PII pair<int, int>

priority_queue<PII, vector<PII>, greater<PII>> Q;

int main(void) {
  int N, L;
  while (scanf("%d%d", &N, &L) && N != 0) {
    int a[100005] = {0};
    int temp;
    int ans = 0;

    for (int i = 1; i <= N; i++) {
      scanf("%d", &temp);
      Q.push(PII(temp, i));
    }

    while (!Q.empty()) {
      int leng = Q.top().first;
      int soej = Q.top().second;

      Q.pop();
      int time = a[soej] + L - leng;

      a[soej - 1] = max(time, a[soej - 1]);
      a[soej + 1] = max(time, a[soej + 1]);
    }

    for (int i = 1; i <= N; i++)
      ans = max(a[i], ans);

    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;
const int mod = 1e9 + 7;
const double EPS = 1e-9;

int main() {
  int n, l;
  cin >> n >> l;
  vi v(n, 0);
  rep(i, n) cin >> v[i];
  vi t(n, 0);
  int cnt = 0;
  int tmp;
  while (1) {
    for (int i = 0; i < n; i++) {
      if (t[i] > 0)
        continue;
      if (i > 0 && v[i] <= v[i - 1])
        continue;
      if (i < n - 1 && v[i] <= v[i + 1])
        continue;
      cnt++;
      tmp = 0;
      if (i > 0)
        tmp = max(tmp, t[i - 1]);
      if (i < n - 1)
        tmp = max(tmp, t[i + 1]);
      t[i] = tmp + l - v[i];
      v[i] = 0;
      // cnt++;
      if (cnt == n)
        break;
    }
    if (cnt == n)
      break;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, t[i]);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 10000000;
const int mod = 1e9 + 7;
const double EPS = 1e-9;

int main() {
  int n, l;
  cin >> n >> l;
  vi v(n, 0);
  rep(i, n) cin >> v[i];
  int t = 0, cnt = 0;
  while (1) {
    ++t;
    for (int i = 0; i < n; i++) {
      if (i > 0 && v[i] <= v[i - 1])
        continue;
      if (i < n - 1 && v[i] <= v[i + 1])
        continue;
      ++v[i];
      if (v[i] == l) {
        v[i] = 0;
        ++cnt;
        // cout<<i<<" "<<t<<endl;
        ++i;
      }
    }
    if (cnt == n)
      break;
  }
  cout << t << endl;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

#define rep1(i, x) for (int i = 1; i <= (x); ++i)

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

int N, L;
int icicles[100002];
int memo[100002];

int rec(int i) {
  if (memo[i] >= 0)
    return memo[i];

  int res = 0;

  if (icicles[i] < icicles[i + 1])
    chmax(res, rec(i + 1));
  if (icicles[i] < icicles[i - 1])
    chmax(res, rec(i - 1));

  res += L - icicles[i];

  return memo[i] = res;
}

signed main() {
  memset(memo, -1, sizeof(memo));

  scanf("%d %d", &N, &L);

  rep1(i, N) scanf("%d", &icicles[i]);

  int ma = 0;

  rep1(i, N) chmax(ma, rec(i));

  printf("%d\\n", ma);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

#define MP make_pair

int a[100000];
int n, l;

bool is_valid(int i) {
  if (i == 0)
    return a[i] > a[i + 1];
  if (i == n - 1)
    return a[i] > a[i - 1];

  return a[i] > a[i - 1] && a[i] > a[i + 1];
}

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

  for (int i = 0; i < n; ++i)
    cin >> a[i];

  vector<pii> v;

  for (int i = 0; i < n; ++i)
    if (is_valid(i))
      v.push_back(MP(a[i], i));

  int ans = 0;

  while (v.size()) {
    sort(v.begin(), v.end());

    int diff = l - v.back().first;

    vector<pii> b;

    for (int i = v.size() - 1; i >= 0; --i) {
      int &fst = v[i].first, &scd = v[i].second;

      if (fst + diff == l) {
        a[scd] = 0;
        v.pop_back();

        if (scd - 1 >= 0 && is_valid(scd - 1))
          b.push_back(MP(a[scd - 1], scd - 1));
        if (scd + 1 < n && is_valid(scd + 1))
          b.push_back(MP(a[scd + 1], scd + 1));
      } else {
        a[scd] += diff;
        fst += diff;
      }
    }

    for (int i = 0; i < b.size(); ++i)
      v.push_back(b[i]);

    ans += diff;
  }

  cout << ans << endl;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;
const int MAX_L = 500001;

int a[MAX_N];
int k[MAX_N];
multimap<int, int> mp;
ll t[MAX_N];
int n, L;

int main() {
  scanf("%d%d", &n, &L);
  rep(i, n) {
    scanf("%d", &a[i]);
    k[i] = a[i];
    mp.emplace(a[i], i);
  }
  sort(a, a + n, greater<int>());
  int id = 0;
  while (id < n) {
    auto p = mp.equal_range(a[id]);
    auto it = p.first;
    int i = 0;
    while (it != p.second) {
      int prev = it->second;
      if (prev != 0 && prev != n - 1) {
        if (k[prev] > k[prev - 1] && k[prev] > k[prev + 1]) {
          t[prev] = L - a[id];
        } else if (k[prev] < k[prev - 1] && k[prev] > k[prev + 1]) {
          t[prev] = t[prev - 1] + L - a[id];
        } else if (k[prev] > k[prev - 1] && k[prev] < k[prev + 1]) {
          t[prev] = t[prev + 1] + L - a[id];
        } else {
          t[prev] = max(t[prev - 1], t[prev + 1]) + L - a[id];
        }
      } else if (prev == 0) {
        if (k[0] > k[1]) {
          t[0] = L - a[id];
        } else {
          t[0] = t[1] + L - a[id];
        }
      } else {
        if (k[n - 1] > k[n - 2]) {
          t[n - 1] = L - a[id];
        } else {
          t[n - 1] = t[n - 2] + L - a[id];
        }
      }
      it++;
      i++;
    }
    id += i;
  }
  printf("%lld\\n", *max_element(t, t + n));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;
const int MAX_L = 500001;

int a[MAX_N];
int k[MAX_N];
multimap<int, int> mp;
ll t[MAX_N];
int n, L;

int main() {
  scanf("%d%d", &n, &L);
  rep(i, n) {
    scanf("%d", &a[i]);
    k[i] = a[i];
    mp.emplace(a[i], i);
  }
  sort(a, a + n, greater<int>());
  int id = 0;
  while (id < n) {
    auto p = mp.equal_range(a[id]);
    auto it = p.first;
    int i = 0;
    while (i < mp.count(a[id])) {
      int prev = it->second;
      if (prev != 0 && prev != n - 1) {
        if (k[prev] > k[prev - 1] && k[prev] > k[prev + 1]) {
          t[prev] = L - a[id];
        } else if (k[prev] < k[prev - 1] && k[prev] > k[prev + 1]) {
          t[prev] = t[prev - 1] + L - a[id];
        } else if (k[prev] > k[prev - 1] && k[prev] < k[prev + 1]) {
          t[prev] = t[prev + 1] + L - a[id];
        } else {
          t[prev] = max(t[prev - 1], t[prev + 1]) + L - a[id];
        }
      } else if (prev == 0) {
        if (k[0] > k[1]) {
          t[0] = L - a[id];
        } else {
          t[0] = t[1] + L - a[id];
        }
      } else {
        if (k[n - 1] > k[n - 2]) {
          t[n - 1] = L - a[id];
        } else {
          t[n - 1] = t[n - 2] + L - a[id];
        }
      }
      it++;
      i++;
    }
    id += i;
  }
  printf("%lld\\n", *max_element(t, t + n));
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int turara[100000];
int dp[100000];
int N, L;
int rec(int idx) {
  if (~dp[idx])
    return dp[idx];
  int ret = 0;
  if (idx > 0 && turara[idx - 1] > turara[idx])
    ret = max(ret, rec(idx - 1));
  if (idx < N - 1 && turara[idx + 1] > turara[idx])
    ret = max(ret, rec(idx + 1));
  return dp[idx] = ret + (L - turara[idx]);
}

int main() {
  fill_n(dp, 100000, -1);
  scanf("%d %d", &N, &L);
  for (int i = 0; i < N; i++) {
    scanf("%d", &turara[i]);
  }
  int ret = 0;
  for (int i = 0; i < N; i++) {
    ret = max(ret, rec(i));
  }
  printf("%d\\n", ret);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int turara[100000];
int dp[100000];
int N, L;
int rec(int idx) {
  if (~dp[idx])
    return dp[idx];
  int ret = 0;
  if (idx > 0 && turara[idx - 1] > turara[idx])
    ret = max(ret, rec(idx - 1));
  if (idx < N - 1 && turara[idx + 1] > turara[idx])
    ret = max(ret, rec(idx + 1));
  return ret + (L - turara[idx]);
}

int main() {
  fill_n(dp, 100000, -1);
  scanf("%d %d", &N, &L);
  for (int i = 0; i < N; i++) {
    scanf("%d", &turara[i]);
  }
  int ret = 0;
  for (int i = 0; i < N; i++) {
    ret = max(ret, rec(i));
  }
  printf("%d\\n", ret);
}
Problem p00474 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 100005
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll n, l, a[N], d[N], ans;
priority_queue<P> q;

int main() {
  cin >> n >> l;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    q.push(P(a[i], i));
  }
  while (!q.empty()) {
    P t = q.top();
    q.pop();
    ll len = t.first, idx = t.second;
    d[idx] = max(d[idx - 1], d[idx + 1]) + l - len;
    ans = max(ans, d[idx]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 505
using namespace std;
typedef pair<int, int> P;
int n, l, a[N], d[N], ans;
priority_queue<P> q;

int main() {
  cin >> n >> l;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    q.push(P(a[i], i));
  }
  while (!q.empty()) {
    P t = q.top();
    q.pop();
    int len = t.first, idx = t.second;
    d[idx] = max(d[idx - 1], d[idx + 1]) + l - len;
    ans = max(ans, d[idx]);
  }
  cout << ans << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <cstring>
#include <iostream>

using namespace std;

int main() {
  int n, lim, i, remain;
  int trr[100002];
  int falltime[100002];

  memset(trr, 0, sizeof(trr));
  memset(falltime, -1, sizeof(falltime));
  cin >> n >> lim;

  for (i = 1; i <= n; i++) {
    cin >> trr[i];
  }

  remain = n;

  for (i = 1; i <= n; i++)
    if (trr[i] > trr[i - 1] && trr[i] > trr[i + 1]) {
      falltime[i] = lim - trr[i];
      remain--;
    }

  while (remain != 0) {
    for (i = 1; i <= n; i++) {
      int left = 0, right = 0;
      if (falltime[i] == -1) {
        if (trr[i - 1] > trr[i])
          if (falltime[i - 1] != -1)
            left = falltime[i - 1];
          else
            continue;

        if (trr[i + 1] > trr[i])
          if (falltime[i + 1] != -1)
            right = falltime[i + 1];
          else
            continue;

        if (left || right) {
          falltime[i] = lim - trr[i] + max(left, right);
          remain--;
        }
      }
    }
  }

  int out = 0;
  for (i = 1; i <= n; i++) {
    out = max(out, falltime[i]);
  }

  cout << out << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <fstream>
#include <iostream>

using namespace std;

int main() {
  int n, lim, i, remain;
  int trr[100002];
  int falltime[100002];

  while (!cin.eof()) {
    memset(trr, 0, sizeof(trr));
    memset(falltime, -1, sizeof(falltime));
    cin >> n >> lim;

    for (i = 1; i <= n; i++) {
      cin >> trr[i];
    }

    remain = n;

    for (i = 1; i <= n; i++)
      if (trr[i] > trr[i - 1] && trr[i] > trr[i + 1]) {
        falltime[i] = lim - trr[i];
        remain--;
      }

    while (remain != 0) {
      for (i = 1; i <= n; i++) {
        int left = 0, right = 0;
        if (falltime[i] == -1) {
          if (trr[i - 1] > trr[i])
            if (falltime[i - 1] != -1)
              left = falltime[i - 1];
            else
              continue;

          if (trr[i + 1] > trr[i])
            if (falltime[i + 1] != -1)
              right = falltime[i + 1];
            else
              continue;

          if (left || right) {
            falltime[i] = lim - trr[i] + max(left, right);
            remain--;
          }
        }
      }
    }

    int out = 0;
    for (i = 1; i <= n; i++) {
      out = max(out, falltime[i]);
    }

    cout << out << endl;
  }

  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;

int main() {
  int n, l, *ice, buf, prog, over, h = 0, sz;
  queue<int> q;

  cin >> n >> l;

  ice = new int[n + 2];
  ice[0] = ice[n + 1] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> ice[i];
  }

  for (int i = 1; i < n + 1; i++) {
    if (ice[i]) {
      if ((ice[i] > ice[i - 1]) && (ice[i] > ice[i + 1])) {
        q.push(i);
      }
    }
  }

  while (1) {
    buf = q.front();
    prog = (l - ice[buf]) > 0 ? (l - ice[buf]) : 0;

    sz = q.size();
    for (int i = 0; i < sz; i++) {
      buf = q.front();
      q.pop();

      ice[buf] += prog;
      over = (ice[buf] - l) >= 0 ? (ice[buf] - l) : -1;
      if (over != -1) {
        ice[buf] = 0;
        if (ice[buf - 1] > ice[buf - 2]) {
          ice[buf - 1] += over;
          q.push(buf - 1);
        }
        if (ice[buf + 1] > ice[buf + 2]) {
          ice[buf + 1] += over;
          q.push(buf + 1);
        }
      } else {
        q.push(buf);
      }
    }
    h += prog;

    if (q.empty()) {
      break;
    }
  }

  cout << h << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

int main() {
  int n, l, *ice, buf, h = 0, sz;
  queue<int> q;

  cin >> n >> l;

  ice = new int[n + 2];
  ice[0] = ice[n + 1] = 0;
  for (int i = 1; i < n + 1; i++) {
    cin >> ice[i];
  }

  for (int i = 1; i < n + 1; i++) {
    if (ice[i]) {
      if ((ice[i] > ice[i - 1]) && (ice[i] > ice[i + 1])) {
        q.push(i);
      }
    }
  }

  while (1) {
    sz = q.size();
    for (int i = 0; i < sz; i++) {
      buf = q.front();
      q.pop();

      ice[buf]++;
      if (ice[buf] == l) {
        ice[buf] = 0;
        if (ice[buf - 1] > ice[buf - 2]) {
          q.push(buf - 1);
        }
        if (ice[buf + 1] > ice[buf + 2]) {
          q.push(buf + 1);
        }
      } else {
        q.push(buf);
      }
    }
    h++;

    if (q.empty()) {
      break;
    }
  }

  cout << h << endl;

  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int high[100000];
int main() {
  queue<P> que;
  int n, l;
  scanf("%d%d", &n, &l);
  rep(i, n) scanf("%d", &high[i]);
  rep(i, n) {
    if ((!i || high[i - 1] < high[i]) && (i == n - 1 || high[i + 1] < high[i]))
      que.push(P(i, l - high[i]));
  }
  int Max = 0;
  while (!que.empty()) {
    queue<P> k;
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      int i = p.first;
      Max = max(Max, p.second);
      high[i] = 0;
      if (i > 0)
        k.push(P(i - 1, p.second));
      if (i < n - 1)
        k.push(P(i + 1, p.second));
    }
    while (!k.empty()) {
      P p = k.front();
      k.pop();
      int i = p.first;
      if ((!i || high[i - 1] < high[i]) &&
          (i == n - 1 || high[i + 1] < high[i]))
        que.push(P(i, p.second + l - high[i]));
    }
  }
  printf("%d\\n", Max);
}

Error: TLE

Fixed submission:

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

int high[100000];
int main() {
  queue<int> que;
  int n, l;
  scanf("%d%d", &n, &l);
  rep(i, n) scanf("%d", &high[i]);
  rep(i, n) {
    if ((!i || high[i - 1] < high[i]) && (i == n - 1 || high[i + 1] < high[i]))
      que.push(i);
  }
  int cnt = 0;
  while (1) {
    bool ok = true;
    rep(i, n) if (high[i]) ok = false;
    if (ok)
      break;
    queue<int> k;
    int size = que.size();
    rep(j, size) {
      int i = que.front();
      que.pop();
      high[i]++;
      if (high[i] >= l) {
        high[i] = 0;
        if (i)
          k.push(i - 1);
        if (i < n - 1)
          k.push(i + 1);
      } else
        que.push(i);
    }
    while (!k.empty()) {
      int i = k.front();
      k.pop();
      if ((!i || high[i - 1] < high[i]) &&
          (i == n - 1 || high[i + 1] < high[i]))
        que.push(i);
    }
    cnt++;
  }
  printf("%d\\n", cnt);
}