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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const int INF = INT_MAX / 10;

bool ok(int M, int t) { return (t <= M); }

int main() {
  int T;
  while (cin >> T, T) {
    vi ti(T);
    REP(i, T) { cin >> ti[i]; }

    int N;
    cin >> N;
    vvi data(N, vi(2));
    REP(i, N) { cin >> data[i][0] >> data[i][1]; }
    sort(ALL(data));
    int tmpN = N;
    FOR(i, 1, N) {
      if (data[i][0] == data[i - 1][0]) {
        data[i - 1][1] = min(data[i - 1][1], data[i][1]);
        data.erase(data.begin() + i);
        i--;
        N--;
      }
    }

    int dmax = data[N - 1][0];

    vvi dp(dmax + 1, vi(T + 1, INF));
    dp[1][1] = 0;

    int dpos = 0;
    if (data[0][0] == 1) {
      dpos++;
    }

    FOR(dd, 2, dmax + 1) {
      FOR(tt, 1, T) { dp[dd][1] = min(dp[dd][1], dp[dd - 1][tt] + 1); }
      dp[dd][1] = min(dp[dd][1], dp[dd - 1][T]);

      FOR(tt, 2, T + 1) {
        if (data[dpos][0] == dd) {
          if (ok(data[dpos][1], ti[tt - 1])) {
            dp[dd][tt] = dp[dd - 1][tt - 1];
          } else {
            dp[dd][tt] = INF;
          }
        } else {
          dp[dd][tt] = dp[dd - 1][tt - 1];
        }
      }

      if (data[dpos][0] == dd) {
        dpos++;
      }

      if (dpos == N) {
        break;
      }
    }

    int ans = INF;
    REP(t, T + 1) { ans = min(ans, dp[dmax][t]); }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const int INF = INT_MAX / 10;

bool ok(int M, int t) { return (t <= M); }

int main() {
  int T;
  while (cin >> T, T) {
    vi ti(T);
    REP(i, T) { cin >> ti[i]; }

    int N;
    cin >> N;
    vvi data(N, vi(2));
    REP(i, N) { cin >> data[i][0] >> data[i][1]; }
    sort(ALL(data));
    int dmax = data[N - 1][0];

    vvi dp(dmax + 1, vi(T + 1, INF));
    dp[1][1] = 0;

    int dpos = 0;
    if (data[0][0] == 1) {
      dpos++;
    }

    FOR(dd, 2, dmax + 1) {
      FOR(tt, 1, T) { dp[dd][1] = min(dp[dd][1], dp[dd - 1][tt] + 1); }
      dp[dd][1] = min(dp[dd][1], dp[dd - 1][T]);

      FOR(tt, 2, T + 1) {
        if (data[dpos][0] == dd) {
          if (ok(data[dpos][1], ti[tt - 1])) {
            dp[dd][tt] = dp[dd - 1][tt - 1];
          } else {
            dp[dd][tt] = INF;
          }
          dpos++;
        } else {
          dp[dd][tt] = dp[dd - 1][tt - 1];
        }
      }

      if (dpos == N) {
        break;
      }
    }

    int ans = INF;
    REP(t, T + 1) { ans = min(ans, dp[dmax][t]); }
    cout << ans << endl;
  }
}