Problem p00474 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define INF 2000000000
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
#define ll long long
#define PL pair<long long, long long>
#define P pair<int, int>
#define mk make_pair
#define en endl;
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0;
#define HERE cout << "here" << en
#define _out(value) cout << "      " << value << en
#define out(value) cout << value << en

#define MAX 100005
int n, l, a[MAX];
int val[MAX];

int main() {
  cin >> n >> l;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = a[n + 1] = 0;
  for (int i = 1; i <= n; i++)
    val[i] = INF;

  for (int i = 1; i <= n; i++) {
    if (val[i] == INF && a[i] > a[i - 1] && a[i] > a[i + 1]) {
      val[i] = l - a[i];

      int nown = i;
      int rec = val[i];
      for (;;) {
        nown++;
        if (a[nown] > a[nown + 1] && nown <= n) {
          val[nown] = rec + l - a[nown];
          rec = val[nown];
        } else
          break;
      }

      nown = i;
      rec = val[i];
      for (;;) {
        nown--;
        if (a[nown] > a[nown - 1] && nown > 0) {
          val[nown] = rec + l - a[nown];
          rec = val[nown];
        } else
          break;
      }
      i += nown - i - 1;
    }
  }

  for (int i = 1; i <= n; i++) {
    if (val[i] == INF) {
      int c = max(val[i - 1], val[i + 1]);
      val[i] = c + l - a[i];
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, val[i]);
  }

  cout << ans << en RE
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define INF 2000000
using namespace std;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
#define ll long long
#define PL pair<long long, long long>
#define P pair<int, int>
#define mk make_pair
#define en endl;
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0;
#define HERE cout << "here" << en
#define _out(value) cout << "      " << value << en
#define out(value) cout << value << en

#define MAX 10005
int n, l, a[MAX];
int val[MAX];

int main() {
  cin >> n >> l;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = a[n + 1] = 0;
  for (int i = 1; i <= n; i++)
    val[i] = INF;

  for (int i = 1; i <= n; i++) {
    if (val[i] == INF && a[i] > a[i - 1] && a[i] > a[i + 1]) {
      val[i] = l - a[i];

      int nown = i;
      int rec = val[i];
      for (;;) {
        nown++;
        if (a[nown] > a[nown + 1] && nown <= n) {
          val[nown] = rec + l - a[nown];
          rec = val[nown];
        } else
          break;
      }

      nown = i;
      rec = val[i];
      for (;;) {
        nown--;
        if (a[nown] > a[nown - 1] && nown > 0) {
          val[nown] = rec + l - a[nown];
          rec = val[nown];
        } else
          break;
      }
      i += nown - i - 1;
    }
  }

  for (int i = 1; i <= n; i++) {
    if (val[i] == INF) {
      int c = max(val[i - 1], val[i + 1]);
      val[i] = c + l - a[i];
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, val[i]);
  }

  cout << ans << en RE
}