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

#include <algorithm>
#include <iostream>
using namespace std;
int a[100000];
pair<int, int> c[100000];
int now = 0, top = 0;

void NP() {
  int i = top - 2;
  while (true) {
    if (c[i].first < c[i + 1].first)
      break;
    int tmp = c[i].first;
    c[i].first = c[i + 1].first;
    c[i + 1].first = tmp;
    tmp = c[i].second;
    c[i].second = c[i + 1].second;
    c[i + 1].second = tmp;
    if (i == now)
      break;
    i--;
  }
}
int main() {
  int n, l;
  cin >> n >> l;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (a[0] > a[1]) {
    c[top].first = l - a[0];
    c[top].second = 0;
    top++;
    NP();
  }
  for (int i = 1; i < n - 1; i++) {
    if (a[i - 1] < a[i] && a[i] > a[i + 1]) {
      c[top].first = l - a[i];
      c[top].second = i;
      top++;
      NP();
    }
  }
  if (a[n - 1] > a[n - 2]) {
    c[top].first = l - a[n - 1];
    c[top].second = n - 1;
    top++;
    NP();
  }
  sort(c, c + top);
  while (now < top) {
    // cout<<"#"<<c[now].first<<" "<<c[now].second<<endl;
    int clear = c[now].second;
    a[clear] = 0;
    if (clear == 0) {
      if (a[1] > 0) {
        c[top].first = l - a[1] + c[now].first;
        c[top].second = 1;
        top++;
        NP();
      }
    }
    if (clear > 0 && clear < n - 1) {
      if (clear == 1) {
        if (a[0] > 0) {
          c[top].first = l - a[0] + c[now].first;
          c[top].second = 0;
          top++;
          NP();
        }
        if (a[2] > a[3]) {
          c[top].first = l - a[2] + c[now].first;
          c[top].second = 2;
          top++;
          NP();
        }
      } else if (clear == n - 2) {
        if (a[n - 1] > 0) {
          c[top].first = l - a[n - 1] + c[now].first;
          c[top].second = n - 1;
          top++;
          NP();
        }
        if (a[n - 3] > a[n - 4]) {
          c[top].first = l - a[n - 3] + c[now].first;
          c[top].second = n - 3;
          top++;
          NP();
        }
      } else {
        if (a[clear - 1] > a[clear - 2]) {
          c[top].first = l - a[clear - 1] + c[now].first;
          c[top].second = clear - 1;
          top++;
          NP();
        }
        if (a[clear + 1] > a[clear + 2]) {
          c[top].first = l - a[clear + 1] + c[now].first;
          c[top].second = clear + 1;
          top++;
          NP();
        }
      }
    }
    if (clear == n - 1) {
      if (a[n - 2] > 0) {
        c[top].first = l - a[n - 2] + c[now].first;
        c[top].second = n - 2;
        top++;
        NP();
      }
    }

    // sort(c+now,c+top);
    now++;
  }
  cout << c[top - 1].first << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int a[100000];
pair<int, int> c[200000];
int main() {
  int n, l;
  cin >> n >> l;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int top = 0;
  if (a[0] > a[1]) {
    c[top].first = l - a[0];
    c[top].second = 0;
    top++;
  }
  for (int i = 1; i < n - 1; i++) {
    if (a[i - 1] < a[i] && a[i] > a[i + 1]) {
      c[top].first = l - a[i];
      c[top].second = i;
      top++;
    }
  }
  if (a[n - 1] > a[n - 2]) {
    c[top].first = l - a[n - 1];
    c[top].second = n - 1;
    top++;
  }
  sort(c, c + top);
  int now = 0;
  while (now < top) {
    // cout<<"#"<<c[now].first<<" "<<c[now].second<<endl;
    int clear = c[now].second;
    a[clear] = 0;
    if (clear == 0) {
      if (a[1] > 0) {
        c[top].first = l - a[1] + c[now].first;
        c[top].second = 1;
        top++;
      }
    }
    if (clear > 0 && clear < n - 1) {
      if (clear == 1) {
        if (a[0] > 0) {
          c[top].first = l - a[0] + c[now].first;
          c[top].second = 0;
          top++;
        }
        if (a[2] > a[3]) {
          c[top].first = l - a[2] + c[now].first;
          c[top].second = 2;
          top++;
        }
      } else if (clear == n - 2) {
        if (a[n - 1] > 0) {
          c[top].first = l - a[n - 1] + c[now].first;
          c[top].second = n - 1;
          top++;
        }
        if (a[n - 3] > a[n - 4]) {
          c[top].first = l - a[n - 3] + c[now].first;
          c[top].second = n - 3;
          top++;
        }
      } else {
        if (a[clear - 1] > a[clear - 2]) {
          c[top].first = l - a[clear - 1] + c[now].first;
          c[top].second = clear - 1;
          top++;
        }
        if (a[clear + 1] > a[clear + 2]) {
          c[top].first = l - a[clear + 1] + c[now].first;
          c[top].second = clear + 1;
          top++;
        }
      }
    }
    if (clear == n - 1) {
      if (a[n - 2] > 0) {
        c[top].first = l - a[n - 2] + c[now].first;
        c[top].second = n - 2;
        top++;
      }
    }
    sort(c + now, c + top);
    now++;
  }
  cout << c[top - 1].first << endl;
  return 0;
}