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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define INF 1 << 29

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int icicles[100005];
int start[100005]; // 相手のつららが落ちれば、自分が成長するつららの番号
int n, l;

ll dfs(int curr) {
  ll res = (ll)max(0, l - icicles[curr]);

  if (start[curr] == curr) {
    return res;
  } // end if

  res += dfs(start[curr]);

  return res;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.icicles3", "r", stdin );
  while (scanf("%d %d", &n, &l) != EOF) {
    memset(icicles, 0, sizeof(icicles));
    memset(start, -1, sizeof(start));
    for (int i = 1; i <= n; i++) {
      scanf("%d", &icicles[i]);
      if (icicles[i] >= l)
        icicles[i] = 0;
    } // end rep

    for (int i = 1; i <= n; i++) {
      if (icicles[i] > icicles[i - 1] && icicles[i] > icicles[i + 1]) {
        start[i] = i;
      } else {
        vector<P> order(3);
        rep(j, 3) {
          order[j].first = -icicles[i + j - 1];
          order[j].second = i + j - 1;
        } // end rep
        sort(ALL(order));
        if (order[1].second == i) {
          start[i] = order[0].second;
        } // end if
      }   // end if
    }     // end for

    for (int i = 1; i <= n; i++) {
      if (start[i] == -1) {
        if (dfs(i - 1) > dfs(i + 1)) {
          start[i] = i - 1;
        } else {
          start[i] = i + 1;
        } // end if
      }   // end if
    }     // end for

    ll res = 0LL;
    for (int i = 1; i <= n; i++) {
      if (icicles[i] == 0)
        continue;
      ll curr = dfs(i);
      res = max(res, curr);
    } // end for
    printf("%ld\\n", res);

  } // end loop

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int icicles[100005];
bool grow[100005];

int main() {
  //	cut here before submit
  //	freopen ("testcase.icicles2", "r", stdin );
  int n, l;
  while (scanf("%d %d", &n, &l) != EOF) {
    memset(icicles, 0, sizeof(icicles));
    for (int i = 1; i <= n; i++) {
      scanf("%d", &icicles[i]);
      if (icicles[i] >= l)
        icicles[i] = 0;

    } // end rep
    int res = 0;
    while ((int)count(icicles + 1, icicles + n + 1, 0) != n) {
      memset(grow, false, sizeof(grow));
      for (int i = 1; i <= n; i++) {
        if (i != 1 && i != n) {
          if (icicles[i] > icicles[i - 1] && icicles[i] > icicles[i + 1]) {
            grow[i] = true;
          } // end if
        } else if (i == 1 && icicles[i] > icicles[i + 1]) {
          grow[i] = true;
        } else if (i == n && icicles[i] > icicles[i - 1]) {
          grow[i] = true;
        } // end if
      }   // end for
      int maxl = 0;
      for (int i = 1; i <= n; i++) {
        if (grow[i] && maxl < icicles[i]) {
          maxl = icicles[i];
        } // end if
      }   // end for

      res += (l - maxl);
      for (int i = 1; i <= n; i++) {
        if (maxl == icicles[i]) {
          icicles[i] = 0;
        } else if (grow[i]) {
          icicles[i] += (l - maxl);
        } // end if
      }   // end for
    }     // end while
    printf("%d\\n", res);

  } // end loop

  return 0;
}