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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
using namespace std;

int ary[100010];
int main() {
  priority_queue<pair<int, int>> que;
  int N, L, tim, hog;
  pair<int, int> pa;
  set<int> se;
  set<int>::iterator it;
  cin >> N >> L;
  for (int i = 1; i <= N; i++) {
    cin >> ary[i];
  }
  ary[0] = ary[N + 1] = 0;
  for (int i = 1; i <= N; i++) {
    if (ary[i - 1] < ary[i] && ary[i + 1] < ary[i]) {
      que.push(make_pair(ary[i], i));
    }
  }
  tim = 0;
  while (!que.empty()) {
    se.clear();
    pa = que.top();
    que.pop();
    tim = L - pa.first;
    ary[pa.second] = 0;
    se.insert(pa.second - 1);
    se.insert(pa.second + 1);
    while (!que.empty() && que.top().first == pa.first) {
      ary[que.top().second] = 0;
      se.insert(que.top().second - 1);
      se.insert(que.top().second + 1);
      que.pop();
    }
    for (it = se.begin(); it != se.end(); it++) {
      hog = (*it);
      if (hog > 0 && hog < N + 1 && ary[hog - 1] < ary[hog] &&
          ary[hog + 1] < ary[hog]) {
        que.push(make_pair(ary[hog] - tim, hog));
      }
    }
  }
  cout << tim << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
using namespace std;

int main() {
  int N, L; // N??¬??°L?????????
  int len[100010];
  bool isgrow[100010], iscut[100010];

  cin >> N >> L;
  for (int i = 0; i < N; i++) {
    cin >> len[i];
  }
  for (int i = 0; i < N; i++) {
    if (i != 0 && i != N - 1) {
      isgrow[i] = (len[i] > len[i - 1] && len[i] > len[i + 1]);
    } else if (i == 0) {
      isgrow[i] = len[i] > len[i + 1];
    } else {
      isgrow[i] = len[i] > len[i - 1];
    }
    iscut[i] = false;
  }
  int cnt = 0, ml, mli, tim = 0;
  stack<int> st;
  while (cnt < N) {
    /*
    cout<<"tim("<<cnt<<")="<<tim<<endl;
    for(int i=0;i<N;i++){
            cout<<"len["<<i<<"]="<<len[i]<<" ";
            cout<<"isgrow["<<i<<"]="<<isgrow[i]<<endl;
    }
    */
    ml = 0;
    for (int i = 0; i < N; i++) {
      if (isgrow[i] && ml < len[i]) {
        ml = len[i];
      }
    }
    for (int i = 0; i < N; i++) {
      if (isgrow[i] && ml == len[i]) {
        st.push(i);
      }
    }
    tim += (L - ml);
    for (int i = 0; i < N; i++) {
      if (isgrow[i])
        len[i] += (L - ml);
    }
    while (!st.empty()) {
      mli = st.top();
      len[mli] = 0;
      isgrow[mli] = false;
      iscut[mli] = true;
      if (mli >= 2 && mli <= N - 3) {
        if (!iscut[mli - 1] && len[mli - 1] > len[mli - 2])
          isgrow[mli - 1] = true;
        if (!iscut[mli + 1] && len[mli + 1] > len[mli + 2])
          isgrow[mli + 1] = true;
      } else if (mli == 1) {
        if (!iscut[mli - 1])
          isgrow[mli - 1] = true;
        if (!iscut[mli + 1] && len[mli + 1] > len[mli + 2])
          isgrow[mli + 1] = true;
      } else if (mli == N - 2) {
        if (!iscut[mli - 1] && len[mli - 1] > len[mli - 2])
          isgrow[mli - 1] = true;
        if (!iscut[mli + 1])
          isgrow[mli + 1] = true;
      } else if (mli == 0) {
        if (!iscut[mli + 1] && len[mli + 1] > len[mli + 2])
          isgrow[mli + 1] = true;
      } else if (mli == N - 1) {
        if (!iscut[mli - 1] && len[mli - 1] > len[mli - 2])
          isgrow[mli - 1] = true;
      }
      st.pop();
      cnt++;
    }
  }
  cout << tim << endl;
  return 0;
}