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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#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;
typedef long long LL;
typedef pair<int, int> P;

int N;
int t[10000 + 10];
int dp[2][10000 + 10];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N - 1; i++) {
    scanf("%d", t + i);
  }
  auto cur = dp[0];
  auto nex = dp[1];
  cur[1] = t[0];
  cur[0] = 1 << 30;
  nex[0] = 1 << 30;
  for (int i = 1; i < N; i++) {
    // cout << i+1 << endl;
    for (int j = 1; j <= i + 1; j++) {
      nex[j] = min(cur[j - 1] - t[i - 1] + t[i], cur[i - j + 1] + t[i]);
      // cout << j << ": " << nex[j] << endl;
    }
    swap(cur, nex);
  }
  printf("%d\\n", cur[N / 2]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#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;
typedef long long LL;
typedef pair<int, int> P;

int N;
int t[10000 + 10];
int dp[2][10000 + 10];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N - 1; i++) {
    scanf("%d", t + i);
  }
  auto cur = dp[0];
  auto nex = dp[1];
  cur[1] = t[0];
  cur[0] = 1 << 30;
  nex[0] = 1 << 30;
  for (int i = 1; i < N; i++) {
    cout << i + 1 << endl;
    for (int j = 1; j <= i + 1; j++) {
      nex[j] = min(cur[j - 1] - t[i - 1] + t[i], cur[i - j + 1] + t[i]);
      cout << j << ": " << nex[j] << endl;
    }
    swap(cur, nex);
  }
  printf("%d\\n", cur[N / 2]);
}
Problem p00473 (C++) — buggy submission, verdict: Memory Limit Exceeded


#include <algorithm>
#include <stdio.h>
#include <string.h>

int n;
int a[9999];
int dp[5001][2];

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i)
    scanf("%d", a + i - 1);
  memset(dp, 0x7F, sizeof(dp));
  dp[0][0] = dp[0][1] = 0;
  for (int j = 0; j <= n / 2; ++j)
    dp[j][1] = 0;
  for (int i = 1; i <= n / 2; ++i) {
    dp[0][0] = 0;
    dp[0][1] = 123456789;
    for (int j = 1; j <= n / 2; ++j) {
      dp[j][0] = std::min(dp[j][0], dp[j][1] + a[i + j - 2]);
      dp[j][1] = std::min(dp[j - 1][0] + a[i + j - 2], dp[j - 1][1]);
    }
  }
  printf("%d\\n", std::min(dp[n / 2][0], dp[n / 2][1]));
  return 0;
}

Error: MLE

Fixed submission:


#include <algorithm>
#include <stdio.h>
#include <string.h>

int n;
int a[9999];
int dp[5001][5001][2];

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i)
    scanf("%d", a + i - 1);
  memset(dp, 0x7F, sizeof(dp));
  dp[0][0][0] = dp[0][0][1] = 0;
  for (int i = 0; i <= n / 2; ++i)
    dp[i][0][0] = 0;
  for (int j = 0; j <= n / 2; ++j)
    dp[0][j][1] = 0;
  for (int i = 1; i <= n / 2; ++i) {
    for (int j = 1; j <= n / 2; ++j) {
      dp[i][j][0] = std::min(dp[i - 1][j][0], dp[i - 1][j][1] + a[i + j - 2]);
      dp[i][j][1] = std::min(dp[i][j - 1][0] + a[i + j - 2], dp[i][j - 1][1]);
    }
  }
  printf("%d\\n", std::min(dp[n / 2][n / 2][0], dp[n / 2][n / 2][1]));
  return 0;
}
Problem p00473 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n;
int t[10005];
int ans;
int main() {
  cin >> n;
  int i, j, k;
  for (i = 0; i < n - 1; i++)
    cin >> t[i];
  ans = t[n / 2 - 1];
  for (i = 0; i < n / 2 - 1; i++)
    ans = min(ans, t[i] + t[i + n / 2]);

  int dp[2][2][5005];
  int inf = 1 << 28;
  fill(dp[0][0], dp[0][0] + 5005, inf);
  fill(dp[0][1], dp[0][1] + 5005, inf);
  fill(dp[1][0], dp[1][0] + 5005, inf);
  fill(dp[1][1], dp[1][1] + 5005, inf);
  dp[0][0][1] = 0;
  dp[0][1][0] = 0;

  for (i = 0; i < n - 1; i++) {
    for (j = 0; j <= n / 2; j++) {
      dp[(i + 1) % 2][0][j + 1] =
          min(dp[(i + 1) % 2][0][j + 1], dp[i % 2][0][j]);
      dp[(i + 1) % 2][0][j + 1] =
          min(dp[(i + 1) % 2][0][j + 1], dp[i % 2][1][j] + t[i]);
      dp[(i + 1) % 2][1][j] = min(dp[(i + 1) % 2][1][j], dp[i % 2][1][j]);
      dp[(i + 1) % 2][1][j] =
          min(dp[(i + 1) % 2][1][j], dp[i % 2][0][j] + t[i]);
    }
    fill(dp[i % 2][0], dp[i % 2][0] + 5005, inf);
    fill(dp[i % 2][1], dp[i % 2][1] + 5005, inf);
  }

  cout << min(dp[i % 2][0][n / 2], dp[i % 2][1][n / 2]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
int t[10005];
int ans;
void dfs(int p, int x, int v, int l) {
  if (v >= ans || x > n / 2 || l > 2)
    return;
  if (p == n) {
    if (x == n / 2)
      ans = v;
    return;
  }
  for (int i = n - p; i > 0; i--) {
    if (ans <= v + t[p + i - 1])
      continue;
    dfs(p + i, x + i, v + t[p + i - 1], l + 1);
    dfs(p + i, x, v + t[p + i - 1], l + 1);
  }
}
int main() {
  cin >> n;
  int i, j, k;
  for (i = 0; i < n - 1; i++)
    cin >> t[i];
  ans = t[n / 2 - 1];
  for (i = 0; i < n / 2 - 1; i++)
    ans = min(ans, t[i] + t[i + n / 2]);
  dfs(0, 0, 0, 0);
  cout << ans << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int n, l;
int a[100010];
int aa[100010];

void f(int i) {
  int k = 0;
  if (i > 0 && a[i] < a[i - 1]) {
    if (aa[i - 1] == 0)
      f(i - 1);
    k = aa[i - 1];
  }
  if (i < n - 1 && a[i] < a[i + 1]) {
    if (aa[i + 1] == 0)
      f(i + 1);
    chmax(k, aa[i + 1]);
  }
  k += l - a[i];
  aa[i] = k;
}

int main() {
  cin >> n >> l;
  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    if (aa[i] > 0)
      continue;
    f(i);
  }
  int res = 0;
  for (i = 0; i < n; i++) {
    chmax(res, aa[i]);
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

int n, l;
int a[100010];
int aa[100010];

void f(int i) {
  int k = 0;
  if (i > 0 && a[i] < a[i - 1]) {
    f(i - 1);
    k = aa[i - 1];
  }
  if (i < n - 1 && a[i] < a[i + 1]) {
    f(i + 1);
    chmax(k, aa[i + 1]);
  }
  k += l - a[i];
  aa[i] = k;
}

int main() {
  cin >> n >> l;
  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    if (aa[i] > 0)
      continue;
    f(i);
  }
  int res = 0;
  for (i = 0; i < n; i++) {
    chmax(res, aa[i]);
  }
  cout << res << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
typedef pair<int, int> P;

int N, L;
int length[100000];

bool nobiruka(int x) {
  if (x < 0 || x >= N)
    return false;
  if (x == 0 && length[0] < length[1])
    return true;
  else if (x == N - 1 && length[N - 1] < length[N - 2])
    return true;
  else if (length[x] < length[x + 1] && length[x] < length[x - 1])
    return true;
  return false;
}

int main() {
  cin >> N >> L;
  for (int i = 0; i < N; i++)
    cin >> length[i], length[i] = L - length[i];
  priority_queue<P, vector<P>, greater<P>> pq;
  for (int i = 0; i < N; i++) {
    if (nobiruka(i))
      pq.push(P(length[i], i));
  }
  int ans = 0;
  while (pq.size()) {
    auto tmp = pq.top();
    pq.pop();

    // cout << tmp.first << "A" << tmp.second << endl;

    ans += (tmp.first - ans);
    // cout << ans << endl;
    length[tmp.second] = 10000000;

    if (nobiruka(tmp.second + 1)) {
      pq.push(P(length[tmp.second + 1] + ans, tmp.second + 1));
    }

    if (nobiruka(tmp.second - 1)) {
      pq.push(P(length[tmp.second - 1] + ans, tmp.second - 1));
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;
typedef pair<int, int> P;

int M, L;
int length[100000];

int main() {
  cin >> M >> L;
  for (int i = 0; i < M; i++)
    cin >> length[i], length[i] = L - length[i];
  priority_queue<P, vector<P>, greater<P>> pq;
  vector<P> buf;
  for (int i = 0; i < M; i++) {
    if (i == 0) {
      if (length[i + 1] > length[i])
        pq.push(P(length[i], i));
    } else if (i == M - 1) {
      if (length[i] < length[i - 1]) {
        pq.push(P(length[i], i));
      }
    } else {
      if (length[i] < length[i - 1] && length[i + 1] > length[i]) {
        pq.push(P(length[i], i));
      }
    }
  }
  int ans = 0;
  while (pq.size()) {
    auto tmp = pq.top();
    pq.pop();
    // cout << tmp.first << "a" << tmp.second << endl;
    ans += tmp.first;
    // cout << ans << endl;
    while (pq.size()) {
      buf.push_back(P(pq.top().first - tmp.first, pq.top().second));
      pq.pop();
    }
    for (int i = 0; i < buf.size(); i++)
      pq.push(buf[i]);
    length[tmp.second] = 10000000;
    buf.clear();

    if (tmp.second - 1 >= 0) {
      if (length[tmp.second - 1] != 10000000) {
        if (tmp.second == 1) {
          pq.push(P(length[tmp.second - 1], tmp.second - 1));
        } else if (length[tmp.second - 1] < length[tmp.second - 2]) {
          pq.push(P(length[tmp.second - 1], tmp.second - 1));
        }
      }
    }

    if (tmp.second + 1 <= M - 1) {
      if (length[tmp.second + 1] != 10000000) {
        if (tmp.second + 1 == M - 1) {
          pq.push(P(length[tmp.second + 1], tmp.second + 1));
        } else if (length[tmp.second + 1] < length[tmp.second + 2]) {
          pq.push(P(length[tmp.second + 1], tmp.second + 1));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Runtime Error

// 21
#include <algorithm>
#include <iostream>

using namespace std;

int n, l;
int c[100000];
int t[100000];
int a = 0;

int gr(int i) {
  if (t[i])
    return t[i];
  int li = (i == 0 || c[i - 1] < c[i]) ? 0 : gr(i - 1);
  int ri = (i == n - 1 || c[i + 1] < c[i]) ? 0 : gr(i + 1);
  t[i] = max(li, ri) + l - c[i];
  a = max(a, t[i]);
  return t[i];
}

int main() {
  cin >> n >> l;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < n; i++) {
    gr(i);
  }
  cout << a << endl;
}

Error: 0

Fixed submission:

// 21
#include <algorithm>
#include <iostream>

using namespace std;

int n, l;
int c[10000];
int t[10000];
int a = 0;

int gr(int i) {
  if (t[i])
    return t[i];
  int li = (i == 0 || c[i - 1] < c[i]) ? 0 : gr(i - 1);
  int ri = (i == n - 1 || c[i + 1] < c[i]) ? 0 : gr(i + 1);
  t[i] = max(li, ri) + l - c[i];
  a = max(a, t[i]);
  return t[i];
}

int main() {
  cin >> n >> l;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < n; i++) {
    gr(i);
  }
  cout << a << endl;
}
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>
#include <utility>
using namespace std;

int main() {
  int n, l, buf, pa, h = 0, sz, ice[100002];
  queue<pair<int, int>> q;
  priority_queue<pair<int, int>> bufq;

  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])) {
        bufq.push(make_pair(ice[i], i));
      }
    }
  }

  while (1) {
    sz = bufq.size();
    for (int i = 0; i < sz; i++) {
      q.push(bufq.top());
      bufq.pop();
    }

    pa = l - q.front().first;

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

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

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

  cout << h << endl;

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

#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;

ll n, l, ai[100000], memo[100000];

ll check(ll num) {
  if (memo[num] > 0)
    return memo[num];
  ll left = num != 0 && ai[num] < ai[num - 1] ? check(num - 1) : 0;
  ll right = num != n - 1 && ai[num] < ai[num + 1] ? check(num + 1) : 0;
  return memo[num] = max(left, right) + l - ai[num];
}

int main() {
  scanf("%lld%lld", &n, &l);
  for (ll i = 0; i < n; i++)
    scanf("%lld", &ai[i]), memo[i] = -1;
  ll ans = 0;
  for (ll i = 0; i < n; i++)
    ans = max(ans, check(i));
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int N, L, a[100000];
  scanf("%d%d", &N, &L);
  for (int i = 0; i < N; i++)
    scanf("%d", &a[i]);
  int count = 0, res = 0;
  while (count < N) {
    int m = INT_MAX;
    for (int i = 0; i < N; i++)
      if ((i == 0 || a[i - 1] < a[i]) && (i == N - 1 || a[i + 1] < a[i]))
        m = min(m, L - a[i]);
    res += m;
    for (int i = 0; i < N; i++)
      if ((i == 0 || a[i - 1] < a[i]) && (i == N - 1 || a[i + 1] < a[i]))
        a[i] += m;
    for (int i = 0; i < N; i++)
      if (a[i] == L) {
        a[i] = 0;
        count++;
      }
  }
  printf("%d\\n", res);
}
Problem p00474 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define MAX 100002
typedef pair<int, int> P;

int main() {
  int N, L, in, dp[MAX] = {0};
  priority_queue<P> Q;

  cin >> N >> L;
  for (int i = 1; i <= N; i++) {
    cin >> in;
    Q.push(P(in, i));
  }
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    dp[p.second] = max(dp[p.second - 1], dp[p.second + 1]) + (L - p.first);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 100000
typedef pair<int, int> P;

int main() {
  int N, L, in, dp[MAX] = {0};
  priority_queue<P> Q;

  cin >> N >> L;
  for (int i = 1; i <= N; i++) {
    cin >> in;
    Q.push(P(in, i));
  }
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    dp[p.second] = max(dp[p.second - 1], dp[p.second + 1]) + (L - p.first);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;

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

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

int main() {
  int n, l;
  while (cin >> n >> l) {
    int v[100000];
    vector<int> a, a2, b(n, 0);

    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }

    for (int i = 0; i < n; i++) {
      if (i != 0 && v[i] < v[i - 1])
        a.push_back(i);
      if (i != n - 1 && v[i] < v[i + 1])
        a2.push_back(i);
    }

    while (1) {
      bool f = false;
      for (int i = 0; i < a.size(); i++) {
        int c = b[a[i]];
        b[a[i]] = max(b[a[i]], b[a[i] - 1] + l - v[a[i] - 1]);
        if (c != b[a[i]])
          f = true;
      }
      for (int i = a2.size() - 1; i >= 0; i--) {
        if (a2[i] != n - 1 && v[a2[i]] < v[a2[i] + 1]) {
          int c = b[a2[i]];
          b[a2[i]] = max(b[a2[i]], b[a2[i] + 1] + l - v[a2[i] + 1]);
          if (c != b[a2[i]])
            f = true;
        }
      }
      if (!f)
        break;
    }

    int ans = 0;
    for (int i = 0; i < n; i++)
      ans = max(ans, b[i] + l - v[i]);

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, l;
  while (cin >> n >> l) {
    int v[100000];
    vector<int> a, b(n, 0);

    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }

    for (int i = 0; i < n; i++) {
      if (i == 0 && v[i] > v[i + 1] || i == n - 1 && v[i] > v[i - 1] ||
          i != 0 && i != n - 1 && v[i] > v[i - 1] && v[i] > v[i + 1]) {
      } else
        a.push_back(i);
    }

    while (1) {
      bool f = false;
      for (int i = 0; i < a.size(); i++) {
        if (a[i] != 0 && v[a[i]] < v[a[i] - 1]) {
          int c = b[a[i]];
          b[a[i]] = max(b[a[i]], b[a[i] - 1] + l - v[a[i] - 1]);
          if (c != b[a[i]])
            f = true;
        }
        if (a[i] != n - 1 && v[a[i]] < v[a[i] + 1]) {
          int c = b[a[i]];
          b[a[i]] = max(b[a[i]], b[a[i] + 1] + l - v[a[i] + 1]);
          if (c != b[a[i]])
            f = true;
        }
      }
      if (!f)
        break;
    }

    int ans = 0;
    for (int i = 0; i < n; i++)
      ans = max(ans, b[i] + l - v[i]);

    cout << ans << endl;
  }
  return 0;
}
Problem p00474 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1 << 29;

int N, L;

int v[100002];
int memo[100002];

int dfs(int n) {
  if (memo[n])
    return memo[n];
  int res = L - v[n];
  int wait = 0;
  if (v[n] < v[n + 1])
    wait = dfs(n + 1);
  if (v[n] < v[n - 1])
    wait = max(wait, dfs(n - 1));
  return memo[n] = res + wait;
}

int main() {
  cin >> N >> L;
  for (int i = 1; i <= N; i++)
    cin >> v[i];
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1 << 29;

int N, L;

int v[10002];
int memo[10002];

int dfs(int n) {
  if (memo[n])
    return memo[n];
  int res = L - v[n];
  int wait = 0;
  if (v[n] < v[n + 1])
    wait = dfs(n + 1);
  if (v[n] < v[n - 1])
    wait = max(wait, dfs(n - 1));
  return memo[n] = res + wait;
}

int main() {
  cin >> N >> L;
  for (int i = 1; i <= N; i++)
    cin >> v[i];
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;

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

#include <cstdio>
#include <vector>

using namespace std;

const int MAX_N = 100005;

int main() {
  int n, l;
  long long max_time = 0;
  vector<int> lens(MAX_N, 0);
  vector<int> valleys;

  scanf("%d %d", &n, &l);
  for (int i = 1; i <= n; i++) {
    int ai;
    scanf("%d", &ai);
    lens[i] = ai;
  }

  valleys.push_back(1);
  for (int i = 2; i <= n - 1; i++) {
    if (lens[i] < lens[i - 1] && lens[i] < lens[i + 1]) {
      valleys.push_back(i);
    }
  }
  valleys.push_back(n);

  for (auto &pos : valleys) {
    auto left_time = l - lens[pos], right_time = l - lens[pos];
    auto left_peak = pos;
    while (lens[left_peak - 1] > lens[left_peak]) {
      left_time += l - lens[--left_peak];
    }
    auto right_peak = pos;
    while (lens[right_peak + 1] > lens[right_peak]) {
      right_time += l - lens[++right_peak];
    }
    if (left_time > max_time) {
      max_time = left_time;
    }
    if (right_time > max_time) {
      max_time = right_time;
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

const int MAX_N = 100005;

int main() {
  int n, l;
  unsigned long long max_time = 0;
  vector<int> lens(MAX_N, 0);
  vector<int> valleys;

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

  valleys.push_back(1);
  for (int i = 2; i <= n - 1; i++) {
    if (lens[i] < lens[i - 1] && lens[i] < lens[i + 1]) {
      valleys.push_back(i);
    }
  }
  valleys.push_back(n);

  for (auto &pos : valleys) {
    auto left_pos = pos - 1, right_pos = pos + 1;
    auto left_time = l - lens[pos], right_time = l - lens[pos];
    auto now_pos = pos;
    while (lens[now_pos - 1] > lens[now_pos]) {
      left_time += l - lens[--now_pos];
    }
    now_pos = pos;
    while (lens[now_pos + 1] > lens[now_pos]) {
      right_time += l - lens[++right_pos];
    }
    if (left_time > max_time) {
      max_time = left_time;
    }
    if (right_time > max_time) {
      max_time = right_time;
    }
  }

  printf("%llu\\n", max_time);

  return 0;
}