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

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;

struct Call {
  int m, l, k;
  int finish;
  int nextcall;
} call[1010];

int m, t;

int gao(int n) {
  priority_queue<int, vector<int>, greater<int>> que;
  //    printf("n: %d\\n",n);
  int i;
  for (i = 0; i < n; i++) {
    //        printf("push: %d\\n",call[i].m);
    que.push(call[i].m);
    call[i].finish = 1;
  }
  for (i = n; i < m; i++) {
    call[i].finish = 0;
    call[i].nextcall = 0;
  }
  for (i = 1; i <= t; i++) {
    int j;
    //        printf("time: %d\\n",i);
    for (j = 0; j < m; j++) {
      if (call[j].finish == 0 && call[j].nextcall <= i &&
          call[j].nextcall + call[j].l >= i) {
        if (que.top() <= i) {
          que.pop();
          que.push(i + call[j].m);
          call[j].finish = 1;
          //                    printf("finish:%d\\n",j);
        } else {
          if (call[j].nextcall + call[j].l == i) {
            call[j].nextcall = i + call[j].k;
            //                        printf("next: %d top: %d\\n",j,que.top());
          }
        }
      }
    }
  }
  for (i = 0; i < m; i++) {
    //        printf("%d,%d\\n",call[i].finish,call[i].nextcall);
    if (call[i].finish == 0)
      return 0;
  }
  for (i = 0; i < n; i++) {
    if (que.top() > t)
      return 0;
    que.pop();
  }
  return 1;
}

int main(void) {
  while (1) {
    scanf("%d%d", &m, &t);
    if (m == 0 && t == 0)
      break;
    int i;
    for (i = 0; i < m; i++)
      scanf("%d%d%d", &call[i].m, &call[i].l, &call[i].k);

    for (i = 1; i <= m; i++) {
      if (gao(i)) {
        printf("%d\\n", i);
        break;
      }
    }

    //        int l=0,r=m;
    //        while(l!=r){
    //            int mid=(l+r)/2;
    //            int re=gao(mid);
    //            if(re==0) l=mid+1;
    //            else r=mid;
    //            printf("  %d %d\\n",re,mid);
    //        }
    //        printf("%d\\n",r);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;

struct Call {
  int m, l, k;
  int finish;
  int nextcall;
} call[1010];

int m, t;

int gao(int n) {
  priority_queue<int, vector<int>, greater<int>> que;
  printf("n: %d\\n", n);
  int i;
  for (i = 0; i < n; i++) {
    printf("push: %d\\n", call[i].m);
    que.push(call[i].m);
    call[i].finish = 1;
  }
  for (i = n; i < m; i++) {
    call[i].finish = 0;
    call[i].nextcall = 0;
  }
  for (i = 1; i <= t; i++) {
    int j;
    printf("time: %d\\n", i);
    for (j = 0; j < m; j++) {
      if (call[j].finish == 0 && call[j].nextcall <= i &&
          call[j].nextcall + call[j].l >= i) {
        if (que.top() <= i) {
          que.pop();
          que.push(i + call[j].m);
          call[j].finish = 1;
          printf("finish:%d\\n", j);
        } else {
          if (call[j].nextcall + call[j].l == i) {
            call[j].nextcall = i + call[j].k;
            printf("next: %d\\n", j);
          }
        }
      }
    }
  }
  for (i = 0; i < m; i++) {
    printf("%d,%d\\n", call[i].finish, call[i].nextcall);
    if (call[i].finish == 0)
      return 0;
  }
  for (i = 0; i < n; i++) {
    if (que.top() > t)
      return 0;
    que.pop();
  }
  return 1;
}

int main(void) {
  while (1) {
    scanf("%d%d", &m, &t);
    if (m == 0 && t == 0)
      break;
    int i;
    for (i = 0; i < m; i++)
      scanf("%d%d%d", &call[i].m, &call[i].l, &call[i].k);
    int l = 0, r = m;
    while (l != r) {
      int mid = (l + r) / 2;
      int re = gao(mid);
      if (re == 0)
        l = mid + 1;
      else
        r = mid;
      printf("  %d %d\\n", re, mid);
    }
    printf("%d\\n", r);
  }
}