alexjercan/bugnet huggingface dataset:
Problem p02264 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

const int MAX = 100000;

struct Proc {
  std::string name;
  int time;
};

class Queue {
private:
  Proc procs[MAX];
  int head;
  int tail;

public:
  Queue() {
    head = 0;
    tail = 0;
  }
  void enqueue(Proc p) {
    procs[tail] = p;
    if (tail + 1 == MAX)
      tail = 0;
    else
      ++tail;
  }

  Proc dequeue() {
    Proc p = procs[head];
    if (head + 1 == MAX)
      head = 0;
    else
      ++head;
    return p;
  }

  bool isfull() { return head == (tail + 1) % MAX; }

  bool isempty() { return head == tail; }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  Queue queue;
  int n, q;
  std::cin >> n >> q;
  while (n--) {
    std::string name;
    int time;
    std::cin >> name >> time;
    Proc p;
    p.name = name;
    p.time = time;
    queue.enqueue(p);
  }
  int time = 0;
  while (!queue.isempty()) {
    Proc p = queue.dequeue();
    if (p.time <= q) {
      time += p.time;
      p.time = 0;
      std::cout << p.name << " " << time << std::endl;
      continue;
    }
    p.time -= q;
    queue.enqueue(p);
    time += q;
  }
}


Error: TLE

Fixed submission:

#include <iostream>

const int MAX = 100000;

struct Proc {
  std::string name;
  int time;
};

class Queue {
private:
  Proc procs[MAX];
  int num;
  int tail;

public:
  Queue() { tail = 0; }
  void enqueue(Proc p) { procs[tail++] = p; }

  Proc dequeue() {
    Proc ret = procs[0];
    for (int i = 0; i < tail; ++i)
      procs[i] = procs[i + 1];
    --tail;
    return ret;
  }

  bool isempty() { return tail == 0; }
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  Queue queue;
  int n, q;
  std::cin >> n >> q;
  while (n--) {
    std::string name;
    int time;
    std::cin >> name >> time;
    Proc p;
    p.name = name;
    p.time = time;
    queue.enqueue(p);
  }
  int time = 0;
  while (!queue.isempty()) {
    Proc p = queue.dequeue();
    if (p.time <= q) {
      time += p.time;
      p.time = 0;
      std::cout << p.name << " " << time << std::endl;
      continue;
    }
    p.time -= q;
    queue.enqueue(p);
    time += q;
  }
}
Problem p02264 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

struct Process {
  string name;
  int time;
};

void print(const vector<Process> &vec) {
  for (const auto &v : vec)
    cout << v.name << " " << v.time << endl;
}

vector<Process> simulate(queue<Process> &queue, int q) {
  vector<Process> result;
  int time = 0;

  while (!queue.empty()) {
    Process proc = queue.front();
    queue.pop();
    if (proc.time > q) {
      proc.time -= q;
      queue.push(proc);
      time += q;
    } else {
      int tmpTime = proc.time;
      proc.time += time;
      result.push_back(proc);
      time += tmpTime;
    }
  }

  return result;
}

int main() {
  int n, q;
  cin >> n >> q;

  queue<Process> queue;

  for (int i = 0; i < n; i++) {
    string name;
    int time;
    cin >> name >> time;
    queue.push({name, time});
  }

  vector<Process> result = simulate(queue, q);
  print(result);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Process {
  string name;
  int time;
};

void print(const vector<Process> &vec) {
  for (const auto &v : vec)
    cout << v.name << " " << v.time << endl;
}

vector<Process> simulate(vector<Process> &vec, int q) {
  vector<Process> result;
  int time = 0;

  while (!vec.empty()) {
    Process proc = vec.at(0);
    vec.erase(vec.begin());
    if (proc.time > q) {
      proc.time -= q;
      vec.push_back(proc);
      time += q;
    } else {
      int tmpTime = proc.time;
      proc.time += time;
      result.push_back(proc);
      time += tmpTime;
    }
  }

  return result;
}

int main() {
  int n, q;
  cin >> n >> q;

  vector<Process> vec;

  for (int i = 0; i < n; i++) {
    string name;
    int time;
    cin >> name >> time;
    vec.push_back({name, time});
  }

  vector<Process> result = simulate(vec, q);
  print(result);
}
Problem p02264 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
pair<string, int> D[1000000];
int n, q, b[100000];
string s[100000];
int main() {
  scanf("%d%d", &n, &q);
  int cnt = 0, t = 0;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> b[i];
    D[cnt] = make_pair(s[i], b[i]);
    cnt++;
  }
  for (int i = 0; i < cnt; i++) {
    if (D[i].second > q) {
      t += q;
      D[cnt] = make_pair(D[i].first, D[i].second - q);
      cnt++;
    } else {
      t += D[i].second;
      cout << D[i].first << ' ' << t << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
pair<string, int> D[10000000];
int n, q, b[100000];
string s[100000];
int main() {
  scanf("%d%d", &n, &q);
  int cnt = 0, t = 0;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> b[i];
    D[cnt] = make_pair(s[i], b[i]);
    cnt++;
  }
  for (int i = 0; i < cnt; i++) {
    if (D[i].second > q) {
      t += q;
      D[cnt] = make_pair(D[i].first, D[i].second - q);
      cnt++;
    } else {
      t += D[i].second;
      cout << D[i].first << ' ' << t << endl;
    }
  }
  return 0;
}
Problem p02264 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <string>

using namespace std;

#define max_datasets 500000

int main() {
  queue<string> data1;
  queue<int> data2;

  string kari_data1;
  int kari_data2;

  int all_time = 0;

  int n, q;
  string name[max_datasets];
  int time[max_datasets];
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> name[i] >> time[i];
    data1.push(name[i]);
    data2.push(time[i]);
  }

  while (data1.size() != 0) {
    if (data2.front() <= q) {
      all_time += data2.front();
      cout << data1.front() << " " << all_time << endl;
      data1.pop();
      data2.pop();
    } else {
      data2.front() -= q;
      all_time += q;
      kari_data1 = data1.front();
      kari_data2 = data2.front();
      data1.pop();
      data2.pop();
      data1.push(kari_data1);
      data2.push(kari_data2);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <string>

using namespace std;

#define max_datasets 1000000

int main() {
  queue<string> data1;
  queue<int> data2;

  string kari_data1;
  int kari_data2;

  int all_time = 0;

  int n, q;
  string name[max_datasets];
  int time[max_datasets];
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> name[i] >> time[i];
    data1.push(name[i]);
    data2.push(time[i]);
  }

  while (data1.size() != 0) {
    if (data2.front() <= q) {
      all_time += data2.front();
      cout << data1.front() << " " << all_time << endl;
      data1.pop();
      data2.pop();
    } else {
      data2.front() -= q;
      all_time += q;
      kari_data1 = data1.front();
      kari_data2 = data2.front();
      data1.pop();
      data2.pop();
      data1.push(kari_data1);
      data2.push(kari_data2);
    }
  }
  return 0;
}
Problem p02264 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

struct Task {
  int t;
  string n;
};

int main(void) {
  int n, q, i, t, c, head, tail;
  ;
  Task task[100000];
  cin >> n >> q;
  for (i = 0; i < n; i++) {
    cin >> task[i].n >> task[i].t;
  }
  t = c = i = head = 0;
  tail = n;
  while (c != n) {
    if (head == 100000) {
      head = 0;
    }
    if (tail == 100000) {
      tail = 0;
    }

    if (task[head].t > q) {
      task[tail].n = task[head].n;
      task[tail].t = task[head].t - q;
      t += q;
      head++;
      tail++;
    } else if (task[head].t > 0) {
      t += task[head].t;
      c++;
      cout << task[head].n << " " << t << endl;
      head++;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

struct Task {
  int time;
  string name;
};

int main(void) {
  int n, q, i, t, c = 0;
  Task task[100000];
  cin >> n >> q;
  for (i = 0; i < n; i++) {
    cin >> task[i].name >> task[i].time;
  }
  t = 0;
  i = 0;
  while (c != n) {
    if (task[i].time > q) {
      task[i].time -= q;
      t += q;
    } else if (task[i].time > 0) {
      t += task[i].time;
      task[i].time = 0;
      c++;
      cout << task[i].name << " " << t << endl;
    }
    if (i == n - 1) {
      i = 0;
    } else
      i++;
  }
  return 0;
}
Problem p02264 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <string>
using namespace std;

class Proc {
public:
  Proc *prev;
  Proc *next;
  string name;
  int remaining_time;
  Proc(char *n, int t) {
    prev = NULL;
    next = NULL;
    name = n;
    remaining_time = t;
  };
  int subtract_time(int t) {
    remaining_time -= t;
    return remaining_time;
  };
};

class Queue {
  Proc *head;
  Proc *tail;

public:
  Queue() {
    head = NULL;
    tail = NULL;
  };
  void enqueue(Proc *Proc) {
    if (head != NULL) { // if Queue is not empty
      tail->next = Proc;
      Proc->prev = tail;
    } else {
      head = Proc;
    }
    tail = Proc;
  };
  Proc *dequeue(void) {
    Proc *ret = head;
    if (head != NULL) {
      head = head->next;
      if (head != NULL)
        head->prev = NULL;
      ret->prev = NULL;
      ret->next = NULL;
    }
    return ret;
  };
};

int main(void) {
  int n, q;

  char name[10];
  int remaining_time;

  int i;

  scanf("%d %d", &n, &q);

  Queue *queue = new Queue();
  for (i = 0; i < n; i++) {
    scanf("%s %d", name, &remaining_time);
    Proc *p = new Proc(name, remaining_time);
    queue->enqueue(p);
  }

  int count = 0;
  while (true) {
    Proc *p = queue->dequeue();
    if (p == NULL)
      break;
    if (p->remaining_time > q) {
      p->subtract_time(q);
      queue->enqueue(p);
      count += q;
    } else {
      count += p->remaining_time;
      printf("%s %d\\n", p->name.c_str(), count);
      delete p;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

struct process {
  string name;
  int time;
};

int main() {
  int n, q;
  int timer;
  int i, tmp;

  cin >> n >> q;
  process proc[n];
  process fin_proc[n];

  int flags[n];
  for (i = 0; i < n; i++)
    flags[i] = 0;

  for (i = 0; i < n; i++) {
    cin >> proc[i].name >> proc[i].time;
  }

  timer = 0;

  int j = 0;
  while (j < n) {
    for (i = 0; i < n; i++) {
      tmp = (proc[i].time < q) ? proc[i].time : q;
      timer += tmp;
      proc[i].time -= tmp;
      if (flags[i] == 0 && proc[i].time == 0) {
        fin_proc[j].name = proc[i].name;
        fin_proc[j].time = timer;
        flags[i] = 1;
        j++;
      }
    }
  }

  for (i = 0; i < n; i++) {
    cout << fin_proc[i].name << ' ' << fin_proc[i].time << endl;
  }
}
Problem p02264 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>

struct Que {
  int head, tail;
  std::vector<int> time;
  std::vector<std::string> name;
};

int initialize(Que &q, int n) {
  int qt;
  std::cin >> qt;
  for (int i = 0; i < n; i++) {
    int time;
    std::string name;
    std::cin >> name;
    std::cin >> time;
    q.time.push_back(time);
    q.name.push_back(name);
    q.tail++;
  }
  q.time.push_back(0);
  q.name.push_back("end");
  return qt;
}

void enque(int x, std::string str, Que &q, int n) {
  q.time[q.tail % (n + 1)] = x;
  q.name[q.tail % (n + 1)] = str;
  q.tail++;
}

int deque(Que &q, int n) {
  q.head++;
  return q.time[(q.head - 1) % (n + 1)];
}

int main(void) {
  Que q;
  q.head = 0;
  q.tail = 0;
  int time = 0, n;
  std::cin >> n;
  int qt = initialize(q, n);
  while (q.head != q.tail) {
    std::string str = q.name[(q.head) % (n + 1)];
    int x = deque(q, n);
    x -= qt;
    if (x <= 0) {
      time += qt + x;
      std::cout << q.name[(q.head - 1) % (n + 1)] << ' ' << time << '\\n';
    } else {
      enque(x, str, q, n);
      time += qt;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

struct Que {
  int head, tail;
  std::vector<int> time;
  std::vector<std::string> name;
};

int initialize(Que &q, int n) {
  int qt;
  std::cin >> qt;
  for (int i = 0; i < n; i++) {
    int time;
    std::string name;
    std::cin >> name;
    std::cin >> time;
    q.time.push_back(time);
    q.name.push_back(name);
    q.tail++;
  }
  q.time.push_back(0);
  q.name.push_back("end");
  return qt;
}

void enque(int x, std::string str, Que &q, int n) {
  q.time[q.tail % (n + 2)] = x;
  q.name[q.tail % (n + 2)] = str;
  q.tail++;
}

int deque(Que &q, int n) {
  q.head++;
  return q.time[(q.head - 1) % (n + 2)];
}

int main(void) {
  Que q;
  q.head = 0;
  q.tail = 0;
  int time = 0, n;
  std::cin >> n;
  int qt = initialize(q, n);
  while (q.head != q.tail) {
    std::string str = q.name[(q.head) % (n + 2)];
    int x = deque(q, n);
    x -= qt;
    if (x <= 0) {
      time += qt + x;
      std::cout << q.name[(q.head - 1) % (n + 2)] << ' ' << time << '\\n';
    } else {
      enque(x, str, q, n);
      time += qt;
    }
  }
  return 0;
}
Problem p02264 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

struct process {
  string name;
  long int time;
};

int main() {
  long long int n, q;
  process queue[100000];
  long long int first, last, point;
  long long int now_time;
  bool flag_first_last = true;
  cin >> n >> q;
  first = 0;
  last = n - 1;
  for (int i = 0; i < n; i++) {
    cin >> queue[i].name >> queue[i].time;
  }
  point = first;
  now_time = 0;
  while (flag_first_last && (first <= last) ||
         !flag_first_last && (first >= last)) {
    if (queue[point].time > q) {
      now_time += q;
      queue[point].time -= q;
      last++;
      if (last >= 100000) {
        last = 0;
        flag_first_last = !flag_first_last;
      }
      queue[last].name = queue[point].name;
      queue[last].time = queue[point].time;
    } else {
      now_time += queue[point].time;
      cout << queue[point].name << " " << now_time << endl;
    }
    first++;
    if (first >= 100000) {
      first = 0;
      flag_first_last = !flag_first_last;
    }
    point++;
    if (point >= 100000) {
      point = 0;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

struct process {
  string name;
  int time;
};

int main() {
  long int n;
  string name_p;
  int q, time_p;
  long int length, index, current_time;
  process p[100000];
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> name_p >> time_p;
    p[i].name = name_p;
    p[i].time = time_p;
  }
  index = 0;
  length = n;
  current_time = 0;
  while (1) {
    if (index > length - 1) {
      index = 0;
    }
    if (p[index].time <= q) {
      current_time += p[index].time;
      cout << p[index].name << " " << current_time << endl;
      for (int i = index; i < length - 1; i++) {
        p[i] = p[i + 1];
      }
      length--;
      if (length == 0) {
        break;
      }
    } else {
      current_time += q;
      p[index].time -= q;
      index++;
    }
  }
  return 0;
}
Problem p02264 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int top = 0;
int last = 0;
int s[1000010];
string name[1000010];

void push(string a, int n) {
  s[last] = n;
  name[last] = a;
  ++last;
}

int front_time() { return s[top]; }

string front_name() { return name[top]; }

void pop() { ++top; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; ++i) {
    string name;
    int t;
    cin >> name >> t;
    push(name, t);
  }

  int ntime = 0;
  while (top < last) {
    int a = front_time();
    if (a <= q) {
      ntime += a;
      cout << front_name() << " " << ntime << endl;
      pop();
    } else {
      a -= q;
      ntime += q;
      push(front_name(), a);
      pop();
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int top = 0;
int last = 0;
int s[10000010];
string name[10000010];

void push(string a, int n) {
  s[last] = n;
  name[last] = a;
  ++last;
}

int front_time() { return s[top]; }

string front_name() { return name[top]; }

void pop() { ++top; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; ++i) {
    string name;
    int t;
    cin >> name >> t;
    push(name, t);
  }

  int ntime = 0;
  while (top < last) {
    int a = front_time();
    if (a <= q) {
      ntime += a;
      cout << front_name() << " " << ntime << endl;
      pop();
    } else {
      a -= q;
      ntime += q;
      push(front_name(), a);
      pop();
    }
  }

  return 0;
}
Problem p02264 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

struct Process {
  string name;
  int time;
  int count;
};

class Queue {
private:
  Process store[1000000];
  int head, tail;
  Process firstProcess;

public:
  Queue() {
    head = 0;
    tail = 0;
  }

  void enqueue(string name, int time) {
    Process p;
    p.name = name;
    p.time = time;
    store[tail] = p;
    tail++;
  }

  Process dequeue() {
    firstProcess = store[head];
    head++;
    return firstProcess;
  }
};

int main() {
  Queue q;
  int n, rr, time, diff, total;
  string name;
  Process p;

  cin >> n >> rr;
  for (int i = 0; i < n; i++) {
    cin >> name >> time;
    q.enqueue(name, time);
  }

  total = 0;
  while (n) {
    p = q.dequeue();
    diff = p.time - rr;
    if (diff > 0) {
      total += rr;
      q.enqueue(p.name, diff);
    } else {
      total += p.time;
      cout << p.name << " " << total << endl;
      n--;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

struct Process {
  string name;
  int time;
  int count;
};

class Queue {
private:
  Process store[100000];
  int head, tail;
  Process firstProcess;

public:
  Queue() {
    head = 0;
    tail = 0;
  }

  void enqueue(string name, int time) {
    Process p;
    p.name = name;
    p.time = time;
    store[tail] = p;
    tail++;
  }

  Process dequeue() {
    firstProcess = store[head];
    head++;
    return firstProcess;
  }
};

int main() {
  Queue q;
  int n, rr, time, diff, total;
  string name;
  Process p;

  cin >> n >> rr;
  for (int i = 0; i < n; i++) {
    cin >> name >> time;
    q.enqueue(name, time);
  }

  total = 0;
  while (n) {
    p = q.dequeue();
    diff = p.time - rr;
    if (diff > 0) {
      total += rr;
      q.enqueue(p.name, diff);
    } else {
      total += p.time;
      cout << p.name << " " << total << endl;
      n--;
    }
  }
  return 0;
}
Problem p02264 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
using namespace std;
static const int LEN = 100000;

struct Queue {
  char name[10];
  int time;
};

struct Queue Q1[LEN];
int head, tail;

bool isEmpty() { return head == tail; }

bool isFull() { return head == ((tail + 1) % LEN); }

void enqueue(struct Queue q1) {
  Q1[tail] = q1;
  if (tail == (LEN - 1)) {
    tail = 0;
  } else {
    tail++;
  }
}

struct Queue dequeue() {
  if (head == (LEN - 1)) {
    head = 0;
    return Q1[LEN - 1];
  } else {
    return Q1[head++];
  }
}

int main() {
  int n, q, elaps = 0;
  struct Queue x;

  scanf("%d %d", &n, &q);
  for (int i = 1; i < n + 1; i++) {
    scanf("%s %d", Q1[i].name, &Q1[i].time);
  }
  head = 1;
  tail = n + 1;

  while (!isEmpty()) {
    // cout << "head:" << head << "   tail:" << tail << endl;
    if (Q1[head].time > q) {
      Q1[head].time = Q1[head].time - q;
      enqueue(Q1[head]);
      dequeue();
      elaps += q;
    } else {
      x = dequeue();
      elaps += x.time;
      cout << x.name << " " << elaps << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
static const int LEN = 100000;

struct Queue {
  char name[10];
  int time;
};

struct Queue Q1[LEN];
int head, tail;

bool isEmpty() { return head == tail; }

bool isFull() { return head == ((tail + 1) % LEN); }

int enqueue(struct Queue q1) {
  if (isFull()) {
    return -1;
  }
  Q1[tail] = q1;
  if (tail == LEN) {
    tail = 0;
  } else {
    tail++;
  }
  return 0;
}

struct Queue dequeue() {
  /*
  if (isEmpty) {
          return -1;
  }
  */
  if (head == LEN) {
    head = 0;
    return Q1[LEN];
  } else {
    return Q1[head++];
  }
}

int main() {
  int n, q, elaps = 0;
  struct Queue x;

  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++) {
    scanf("%s %d", Q1[i].name, &Q1[i].time);
  }
  head = 0;
  tail = n;

  while (!isEmpty()) {
    if (Q1[head].time > q) {
      Q1[head].time = Q1[head].time - q;
      enqueue(Q1[head++]);
      elaps += q;
    } else {
      x = dequeue();
      elaps += x.time;
      cout << x.name << " " << elaps << endl;
    }
  }
  return 0;
}
Problem p02264 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

struct Task {
  string name;
  int tim;
};

template <typename T> class Q {
  T *t;
  int head, tail, n;

public:
  Q(int _n) : head(0), tail(1), n(_n) { t = new T[n]; }
  ~Q() { delete[] t; }
  void enqueue(T _t) {
    // if(head==tail) throw 1;
    t[(tail - 1) % n] = _t;
    tail++;
  }
  T dequeue() {
    if (head + 1 == tail)
      throw 2;
    T _t = t[head % n];
    head++;
    return _t;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  Q<Task> que(n);
  for (int i = 0; i < n; i++) {
    Task t;
    cin >> t.name >> t.tim;
    que.enqueue(t);
  }

  int sumq = 0;
  try {
    while (true) {
      Task task;
      task = que.dequeue();
      if (task.tim > q) {
        task.tim -= q;
        que.enqueue(task);
        sumq += q;
      } else {
        cout << task.name << ' ' << sumq + task.tim << endl;
        sumq += task.tim;
      }
    }
  } catch (int err) {
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

struct Task {
  string name;
  int tim;
};

template <typename T> class Q {
  T *t;
  int head, tail, n;

public:
  Q(int n) : head(0), tail(1), n(n + 1) { t = new T[n]; }
  ~Q() { delete[] t; }
  void enqueue(T _t) {
    // if(head==tail) throw 1;
    t[(tail - 1) % n] = _t;
    tail++;
  }
  T dequeue() {
    if (head + 1 == tail)
      throw 2;
    T _t = t[head % n];
    head++;
    return _t;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  Q<Task> que(n);
  for (int i = 0; i < n; i++) {
    Task t;
    cin >> t.name >> t.tim;
    que.enqueue(t);
  }

  int sumq = 0;
  try {
    while (true) {
      Task task;
      task = que.dequeue();
      if (task.tim > q) {
        task.tim -= q;
        que.enqueue(task);
        sumq += q;
      } else {
        cout << task.name << ' ' << sumq + task.tim << endl;
        sumq += task.tim;
      }
    }
  } catch (int err) {
    cout << "error" << err;
  }
}