Problem p00474 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \\
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

#define MAX 100010

int N, L;
int a[MAX] = {};
int dp[MAX];

int rec(int i) {
  if (dp[i] != -1)
    return dp[i];
  dp[i] = L - a[i];
  int add = 0;
  if (i != 0 && a[i - 1] > a[i])
    add = rec(i - 1);
  if (i != N - 1 && a[i + 1] > a[i])
    add = max(add, rec(i + 1));
  dp[i] += add;
  return dp[i];
}

signed main() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> L;
  rep(i, 0, N) cin >> a[i];
  rep(i, 0, N) rec(i);
  cout << *max_element(dp, dp + N) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \\
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

#define MAX 100010

signed main() {
  int N, L;
  cin >> N >> L;
  int a[MAX] = {};
  rep(i, 0, N) cin >> a[i];
  bool b[MAX] = {};
  int ans = 0;
  rep(h, 0, N) {
    fill(b, b + MAX, false);
    int m = INF;
    rep(i, 0, N) {
      if (i == 0 && a[i] > a[i + 1])
        m = min(m, L - a[i]);
      else if (i == N - 1 && a[i - 1] < a[i])
        m = min(m, L - a[i]);
      else if (a[i - 1] < a[i] && a[i] > a[i + 1])
        m = min(m, L - a[i]);
    }
    if (m != INF)
      ans += m;
    rep(i, 0, N) {
      if (i == 0 && a[i] > a[i + 1])
        b[i] = true;
      else if (i == N - 1 && a[i - 1] < a[i])
        b[i] = true;
      else if (a[i - 1] < a[i] && a[i] > a[i + 1])
        b[i] = true;
    }
    rep(i, 0, N) {
      if (b[i]) {
        a[i] += m;
        if (a[i] == L)
          a[i] = 0;
      }
    }
    // rep(i, 0, N)cout << a[i] << " ";
    // cout << endl;
  }
  cout << ans << endl;

  return 0;
}