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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

int dp[1000001];

int dfs(int icicles[100000], int num_of_icicles, int max_length, int icicle_i) {
  if (dp[icicle_i] != -1)
    return dp[icicle_i];

  int neighbor_cost = 0;
  if (icicle_i < num_of_icicles - 1 &&
      icicles[icicle_i + 1] > icicles[icicle_i]) {
    neighbor_cost = max(dfs(icicles, num_of_icicles, max_length, icicle_i + 1),
                        neighbor_cost);
  }
  if (icicle_i > 0 && icicles[icicle_i - 1] > icicles[icicle_i]) {
    neighbor_cost = max(dfs(icicles, num_of_icicles, max_length, icicle_i - 1),
                        neighbor_cost);
  }
  return (dp[icicle_i] = max(neighbor_cost + (max_length - icicles[icicle_i]),
                             dp[icicle_i]));
}

int main() {
  int num_of_icicles;
  int max_length;
  while (~scanf("%d %d", &num_of_icicles, &max_length)) {
    memset(dp, -1, sizeof(dp));
    int icicles[100000];
    for (int i = 0; i < num_of_icicles; i++) {
      scanf("%d", &icicles[i]);
    }

    int res = 0;
    for (int i = 0; i < num_of_icicles; i++) {
      res = max(res, dfs(icicles, num_of_icicles, max_length, i));
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

int main() {
  int total_icicles, limit_length;
  while (~scanf("%d %d", &total_icicles, &limit_length)) {
    int icicles[100000];
    for (int icicle_idx = 0; icicle_idx < total_icicles; icicle_idx++) {
      scanf("%d", icicles + icicle_idx);
    }

    int res = 0;
    for (int turn = 0; turn < total_icicles; turn++) {
      bool target[100000];
      memset(target, false, sizeof(target));
      int length = INF;
      for (int icicle_idx = 0; icicle_idx < total_icicles; icicle_idx++) {
        if (icicles[icicle_idx] == 0)
          continue;
        int lhs = icicle_idx - 1;
        int rhs = icicle_idx + 1;
        bool isok = true;
        if (lhs >= 0) {
          if (icicles[lhs] > icicles[icicle_idx]) {
            isok = false;
          }
        }
        if (rhs < total_icicles) {
          if (icicles[rhs] > icicles[icicle_idx]) {
            isok = false;
          }
        }

        if (isok) {
          length = min(limit_length - icicles[icicle_idx], length);
          target[icicle_idx] = true;
        }
      }
      if (length == INF)
        break;
      for (int icicle_idx = 0; icicle_idx < total_icicles; icicle_idx++) {
        if (target[icicle_idx]) {
          icicles[icicle_idx] += length;
          if (icicles[icicle_idx] == limit_length) {
            icicles[icicle_idx] = 0;
          }
        }
      }
      res += length;
    }
    printf("%d\\n", res);
  }
}