alexjercan/bugnet huggingface dataset:
Problem p00474 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define fir first
#define sec second

vector<pair<int, int>> vi;
int dp[100001];

int main(void) {
  int n, L, tmp;
  cin >> n >> L;

  for (int i = 0; i < n; i++) {
    cin >> tmp;
    vi.push_back(make_pair(tmp, i));
  }
  sort(vi.begin(), vi.end());

  for (int i = 0; i < vi.size(); i++) {
    dp[vi[i].sec + 1] =
        max(dp[vi[i].second], dp[vi[i].second + 2]) + (L - vi[i].fir);
  }

  int res = 0;
  for (int i = 0; i <= n; i++) {
    res = max(res, dp[i]);
  }
  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define fir first
#define sec second

vector<pair<int, int>> vi;
int dp[50500];

int main(void) {
  int n, L, tmp;
  cin >> n >> L;

  for (int i = 0; i < n; i++) {
    cin >> tmp;
    vi.push_back(make_pair(tmp, i));
  }
  sort(vi.begin(), vi.end());

  for (int i = 0; i < vi.size(); i++) {
    if (vi[i].sec != 0 && vi[i].sec != n - 2) {
      dp[vi[i].sec] =
          max(dp[vi[i].sec - 1], dp[vi[i].sec + 1]) + (L - vi[i].fir);
      // cout << vi[i].sec - 1 << " " << vi[i].sec + 1 << endl;
    } else if (vi[i].sec == 0) {
      dp[vi[i].sec] = dp[vi[i].sec + 1] + (L - vi[i].fir);
      // cout << vi[i].sec + 1 << endl;
    } else if (vi[i].sec + 1 == n - 1) {
      dp[vi[i].sec] = dp[vi[i].sec - 1] + (L - vi[i].fir);
      // cout << vi[i].sec - 1 << endl;
    }
  }

  int res = 0;
  for (int i = 0; i < n; i++) {
    res = max(res, dp[i]);
  }
  cout << res << endl;

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, l;

  scanf("%d", &n);
  scanf("%d", &l);

  vector<int> a(n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  vector<pair<int, int>> b;

  for (int i = 0; i < n; i++) {
    b.push_back(make_pair(a[i], i));
  }

  sort(b.begin(), b.end(), greater<pair<int, int>>());

  vector<long long> d(n, 0);

  for (int i = 0; i < n; i++) {
    int ptr = b[i].second;

    bool lc = true, rc = true;

    if (ptr != 0) {
      lc = (a[ptr] > a[ptr - 1]);
    }

    if (ptr + 1 != n) {
      rc = (a[ptr] > a[ptr + 1]);
    }

    if (lc && rc) {
      d[ptr] = l - a[ptr];
    }

    if (lc && !rc) {
      d[ptr] = d[ptr + 1] + l - a[ptr];
    }

    if (!lc && rc) {
      d[ptr] = d[ptr - 1] + l - a[ptr];
    }

    if (!lc && !rc) {
      d[ptr] = max(d[ptr + 1], d[ptr - 1]) + l - a[ptr];
    }
  }

  long long ret = 0;

  for (int i = 0; i < n; i++) {
    ret = max(ret, d[i]);
  }

  printf("%lld\\n", ret);

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, l;

  scanf("%d", &n);
  scanf("%d", &l);

  vector<int> a(n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  long long ret = 0;

  while (true) {
    bool ok = true;

    for (int i = 0; i < n; i++) {
      if (a[i] != 0) {
        ok = false;
      }
    }

    if (ok) {
      break;
    }

    int max_ = 0;

    for (int i = 0; i < n; i++) {
      bool ok2 = true;

      if (i + 1 != n) {
        ok2 = ok2 & (a[i] > a[i + 1]);
      }

      if (i != 0) {
        ok2 = ok2 & (a[i] > a[i - 1]);
      }

      if (ok2) {
        max_ = max(max_, a[i]);
      }
    }

    ret += (l - max_);

    for (int i = 0; i < n; i++) {
      bool ok2 = true;

      if (i + 1 != n) {
        ok2 = ok2 & (a[i] > a[i + 1]);
      }

      if (i != 0) {
        ok2 = ok2 & (a[i] > a[i - 1]);
      }

      if (ok2) {
        a[i] += (l - max_);
      }
    }

    for (int i = 0; i < n; i++) {
      if (a[i] == l) {
        a[i] = 0;
      }
    }
  }

  printf("%lld\\n", ret);

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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int main() {
  int n, l;
  cin >> n >> l;
  int icic[114514];
  icic[0] = 0;
  icic[1] = 0;
  for (int i = 0; i < n; i++)
    cin >> icic[i + 2];
  icic[n + 2] = 0;
  icic[n + 3] = 0;
  int t = 0;

  priority_queue<P> Q;
  for (int i = 1; i <= n + 2; i++) {
    if (icic[i] > icic[i - 1] && icic[i] > icic[i + 1]) {
      Q.push(P(icic[i], i));
    }
  }

  while (Q.size()) {
    P p = Q.top();
    Q.pop();

    if (p.first == 0)
      continue;
    t += (l - p.first - t);

    icic[p.second] = 0;
    if (icic[p.second - 2] < icic[p.second - 1])
      Q.push(P(icic[p.second - 1] - t, p.second - 1));
    if (icic[p.second + 2] < icic[p.second + 1])
      Q.push(P(icic[p.second + 1] - t, p.second + 1));
  }
  cout << t << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int main() {
  int n, l;
  cin >> n >> l;
  int icic[114514];
  icic[0] = 0;
  icic[1] = 0;
  for (int i = 0; i < n; i++)
    cin >> icic[i + 2];
  icic[n + 2] = 0;
  icic[n + 3] = 0;
  int t = 0;

  priority_queue<P> Q;
  for (int i = 1; i <= n; i++) {
    if (icic[i] > icic[i - 1] && icic[i] > icic[i + 1]) {
      Q.push(P(icic[i], i));
    }
  }

  while (Q.size()) {
    P p = Q.top();
    Q.pop();

    if (p.first == 0)
      continue;
    if (p.first + t < l) {
      t += (l - p.first - t);
    }

    if (icic[p.second - 2] < icic[p.second - 1])
      Q.push(P(icic[p.second - 1], p.second - 1));
    if (icic[p.second + 2] < icic[p.second + 1])
      Q.push(P(icic[p.second + 1], p.second + 1));
  }
  cout << t << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int ic[100002], icL[100002];
int maxer = 0;
int N, L;
void pick(int i) {
  if (icL[i - 1] < icL[i] && icL[i + 1] < icL[i]) {
    ic[i] = max(ic[i - 1], ic[i + 1]) + L - icL[i];
    maxer = max(ic[i], maxer);
    icL[i] = 0;
    pick(i + 1);
    pick(i - 1);
  }
}
int main() {

  cin >> N >> L;
  icL[0] = 0;
  icL[N + 1] = 0;
  for (int i = 1; i <= N; i++)
    cin >> icL[i];
  for (int i = 1; i <= N; i++) {
    pick(i);
  }
  cout << maxer << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int ic[50002], icL[50002];
int maxer = 0;
int N, L;
void pick(int i) {
  if (icL[i - 1] < icL[i] && icL[i + 1] < icL[i]) {
    ic[i] = max(ic[i - 1], ic[i + 1]) + L - icL[i];
    maxer = max(ic[i], maxer);
    icL[i] = 0;
    pick(i + 1);
    pick(i - 1);
  }
}
int main() {

  cin >> N >> L;
  icL[0] = 0;
  icL[N + 1] = 0;
  for (int i = 1; i <= N; i++)
    cin >> icL[i];
  for (int i = 1; i <= N; i++) {
    pick(i);
  }
  cout << maxer << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
vector<long> p;
long i;
long n, m;
long a;
long f;
long ice[100002], tim[100002];
int main() {
  cin >> n >> m;
  ice[0] = ice[n + 1] = 0;
  cin >> ice[1];
  for (i = 2; i <= n; i++) {
    cin >> ice[i];
    if (ice[i - 2] < ice[i - 1] && ice[i - 1] > ice[i]) {
      p.push_back(i - 1);
    }
  }
  // cout <<"AA";
  if (ice[n - 1] < ice[n] && ice[n] > ice[n + 1]) {
    p.push_back(n);
  }
  while (!p.empty()) {
    // cout << "BB" ;
    a = p[p.size() - 1];
    p.pop_back();
    if (ice[a - 1] < ice[a] && ice[a] > ice[a + 1]) {
      tim[a] = m - ice[a];
      if (ice[a - 1] != 0)
        p.push_back(a - 1);
      if (ice[a + 1] != 0)
        p.push_back(a + 1);
    } else if (ice[a - 1] < ice[a] || ice[a] > ice[a + 1]) {
      if (ice[a - 1] > ice[a]) {
        f = tim[a - 1];
        if (ice[a + 1] != 0)
          p.push_back(a + 1);
      }
      if (ice[a + 1] > ice[a]) {
        f = tim[a + 1];
        if (ice[a - 1])
          p.push_back(a - 1);
      }
      tim[a] = m - ice[a] + f;
    } else {
      if (tim[a - 1] > tim[a + 1]) {
        tim[a] = m - ice[a] + tim[a - 1];
      }
      if (tim[a - 1] < tim[a + 1]) {
        tim[a] = m - ice[a] + tim[a + 1];
      }
    }
  }
  // cout << "CC";
  long mx = 0;
  for (i = 1; i <= n; i++) {
    if (mx < tim[i])
      mx = tim[i];
  }
  cout << mx << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream> // JOI 2009 ???3
#include <vector>
using namespace std;
vector<long> t;
long cnt;
long n, m;
long i, j;
long a, f;
int main() {
  cin >> n >> m;
  t.push_back(0);
  for (i = 0; i < n; i++) {
    cin >> a;
    t.push_back(a);
  }
  t.push_back(0);
  cnt = 0;
  while (1) {
    f = 0;
    for (i = 1; i <= n; i++) {
      if (t[i - 1] < t[i] && t[i] > t[i + 1]) {
        t[i]++;
        if (t[i] == m) {
          t[i] = 0;
          if (i < n && t[i + 1] > t[i + 2])
            i++;
        }
      } else if ((t[i - 1] != t[i] || t[i] != t[i + 1]) && !f)
        f = 1;
    }
    if (f == 0)
      break;
    cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t[100010];
int main() {
  int n, l;
  cin >> n >> l;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  t[0] = l - a[0];
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i])
      t[i] = t[i - 1] + (l - a[i]);
    else
      t[i] = l - a[i];
  }
  for (int i = n - 2; i >= 0; i--) {
    if (a[i] < a[i + 1])
      t[i] = max(t[i], t[i + 1] + (l - a[i]));
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, t[i]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t[510];
int main() {
  int n, l;
  cin >> n >> l;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  t[0] = l - a[0];
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i])
      t[i] = t[i - 1] + (l - a[i]);
    else
      t[i] = l - a[i];
  }
  for (int i = n - 2; i >= 0; i--) {
    if (a[i] < a[i + 1])
      t[i] = max(t[i], t[i + 1] + (l - a[i]));
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans = max(ans, t[i]);
  cout << ans << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int N, L;

  cin >> N >> L;

  vector<int> icicle(N);

  for (int i = 0; i < N; i++)
    cin >> icicle[i];

  // ツ堕敖嘉陳づ可堕ォツつオツ堕アツつッツづゥ
  ll res = 0, buf = L - icicle[0];
  for (int i = 1; i < N; i++) {
    if (icicle[i - 1] >= icicle[i]) {
      res = max(res, buf);
      buf = 0;
    }

    buf += L - icicle[i];

    if (i == N - 1) {
      if (icicle[i - 1] >= icicle[i])
        res = max(res, (ll)icicle[i]);
      else
        res = max(res, buf);
    }
  }

  // ツ個クツ渉ュツ陳づ可堕ォツつオツ堕アツつッツづゥ
  buf = L - icicle[0];
  for (int i = 1; i < N; i++) {
    if (icicle[i - 1] <= icicle[i]) {
      res = max(res, buf);
      buf = 0;
    }

    buf += L - icicle[i];

    if (i == N - 1) {
      if (icicle[i - 1] <= icicle[i])
        res = max(res, (ll)icicle[i]);
      else
        res = max(res, buf);
    }
  }

  cout << res << endl;
  // cin.sync(); cin.get();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int N, L;

  cin >> N >> L;

  vector<int> icicle(N + 2);

  for (int i = 1; i <= N; i++)
    cin >> icicle[i];
  icicle[0] = icicle[N + 1] = 0;

  ll res = 0;
  while (1) {
    vector<int> number;
    int Tunit = L;

    for (int i = 1; i <= N; i++) {
      if (icicle[i - 1] < icicle[i] && icicle[i + 1] < icicle[i]) {
        number.push_back(i);
        Tunit = min(Tunit, L - icicle[i]);
      }
    }

    if (Tunit == L)
      break;

    for (int i = 0; i < number.size(); i++) {
      if (icicle[number[i]] == L - Tunit)
        icicle[number[i]] = 0;
      else
        icicle[number[i]] += Tunit;
    }

    res += Tunit;
  }

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

#include <algorithm>
#include <iostream>
#include <list>
#define F first
#define S second
using namespace std;

typedef pair<int, int> P;

P L[100000];
long long int dp[100001] = {0};
int main(void) {
  long long int time = 0;
  int n, l, tmp;
  cin >> n >> l;
  for (int i = 0; i < n; i++)
    cin >> tmp, L[i].F = l - tmp, L[i].S = i;
  sort(L, L + n);
  for (int i = 0; i < n; i++) {
    dp[L[i].S + 1] = L[i].F + max(dp[L[i].S], dp[L[i].S + 2]);
    time = max(time, dp[L[i].S + 1]);
  }
  cout << time << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 10e6
using namespace std;

bool S[100000] = {false};
int L[100000] = {0};
int main(void) {
  long long int time = 0;
  int n, l;
  cin >> n >> l;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    L[i] = l - tmp;
  }
  int sum = 0;
  while (sum != n) {
    for (int i = 0; i < n; i++) {
      if (!S[i]) {
        if (!i || L[i - 1] == 0) {
          if (L[i] < L[i + 1] || L[i + 1] == 0) {
            S[i] = true;
            sum++;
          }
        } else if (i == n - 1 || L[i + 1] == 0) {
          if (L[i] < L[i - 1] || L[i - 1] == 0) {
            S[i] = true;
            sum++;
          }
        } else {
          if ((L[i] < L[i - 1] || L[i - 1] == 0) &&
              (L[i] < L[i + 1] || L[i + 1] == 0)) {
            S[i] = true;
            sum++;
          }
        }
      }
    }
    int MIN = INF;
    for (int i = 0; i < n; i++) {
      if (S[i] && L[i] > 0)
        MIN = min(MIN, L[i]);
    }
    time += MIN;
    for (int i = 0; i < n; i++)
      if (S[i] && L[i] > 0)
        L[i] -= MIN;
  }
  int MAX = 0;
  for (int i = 0; i < n; i++)
    MAX = max(MAX, L[i]);
  time += MAX;
  cout << time << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int a[100002], m, n;
  long long b[100002], mx = -1;
  a[0] = 0;
  a[100001] = 0;
  cin >> n >> m;
  for (i = 1; i < n + 1; i++)
    cin >> a[i];
  for (i = 1; i < n + 1; i = j) {
    for (; i < n + 1 && (a[i - 1] > a[i] || a[i + 1] > a[i]); i++)
      ;
    if (i != n + 1) {
      b[i] = m - a[i];
      for (j = i - 1; j > 0; j--) {
        if (a[j - 1] > a[j]) {
          b[j] = m - a[j] + (b[j - 1] > b[j + 1] ? b[j - 1] : b[j + 1]);
          break;
        } else
          b[j] = m - a[j] + b[j + 1];
      }
      for (j = i + 1; j < n + 1 && a[j - 1] > a[j]; j++)
        b[j] = m - a[j] + b[j - 1];
    } else
      j = i;
  }
  for (i = 1; i < n + 1; i++) {
    if (mx < b[i])
      mx = b[i];
  }
  cout << mx << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int a[100002], m, n;
  long long b[100002], mx = -1;
  a[0] = 0;
  a[100001] = 0;
  cin >> n >> m;
  for (i = 1; i < n + 1; i++)
    cin >> a[i];
  for (i = 1; i < n + 1; i = j) {
    for (; i < n + 1 && (a[i - 1] > a[i] || a[i + 1] > a[i]); i++)
      ;
    if (i != n + 1) {
      b[i] = m - a[i];
      for (j = i - 1; j > 0; j--) {
        if (a[j - 1] > a[j]) {
          b[j] = m - a[j] + (b[j - 1] > b[j + 1] ? b[j - 1] : b[j + 1]);
          break;
        } else
          b[j] = m - a[j] + b[j + 1];
      }
      for (j = i + 1; j < n + 1 && a[j - 1] > a[j]; j++)
        b[j] = m - a[j] + b[j - 1];
    }
  }
  for (i = 1; i < n + 1; i++) {
    if (mx < b[i])
      mx = b[i];
  }
  cout << mx << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PA;
int main() {
  int n, l;
  int a[100002];
  priority_queue<PA, vector<PA>, greater<PA>> pq;
  cin >> n >> l;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (a[i] > a[i - 1] && a[i] > a[i + 1])
      pq.push(PA(l - a[i], i));
  }
  int keika = 0, ans = 0;
  PA p;
  while (!pq.empty()) {
    p = pq.top();
    pq.pop();
    ans = max(ans, p.first);
    a[p.second] = 0;
    if (a[p.second - 1] > a[p.second - 2]) {
      pq.push(PA(l - a[p.second - 1] + p.first, p.second - 1));
    }
    if (a[p.second + 1] > a[p.second + 2]) {
      pq.push(PA(l - a[p.second + 1] + p.first, p.second + 1));
    }
  }
  cout << ans << endl;
  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, l;
  int a[100002] = {};
  queue<int> que;
  cin >> n >> l;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (a[i] > a[i - 1] && a[i] > a[i + 1])
      que.push(i);
  }
  int tura, ans = 0;
  while (!que.empty()) {
    ans++;
    while (!que.empty()) {
      tura = que.front();
      que.pop();
      a[tura] += 1;
      if (a[tura] >= l) {
        a[tura] = 0;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] > a[i - 1] && a[i] > a[i + 1])
        que.push(i);
    }
  }
  cout << ans << endl;
  return (0);
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int a[100010] = {0};

int main(void) {
  int N, L, num = 0;
  ll sum = 0, Max = 0;
  cin >> N >> L;

  cin >> a[1];
  for (int i = 1; i <= N; ++i) {
    if (i < N)
      cin >> a[i + 1];
    num++;
    sum += a[i];
    if ((a[i] - a[i - 1] < 0 && a[i + 1] - a[i] >= 0) ||
        (a[i] - a[i - 1] > 0 && a[i + 1] - a[i] <= 0) || i == N) {
      Max = max(Max, (ll)num * L - sum);
      num = 1;
      sum = a[i];
    }
  }

  cout << Max << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int a[100010] = {0};

int main(void) {
  int N, L, num = 0;
  int sum = 0, Max = 0;
  cin >> N >> L;

  cin >> a[1];
  for (int i = 1; i <= N; ++i) {
    if (i < N)
      cin >> a[i + 1];
    num++;
    sum += a[i];
    if ((a[i] - a[i - 1]) * (a[i + 1] - a[i]) <= 0 || i == N) {
      Max = max(Max, num * L - sum);
      num = 1;
      sum = a[i];
    }
  }

  cout << Max << endl;
  while (1)
    ;

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

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 1e5 + 1;

int T[MAX];
int N, L;
vector<int> V;

int rec(int n) {
  // cout << "n " << n << endl;
  if (T[n] != -1)
    return T[n];
  int tmp = 0;
  if (0 <= n - 1 && V[n - 1] > V[n])
    tmp = max(tmp, rec(n - 1));
  if (n + 1 < N && V[n] < V[n + 1])
    tmp = max(tmp, rec(n + 1));
  tmp += L - V[n];
  return T[n] = tmp;
}

int main() {

  cin >> N >> L;
  V.resize(N);

  for (int i = 0; i < N; i++)
    cin >> V[i];
  memset(T, -1, sizeof(T));

  int ans = 0;
  for (int i = 0; i < N; i++)
    ans = max(ans, rec(i));
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

typedef pair<int, int> P;
vector<P> V, Q, NEW;
int N, L;

int getmax() {
  int out = 0;
  for (int i = 0; i < Q.size(); i++)
    out = max(out, Q[i].first);

  return out;
}

bool isUp(int n) {
  if (V[n].second == 1)
    return false;

  return (V[n - 1] < V[n] && V[n + 1] < V[n]);
}
int main() {

  cin >> N >> L;

  V.push_back(P(0, 1));
  for (int i = 0; i < N; i++) {
    int in;
    cin >> in;
    V.push_back(P(in, 0));
  }
  V.push_back(P(0, 1));

  for (int i = 1; i < V.size() - 1; i++)
    if (V[i - 1] < V[i] && V[i + 1] < V[i]) {
      Q.push_back(P(V[i].first, i));
      V[i].second = 1;
      i++;
    }
  int finnum = 0;
  int ans = 0;

  while (1) {

    /* for(int i = 1; i < V.size()-1; i++)
     cout << V[i].first << " ";
    cout << ans << endl;
    //cout << endl;
    */

    if (finnum == N)
      break;

    int maxpos = getmax();
    int add = L - maxpos;
    ans += add;

    for (int i = 0; i < Q.size(); i++) {

      if (Q[i].first == 0)
        continue;

      if (Q[i].first != maxpos) {
        V[Q[i].second].first += add;
        Q[i].first += add;
      } else {
        int fin = Q[i].second;
        finnum++;
        V[fin].second = 1;
        V[fin].first = 0;

        Q[i].first = 0;

        if (isUp(fin - 1)) {
          NEW.push_back(P(V[fin - 1].first, fin - 1));
          V[fin - 1].second = 1;
        }
        if (isUp(fin + 1)) {
          NEW.push_back(P(V[fin + 1].first, fin + 1));
          V[fin + 1].second = 1;
        }
      }
    }
    for (int i = 0; i < NEW.size(); i++)
      Q.push_back(NEW[i]);

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

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

#define INF 10000000000

int main() {
  long long int n, l;
  cin >> n >> l;
  vector<long long int> d(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  vector<long long int> t(n, INF);
  while (true) {
    bool h = true;
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        if (d[i] > d[i + 1]) {
          t[i] = (l - d[i]);
        } else {
          t[i] = (l - d[i]) + t[i + 1];
        }
      } else if (i == n - 1) {
        if (d[i] > d[i - 1]) {
          t[i] = (l - d[i]);
        } else {
          t[i] = (l - d[i]) + t[i - 1];
        }
      } else {
        if (d[i] > d[i + 1] && d[i] > d[i - 1]) {
          t[i] = (l - d[i]);
        } else if (d[i] > d[i + 1]) {
          t[i] = (l - d[i]) + t[i - 1];
        } else if (d[i] > d[i - 1]) {
          t[i] = (l - d[i]) + t[i + 1];
        } else {
          t[i] = (l - d[i]) + max(t[i + 1], t[i - 1]);
        }
      }
      if (t[i] >= INF) {
        h = false;
      }
    }
    if (h) {
      long long int tt = 0;
      for (int i = 0; i < n; i++) {
        tt = max(tt, t[i]);
      }
      cout << tt << endl;
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long int n, l;
  cin >> n >> l;
  vector<long long int> d(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  long long int t = 0;
  while (true) {
    bool h = true;
    long long int MIN = 1000000;
    vector<int> dd;
    for (int i = 0; i < n; i++) {
      if (d[i] != 0) {
        h = false;
      }
      if (i == 0) {
        if (d[i] > d[i + 1]) {
          MIN = min(MIN, l - d[i]);
          dd.push_back(i);
        }
      } else if (i == n - 1) {
        if (d[i] > d[i - 1]) {
          MIN = min(MIN, l - d[i]);
          dd.push_back(i);
        }
      } else {
        if (d[i] > d[i + 1] && d[i] > d[i - 1]) {
          MIN = min(MIN, l - d[i]);
          dd.push_back(i);
        }
      }
    }
    if (h) {
      break;
    }
    t += MIN;
    for (int i = 0; i < dd.size(); i++) {
      d[dd[i]] += MIN;
      if (d[dd[i]] == l) {
        d[dd[i]] = 0;
      }
    }
  }
  cout << t << endl;
  return 0;
}