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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;
typedef pair<ll, string> PLS;

ll n, l;
ll a[100100];

ll sum[100100];

int main() {
  ll ans = 0;
  cin >> n >> l;
  REP(i, n) cin >> a[i];
  ll pos = 0;
  REP(i, n) {
    if (i == n - 1) {
      if (a[i] < a[i - 1]) {
        sum[i] = sum[i - 1] + l - a[i];
        ans = max(ans, sum[i]);
      } else {
        for (int j = i; j >= pos; j--) {
          sum[j] = l - a[j];
          if (j != i) {
            if (j == pos && pos != 0) {
              sum[j] += max(sum[j + 1], sum[j - 1]);
            } else
              sum[j] += sum[j + 1];
            if (j == pos)
              ans = max(ans, sum[j]);
          }
        }
      }
    } else {
      if (a[i] > a[i + 1]) {
        for (int j = i; j >= pos; j--) {
          sum[j] = l - a[j];
          if (j == pos && pos != 0) {
            sum[j] += max(sum[j + 1], sum[j - 1]);
          } else if (j != i)
            sum[j] += sum[j + 1];
          if (j == pos)
            ans = max(ans, sum[j]);
        }
        pos = i + 1;
      }
    }
  }
  // REP(i, n){
  //	cout << sum[i] << endl;
  // }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e15 + 9
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, l;
int a[100000];

int main() {
  ll ans = 0;
  cin >> n >> l;
  REP(i, n) cin >> a[i];
  while (1) {
    vector<int> vi;
    bool F = 0;
    REP(i, n) {
      if (a[i] != 0)
        break;
      if (i == n - 1)
        F = 1;
    }
    if (F)
      break;
    ans++;
    REP(i, n) {
      bool f = 1;
      if (i != 0 && a[i - 1] >= a[i])
        f = 0;
      if (i != n - 1 && a[i + 1] >= a[i])
        f = 0;
      if (f)
        vi.push_back(i);
    }
    REP(i, vi.size()) {
      a[vi[i]]++;
      if (a[vi[i]] == l)
        a[vi[i]] = 0;
    }
  }
  cout << ans << endl;
}