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

// AOJ 0579: Hot days
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 10000000;
int n, d, ans, c[201][61], save[3], dt[201], plan[200][2], s[200][101];
void solve() {
  for (int i = 1; i < d; i++) {
    if (plan[i][0] == plan[i][1]) {
      s[i][plan[i][0]] =
          max(s[i - 1][plan[i - 1][0]] + abs(plan[i][0] - plan[i - 1][0]),
              s[i - 1][plan[i - 1][1]] + abs(plan[i][0] - plan[i - 1][1]));
    }

    else {
      s[i][plan[i][0]] =
          max(s[i - 1][plan[i - 1][0]] + abs(plan[i][0] - plan[i - 1][0]),
              s[i - 1][plan[i - 1][1]] + abs(plan[i][0] - plan[i - 1][1]));
      s[i][plan[i][1]] =
          max(s[i - 1][plan[i - 1][0]] + abs(plan[i][1] - plan[i - 1][0]),
              s[i - 1][plan[i - 1][1]] + abs(plan[i][1] - plan[i - 1][1]));
    }
  }
  ans = max(s[d - 1][plan[d - 1][0]], s[d - 1][plan[d - 1][1]]);
}
int main() {
  cin >> d >> n;
  for (int i = 0; i < d; i++) {
    cin >> dt[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 61; j++) {
      c[i][j] = -1;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> save[0] >> save[1] >> save[2];
    for (int j = save[0]; j <= save[1]; j++) {
      c[i][j] = save[2];
    }
  }
  for (int i = 0; i < d; i++) {
    plan[i][0] = INF;
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < n; j++) {
      if (c[j][dt[i]] != -1) {
        plan[i][0] = min(c[j][dt[i]], plan[i][0]);
        plan[i][1] = max(c[j][dt[i]], plan[i][1]);
      }
    }
  }
  solve();
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

// AOJ 0579: Hot days
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 10000000;
int n, d, ans, c[200][61], save[3], dt[200], plan[101][2], s[200][101];
void solve() {
  for (int i = 1; i < d; i++) {
    if (plan[i][0] == plan[i][1]) {
      s[i][plan[i][0]] =
          max(s[i - 1][plan[i - 1][0]] + abs(plan[i][0] - plan[i - 1][0]),
              s[i - 1][plan[i - 1][1]] + abs(plan[i][0] - plan[i - 1][1]));
    }

    else {
      s[i][plan[i][0]] =
          max(s[i - 1][plan[i - 1][0]] + abs(plan[i][0] - plan[i - 1][0]),
              s[i - 1][plan[i - 1][1]] + abs(plan[i][0] - plan[i - 1][1]));
      s[i][plan[i][1]] =
          max(s[i - 1][plan[i - 1][0]] + abs(plan[i][1] - plan[i - 1][0]),
              s[i - 1][plan[i - 1][1]] + abs(plan[i][1] - plan[i - 1][1]));
    }
  }
  ans = max(s[d - 1][plan[d - 1][0]], s[d - 1][plan[d - 1][1]]);
}
int main() {
  cin >> d >> n;
  for (int i = 0; i < d; i++) {
    cin >> dt[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 61; j++) {
      c[i][j] = -1;
    }
  }
  for (int i = 0; i < n; i++) {
    cin >> save[0] >> save[1] >> save[2];
    for (int j = save[0]; j <= save[1]; j++) {
      c[i][j] = save[2];
    }
  }
  for (int i = 0; i < d; i++) {
    plan[i][0] = INF;
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < n; j++) {
      if (c[j][dt[i]] != -1) {
        plan[i][0] = min(c[j][dt[i]], plan[i][0]);
        plan[i][1] = max(c[j][dt[i]], plan[i][1]);
      }
    }
  }
  solve();
  cout << ans << endl;
  return 0;
}