prithivMLmods/Coder-Stat huggingface dataset:
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int top, s[100];
int pop() {
  top--;
  return s[top + 1];
}
void push(int a) {
  s[++top] = a;
  // cout<<a<<endl;
}
int main() {
  char temp[100] = {};
  int a, b;
  while (cin >> temp) {
    if (temp[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (temp[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
      // cout<<a<<"-"<<b<<endl;
    } else if (temp[0] == '*')
      push(pop() * pop());
    else {
      push(atoi(temp));
      // cout<<temp<<endl;
    }
  }
  cout << pop() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int top, s[101];
int pop() {
  top--;
  return s[top + 1];
}
void push(int a) {
  s[++top] = a;
  // cout<<a<<endl;
}
int main() {
  char temp[100] = {};
  int a, b;
  while (cin >> temp) {
    if (temp[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (temp[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
      // cout<<a<<"-"<<b<<endl;
    } else if (temp[0] == '*')
      push(pop() * pop());
    else {
      push(atoi(temp));
      // cout<<temp<<endl;
    }
  }
  cout << pop() << endl;
  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>

using namespace std;

int top, S[100];

void push(int x) { S[++top] = x; }

int pop() {
  top--;
  return S[top + 1];
}

int main() {
  int a, b;
  top = 0;
  string s;
  while (cin >> s) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(s.c_str()));
    }
  }

  cout << pop() << endl;

  return 0;
}


Fixed submission:
#include <cstdlib>
#include <iostream>

using namespace std;

int top, S[101];

void push(int x) { S[++top] = x; }

int pop() {
  top--;
  return S[top + 1];
}

int main() {
  int a, b;
  top = 0;
  string s;
  while (cin >> s) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(s.c_str()));
    }
  }

  cout << pop() << endl;

  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;

int main() {

  string s;
  long int stk[50];
  int indx = 0;
  int i = 0;

  getline(cin, s);

  while (i < (int)s.size()) {

    if (s[i] == ' ') {
    } else if (s[i] == '+') {
      stk[indx - 2] += stk[indx - 1];
      indx--;
    } else if (s[i] == '-') {
      stk[indx - 2] -= stk[indx - 1];
      indx--;
    } else if (s[i] == '*') {
      stk[indx - 2] *= stk[indx - 1];
      indx--;
    } else {
      stk[indx] = 0;
      while (s[i] != ' ') {
        stk[indx] *= 10;
        stk[indx] += s[i] - '0';
        i++;
      }
      i--;
      indx++;
    }

    i++;
  }

  cout << stk[0] << endl;

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;

int main() {

  string s;
  int stk[100];
  int indx = 0;
  int i = 0;

  getline(cin, s);

  while (i < (int)s.size()) {

    if (s[i] == ' ') {
    } else if (s[i] == '+') {
      stk[indx - 2] += stk[indx - 1];
      indx--;
    } else if (s[i] == '-') {
      stk[indx - 2] -= stk[indx - 1];
      indx--;
    } else if (s[i] == '*') {
      stk[indx - 2] *= stk[indx - 1];
      indx--;
    } else {
      stk[indx] = 0;
      while (s[i] != ' ') {
        stk[indx] *= 10;
        stk[indx] += s[i] - '0';
        i++;
      }
      i--;
      indx++;
    }

    i++;
  }

  cout << stk[0] << endl;

  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

int main() {
  string str;
  getline(cin, str);

  stack<int> sta;
  stringstream ss(str);
  string item;
  while (getline(ss, item, ' ')) {
    if ('0' < item[0] && item[0] < '9') {
      sta.push(stoi(item));
      continue;
    }

    int r = sta.top();
    sta.pop();
    int l = sta.top();
    sta.pop();

    switch (item[0]) {
    case '+':
      sta.push(l + r);
      break;
    case '-':
      sta.push(l - r);
      break;
    case '*':
      sta.push(l * r);
      break;
    default:
      break;
    }
  }

  cout << sta.top() << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

int main() {
  string str;
  getline(cin, str);

  stack<int> sta;
  stringstream ss(str);
  string item;
  while (getline(ss, item, ' ')) {
    if ('0' <= item[0] && item[0] <= '9') {
      sta.push(stoi(item));
      continue;
    }

    int r = sta.top();
    sta.pop();
    int l = sta.top();
    sta.pop();

    switch (item[0]) {
    case '+':
      sta.push(l + r);
      break;
    case '-':
      sta.push(l - r);
      break;
    case '*':
      sta.push(l * r);
      break;
    default:
      break;
    }
  }

  cout << sta.top() << endl;

  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

struct stack {
public:
  int top;
  int stk[5];

  void reset() { top = 0; }
  void push(int x) {
    top++;
    stk[top] = x;
  }
  int pop() {
    top--;
    return stk[top + 1];
  }
};

int main() {
  string in;
  stack stack;
  stack.reset();
  while (true) {
    cin >> in;
    if (cin.eof())
      break;
    else if (in == "+") {
      int a = stack.pop();
      int b = stack.pop();
      stack.push(a + b);
    } else if (in == "*") {
      int a = stack.pop();
      int b = stack.pop();
      stack.push(a * b);
    } else if (in == "-") {
      int a = stack.pop();
      int b = stack.pop();
      stack.push(b - a);
    } else {
      stack.push(atoi(in.c_str()));
    }
  }
  cout << stack.pop() << endl;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

struct stack {
public:
  int top;
  int stk[10000];

  void reset() { top = 0; }
  void push(int x) {
    top++;
    stk[top] = x;
  }
  int pop() {
    top--;
    return stk[top + 1];
  }
};

int main() {
  string in;
  stack stack;
  stack.reset();
  while (true) {
    cin >> in;
    if (cin.eof())
      break;
    else if (in == "+") {
      int a = stack.pop();
      int b = stack.pop();
      stack.push(a + b);
    } else if (in == "*") {
      int a = stack.pop();
      int b = stack.pop();
      stack.push(a * b);
    } else if (in == "-") {
      int a = stack.pop();
      int b = stack.pop();
      stack.push(b - a);
    } else {
      stack.push(atoi(in.c_str()));
    }
  }
  cout << stack.pop() << endl;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

template <typename T, int N> class stack {
private:
  int size;
  T array[N];

public:
  stack() { size = 0; }

  bool push(T x) {
    if (isFull()) {
      return false;
    }
    array[size] = x;
    size++;

    return true;
  }

  T pop() {
    size--;
    return array[size];
  }

  bool isEmpty() {
    if (size == 0) {
      return true;
    } else {
      return false;
    }
  }

  bool isFull() {
    if (size >= N) {
      return true;
    } else {
      return false;
    }
  }
};

int main() {
  stack<int, 20000> st;
  string a;
  int j = 0;
  while (j < 7) {
    j++;
    cin >> a;
    if (a[0] == '+') {
      int a = st.pop();
      int b = st.pop();
      st.push(b + a);
    } else if (a[0] == '-') {
      int a = st.pop();
      int b = st.pop();
      st.push(b - a);
    } else if (a[0] == '*') {
      int a = st.pop();
      int b = st.pop();
      st.push(b * a);
    } else {
      int x = 0;
      for (int i = 0; i < a.size(); i++) {
        x *= 10;
        x += a[i] - '0';
      }
      st.push(x);
    }
  }
  cout << st.pop() << "\n";
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>

using namespace std;

template <typename T, int N> class stack {
private:
  int size;
  T array[N];

public:
  stack() { size = 0; }

  bool push(T x) {
    if (isFull()) {
      return false;
    }
    array[size] = x;
    size++;

    return true;
  }

  T pop() {
    size--;
    return array[size];
  }

  bool isEmpty() {
    if (size == 0) {
      return true;
    } else {
      return false;
    }
  }

  bool isFull() {
    if (size >= N) {
      return true;
    } else {
      return false;
    }
  }
};

int main() {
  stack<int, 20000> st;
  string a;
  while (cin >> a) {
    if (a[0] == '+') {
      int a = st.pop();
      int b = st.pop();
      st.push(b + a);
    } else if (a[0] == '-') {
      int a = st.pop();
      int b = st.pop();
      st.push(b - a);
    } else if (a[0] == '*') {
      int a = st.pop();
      int b = st.pop();
      st.push(b * a);
    } else {
      int x = 0;
      for (int i = 0; i < a.size(); i++) {
        x *= 10;
        x += a[i] - '0';
      }
      st.push(x);
    }
  }
  cout << st.pop() << "\n";
  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int s[1000], top = 0;
void push(int x) { s[++top] = x; }
int pop() {
  --top;
  return s[top + 1];
}

int main() {
  int c, b;

  char a[100];
  while (scanf("%s", a)) {
    if (a[0] == '+') {
      b = pop();
      c = pop();
      push(b + c);
    } else if (a[0] == '-') {
      b = pop();
      c = pop();
      push(c - b);
    } else if (a[0] == '*') {
      b = pop();
      c = pop();
      push(b * c);
    } else {
      push(atoi(a));
    }
  }
  printf("%d\n", pop());
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int s[1000], top = 0;
void push(int x) { s[++top] = x; }
int pop() {
  --top;
  return s[top + 1];
}

int main() {
  int c, b;

  char a[100];
  while (scanf("%s", a) != EOF) {
    if (a[0] == '+') {
      b = pop();
      c = pop();
      push(b + c);
    } else if (a[0] == '-') {
      b = pop();
      c = pop();
      push(c - b);
    } else if (a[0] == '*') {
      b = pop();
      c = pop();
      push(b * c);
    } else {
      push(atoi(a));
    }
  }
  printf("%d\n", pop());
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

class Process {
private:
  string name;
  int time;

public:
  Process() = default;
  Process(string name, int time) {
    this->name = name;
    this->time = time;
  }
  string getName() { return this->name; }
  int getTime() { return this->time; }
  void setTime(int time) { this->time = time; }
};

int pos_min = 0;
int pos_max = 0;
Process queue[100000];

void enqueue(Process &process) {
  queue[pos_max] = process;
  pos_max++;
}

Process dequeue() {
  Process process = queue[pos_min];
  pos_min++;
  return process;
}

bool isEmpty() { return pos_min == pos_max; }

int main() {

  int num, quantum;
  scanf("%d %d", &num, &quantum);

  for (int i = 0; i < num; i++) {
    char name[100];
    int time;
    scanf("%s %d", name, &time);
    Process process(name, time);
    enqueue(process);
  }

  int cost = 0;
  while (true) {
    Process process = dequeue();
    if (process.getTime() > quantum) {
      cost += quantum;
      process.setTime(process.getTime() - quantum);
      enqueue(process);
    } else if (process.getTime() <= quantum) {
      cost += process.getTime();
      printf("%s %d\n", process.getName().c_str(), cost);
      if (isEmpty()) {
        break;
      }
    }
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>

using namespace std;

class Process {
private:
  string name;
  int time;

public:
  Process() = default;
  Process(string name, int time) {
    this->name = name;
    this->time = time;
  }
  string getName() { return this->name; }
  int getTime() { return this->time; }
  void setTime(int time) { this->time = time; }
};

int pos_min = 0;
int pos_max = 0;
Process queue[1000000];

void enqueue(Process &process) {
  queue[pos_max] = process;
  pos_max++;
}

Process dequeue() {
  Process process = queue[pos_min];
  pos_min++;
  return process;
}

bool isEmpty() { return pos_min == pos_max; }

int main() {

  int num, quantum;
  scanf("%d %d", &num, &quantum);

  for (int i = 0; i < num; i++) {
    char name[100];
    int time;
    scanf("%s %d", name, &time);
    Process process(name, time);
    enqueue(process);
  }

  int cost = 0;
  while (true) {
    Process process = dequeue();
    if (process.getTime() > quantum) {
      cost += quantum;
      process.setTime(process.getTime() - quantum);
      enqueue(process);
    } else if (process.getTime() <= quantum) {
      cost += process.getTime();
      printf("%s %d\n", process.getName().c_str(), cost);
      if (isEmpty()) {
        break;
      }
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define LEN 100005
using namespace std;

typedef struct node {
  char name[100];
  int time;
} P;

P Q[LEN + 1];
int head, tail, n;

void enqueue(P x) { Q[tail + 1] = x; }

P dequeue() { return Q[head]; }

int main(int argc, char *argv[]) {
  int i, q;

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

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

  int elaps = 0;
  P u;
  head = 0, tail = n;
  while (head != tail) {
    u = Q[head++];
    if (u.time > q) {
      u.time -= q;
      Q[tail++] = u;
      elaps += q;
    } else {
      elaps = elaps + u.time;
      printf("%s %d\n", u.name, elaps);
    }
  }

  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define LEN 300005
using namespace std;

typedef struct node {
  char name[100];
  int time;
} P;

P Q[LEN + 1];
int head, tail, n;

void enqueue(P x) { Q[tail + 1] = x; }

P dequeue() { return Q[head]; }

int main(int argc, char *argv[]) {
  int i, q;

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

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

  int elaps = 0;
  P u;
  head = 0, tail = n;
  while (head != tail) {
    u = Q[head++];
    if (u.time > q) {
      u.time -= q;
      Q[tail++] = u;
      elaps += q;
    } else {
      elaps = elaps + u.time;
      printf("%s %d\n", u.name, elaps);
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAX 100000

int tail = 0;

int head = 0;

typedef struct _queue {
  char name[10];
  int time;
} queuecell;

queuecell queue[MAX];

void enqueue(char *name, int time) {
  tail++;
  strcpy(queue[tail].name, name);
  queue[tail].time = time;
  head++;
}

queuecell dequeue() {
  queuecell cell;

  strcpy(cell.name, queue[head].name);
  cell.time = queue[head].time;
  head++;

  return cell;
}

int main() {
  int n, maxtime;
  int sumtime = 0;
  cin >> n;
  cin >> maxtime;
  queuecell cell;

  for (int i = 0; i < n; i++) {
    cin >> queue[i].name;
    cin >> queue[i].time;
  }
  tail = n - 1;

  while (tail >= head) {
    if (queue[head].time > maxtime) {
      sumtime += maxtime;
      enqueue(queue[head].name, queue[head].time - maxtime);
    } else {
      cell = dequeue();
      sumtime += cell.time;
      printf("%s %d\n", cell.name, sumtime);
    }
  }
}


Fixed submission:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAX 200000

int tail = 0;

int head = 0;

typedef struct _queue {
  char name[10];
  int time;
} queuecell;

queuecell queue[MAX];

void enqueue(char *name, int time) {
  tail++;
  strcpy(queue[tail].name, name);
  queue[tail].time = time;
  head++;
}

queuecell dequeue() {
  queuecell cell;

  strcpy(cell.name, queue[head].name);
  cell.time = queue[head].time;
  head++;

  return cell;
}

int main() {
  int n, maxtime;
  int sumtime = 0;
  cin >> n;
  cin >> maxtime;
  queuecell cell;

  for (int i = 0; i < n; i++) {
    cin >> queue[i].name;
    cin >> queue[i].time;
  }
  tail = n - 1;

  while (tail >= head) {
    if (queue[head].time > maxtime) {
      sumtime += maxtime;
      enqueue(queue[head].name, queue[head].time - maxtime);
    } else {
      cell = dequeue();
      sumtime += cell.time;
      printf("%s %d\n", cell.name, sumtime);
    }
  }
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

class Process {
public:
  string name;
  int time;
};

class myqueue {
public:
  int size;
  int begin;
  int end;
  Process v[111111];
  myqueue() {
    size = 111111;
    begin = 0;
    end = 0; //[begin, end)
  }

  void push(Process a) {
    v[end] = a;
    end = (end + 1) % size;
  }
  Process front() { return v[begin]; }
  void pop() { begin++; }
  bool isEmpty() { return begin == end; }
  void pr() {
    for (int i = begin; i < end; i++) {
      cout << v[i].name << " ";
    }
    cout << endl;
  }
};

int main(void) {
  int n, q;
  myqueue que;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    Process tmp;
    cin >> tmp.name >> tmp.time;
    que.push(tmp);
  }
  int t = 0;
  while (!que.isEmpty()) {
    Process a = que.front();
    que.pop();
    if (a.time > q) {
      a.time -= q;
      que.push(a);
      t += q;
    } else {
      t += a.time;
      cout << a.name << " " << t << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

class Process {
public:
  string name;
  int time;
};

class myqueue {
public:
  int size;
  int begin;
  int end;
  Process v[111111];
  myqueue() {
    size = 111111;
    begin = 0;
    end = 0; //[begin, end)
  }

  void push(Process a) {
    v[end] = a;
    end = (end + 1) % size;
  }
  Process front() { return v[begin]; }
  void pop() { begin = (begin + 1) % size; }
  bool isEmpty() { return begin == end; }
  void pr() {
    for (int i = begin; i < end; i++) {
      cout << v[i].name << " ";
    }
    cout << endl;
  }
};

int main(void) {
  int n, q;
  myqueue que;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    Process tmp;
    cin >> tmp.name >> tmp.time;
    que.push(tmp);
  }
  int t = 0;
  while (!que.isEmpty()) {
    Process a = que.front();
    que.pop();
    if (a.time > q) {
      a.time -= q;
      que.push(a);
      t += q;
    } else {
      t += a.time;
      cout << a.name << " " << t << endl;
    }
  }
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <string>
using namespace std;

#define abs(N) ((N > 0) ? N : -1 * N)
#define lt(A, B) ((A > B) ? B : A)
#define gt(A, B) ((A < B) ? B : A)
#define print(S) (cout << S)
#define println(S) (cout << S << endl)

template <typename T> class queue {
public:
  const static int LEN = 10000;
  queue<T>() { head = tail = 0; }

  void empty() { head = tail = 0; }

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

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

  T dequeue() {
    if (isEmpty()) {
      throw exception();
    }
    if (head == LEN - 1) {
      head = 0;
      return body[LEN - 1];
    }
    return body[head++];
  }

  void enqueue(T el) {
    if (isFull()) {
      throw exception();
    }
    if (tail == LEN - 1) {
      tail = 0;
      body[LEN - 1] = el;
    } else {
      body[tail++] = el;
    }
  }

  T top() {
    if (isEmpty()) {
      throw exception();
    }
    return body[head];
  }

private:
  T body[LEN];
  int head, tail;
};

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

int main() {
  queue<process> que;
  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; ++i) {
    process p;
    cin >> p.name >> p.time;
    que.enqueue(p);
  }
  int t = 0;
  while (!que.isEmpty()) {
    process p = que.dequeue();
    if (p.time > q) {
      p.time -= q;
      que.enqueue(p);
      t += q;
    } else {
      t += p.time;
      cout << p.name << " " << t << endl;
      ;
    }
  }

  return 0;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <string>
using namespace std;

#define abs(N) ((N > 0) ? N : -1 * N)
#define lt(A, B) ((A > B) ? B : A)
#define gt(A, B) ((A < B) ? B : A)
#define print(S) (cout << S)
#define println(S) (cout << S << endl)

template <typename T> class queue {
public:
  const static int LEN = 1000000;
  queue<T>() { head = tail = 0; }

  void empty() { head = tail = 0; }

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

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

  T dequeue() {
    if (isEmpty()) {
      throw exception();
    }
    if (head == LEN - 1) {
      head = 0;
      return body[LEN - 1];
    }
    return body[head++];
  }

  void enqueue(T el) {
    if (isFull()) {
      throw exception();
    }
    if (tail == LEN - 1) {
      tail = 0;
      body[LEN - 1] = el;
    } else {
      body[tail++] = el;
    }
  }

  T top() {
    if (isEmpty()) {
      throw exception();
    }
    return body[head];
  }

private:
  T body[LEN];
  int head, tail;
};

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

int main() {
  queue<process> que;
  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n; ++i) {
    process p;
    cin >> p.name >> p.time;
    que.enqueue(p);
  }
  int t = 0;
  while (!que.isEmpty()) {
    process p = que.dequeue();
    if (p.time > q) {
      p.time -= q;
      que.enqueue(p);
      t += q;
    } else {
      t += p.time;
      cout << p.name << " " << t << endl;
      ;
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

struct queue {
  string name;
  int time;
};

int tail, head;
queue Q[10000000];

void enqueue(queue x);
queue dequeue();

int main() {
  int n, q, t = 0, sum = 0;
  string str;
  cin >> n >> q;
  tail = n;
  head = 0;
  for (int i = 0; i < n; i++)
    cin >> Q[i].name >> Q[i].time;

  while (sum != n) {
    if (Q[head].time <= q)
      t += Q[head].time;
    else
      t += q;
    Q[head].time -= q;
    if (Q[head].time <= 0) {
      cout << Q[head].name << ' ' << t << endl;
      head++;
      sum++;
    } else
      enqueue(dequeue());
  }
  return 0;
}

void enqueue(queue x) { Q[tail++] = x; }

queue dequeue() { return Q[head++]; }

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

struct queue {
  string name;
  int time;
};

int tail, head;
queue Q[1000000];

void enqueue(queue x);
queue dequeue();

int main() {
  int n, q, t = 0, sum = 0;
  string str;
  cin >> n >> q;
  tail = n;
  head = 0;
  for (int i = 0; i < n; i++)
    cin >> Q[i].name >> Q[i].time;

  while (sum != n) {
    if (Q[head].time <= q)
      t += Q[head].time;
    else
      t += q;
    Q[head].time -= q;
    if (Q[head].time <= 0) {
      cout << Q[head].name << ' ' << t << endl;
      head++;
      sum++;
    } else
      enqueue(dequeue());
  }
  return 0;
}

void enqueue(queue x) { Q[tail++] = x; }

queue dequeue() { return Q[head++]; }
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main() {
  int n;
  int q;
  int time_cnt = 0;
  std::queue<string> name;
  std::queue<int> time;
  std::string name1[10000];
  int time1[10000];
  std::string k_name;
  int k_time;

  std::cin >> n >> q;

  for (int i = 0; i < n; i++) {
    std::cin >> name1[i] >> time1[i];
    name.push(name1[i]);
    time.push(time1[i]);
  }

  while (name.size() != 0) {
    if (time.front() <= q) {
      time_cnt += time.front();
      std::cout << name.front() << " " << time_cnt << '\n';
      name.pop();
      time.pop();
    } else {
      time.front() -= q;
      time_cnt += q;
      k_name = name.front();
      k_time = time.front();
      name.pop();
      time.pop();
      name.push(k_name);
      time.push(k_time);
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main() {
  int n;
  int q;
  int time_cnt = 0;
  std::queue<string> name;
  std::queue<int> time;
  std::string name1[100000];
  int time1[100000];
  std::string k_name;
  int k_time;

  std::cin >> n >> q;

  for (int i = 0; i < n; i++) {
    std::cin >> name1[i] >> time1[i];
    name.push(name1[i]);
    time.push(time1[i]);
  }

  while (name.size() != 0) {
    if (time.front() <= q) {
      time_cnt += time.front();
      std::cout << name.front() << " " << time_cnt << '\n';
      name.pop();
      time.pop();
    } else {
      time.front() -= q;
      time_cnt += q;
      k_name = name.front();
      k_time = time.front();
      name.pop();
      time.pop();
      name.push(k_name);
      time.push(k_time);
    }
  }
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>
#define LEN 100005

/* プロセスを表す構造体 */
typedef struct pp {
  char name[100];
  int t;
} P;

P Q[LEN];
int head, tail, n;

void enqueue(P x) {
  Q[tail] = x;
  tail = (tail + 1) % LEN;
}

P dequeue() {
  P x = Q[head];
  head = (head + 1) % LEN;
  return x;
}

int min(int a, int b) { return a < b ? a : b; } /* 最小値を返す */

int main() {
  int elaps = 0, c;
  int i, q;
  P u;
  scanf("%d %d", &n, &q);

  /* 全てのプロセスをキューに順番に追加する */
  for (i = 1; i <= n; i++) {
    scanf("%s", Q[i].name);
    scanf("%d", &Q[i].t);
  }
  head = 1;
  tail = n + 1;

  /* シミュレーション */
  while (head != tail) {
    u = dequeue();
    c = min(q, u.t); /* q または必要な時間 u.t だけ処理を行う */
    u.t -= c;        /* 残りの必要時間を計算 */
    elaps += c;      /* 経過時間を計算 */
    if (u.t > 0)
      enqueue(u); /* 処理が完了していなければキューに追加 */
    else
      printf("%s %s", u.name, elaps);
  }

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <string.h>
#define LEN 100005

/* プロセスを表す構造体 */
typedef struct pp {
  char name[100];
  int t;
} P;

P Q[LEN];
int head, tail, n;

void enqueue(P x) {
  Q[tail] = x;
  tail = (tail + 1) % LEN;
}

P dequeue() {
  P x = Q[head];
  head = (head + 1) % LEN;
  return x;
}

int min(int a, int b) { return a < b ? a : b; } /* 最小値を返す */

int main() {
  int elaps = 0, c;
  int i, q;
  P u;
  scanf("%d %d", &n, &q);

  /* 全てのプロセスをキューに順番に追加する */
  for (i = 1; i <= n; i++) {
    scanf("%s", Q[i].name);
    scanf("%d", &Q[i].t);
  }
  head = 1;
  tail = n + 1;

  /* シミュレーション */
  while (head != tail) {
    u = dequeue();
    c = min(q, u.t); /* q または必要な時間 u.t だけ処理を行う */
    u.t -= c;        /* 残りの必要時間を計算 */
    elaps += c;      /* 経過時間を計算 */
    if (u.t > 0)
      enqueue(u); /* 処理が完了していなければキューに追加 */
    else
      printf("%s %d\n", u.name, elaps);
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second

typedef pair<string, int> P;

void solve(void) {
  int N, Q;
  cin >> N >> Q;
  queue<P> q;
  rep(N, i) {
    string name;
    int t;
    cin >> name >> t;
    q.push(P(name, t));
  }
  int acct = 0;
  while (!q.empty()) {
    P &p = q.front();
    q.pop();
    if (p.snd <= Q) {
      acct += p.snd;
      cout << p.fst << ' ' << acct << endl;
      continue;
    }
    p.second -= Q;
    acct += Q;
    q.push(p);
  }
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

#define Rep(b, e, i) for (int i = b; i <= e; i++)
#define Repr(e, b, i) for (int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second

typedef pair<string, int> P;

void solve(void) {
  int N, Q;
  cin >> N >> Q;
  queue<P> q;
  rep(N, i) {
    string name;
    int t;
    cin >> name >> t;
    q.push(P(name, t));
  }
  int acct = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (p.snd <= Q) {
      acct += p.snd;
      cout << p.fst << ' ' << acct << endl;
      continue;
    }
    p.second -= Q;
    acct += Q;
    q.push(p);
  }
}

int main(void) {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

struct Process {
  string name;
  int time;
};

int const MAX = 100000;
int head;
int tail;
Process Q[MAX];

void initalize() { head = tail = 0; }

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

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

void enqueue(Process x) {
  if (isFull()) {
    cout << "エラー(オーバーフロー)" << endl;
    return;
  }
  Q[tail] = x;
  if (head + 1 == MAX) {
    head = 0;
  } else {
    tail++;
  }
}

Process denqueue() {
  if (isEmpty()) {
    cout << "エラー(アンダーフロー)" << endl;
  }
  Process x = Q[head];
  if (head + 1 == MAX) {
    head = 0;
  } else {
    head++;
  }
  return x;
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; ++i) {
    string name;
    int time;
    cin >> name >> time;
    enqueue(Process{name, time});
  }
  int elapsed_time = 0;
  while (!isEmpty()) {
    Process p = denqueue();
    if (p.time > q) {
      elapsed_time += q;
      p.time -= q;
      enqueue(p);
    } else {
      elapsed_time += p.time;
      cout << p.name << " " << elapsed_time << endl;
    }
  }
}


Fixed submission:
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

struct Process {
  string name;
  int time;
};

int const MAX = 1000000;
int head;
int tail;
Process Q[MAX];

void initalize() { head = tail = 0; }

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

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

void enqueue(Process x) {
  if (isFull()) {
    cout << "エラー(オーバーフロー)" << endl;
    return;
  }
  Q[tail] = x;
  if (head + 1 == MAX) {
    head = 0;
  } else {
    tail++;
  }
}

Process denqueue() {
  if (isEmpty()) {
    cout << "エラー(アンダーフロー)" << endl;
  }
  Process x = Q[head];
  if (head + 1 == MAX) {
    head = 0;
  } else {
    head++;
  }
  return x;
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; ++i) {
    string name;
    int time;
    cin >> name >> time;
    enqueue(Process{name, time});
  }
  int elapsed_time = 0;
  while (!isEmpty()) {
    Process p = denqueue();
    if (p.time > q) {
      elapsed_time += q;
      p.time -= q;
      enqueue(p);
    } else {
      elapsed_time += p.time;
      cout << p.name << " " << elapsed_time << endl;
    }
  }
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define M 100000
typedef struct PT {
  char name[11];
  int time;
} PT;
PT a[M + 1];
int head = 0, tail = 0;
void Init() { head = tail = 0; }
bool IsFull() { return tail - head == -1 || tail - head == M; }
bool IsEmpty() { return head == tail; }
PT dequeue() {
  if (IsEmpty()) {
    cout << "EMPTY" << endl;
    exit(1);
  }
  if (head == M) {
    head = 0;
    return a[M];
  }
  ++head;
  return a[head - 1];
}
void enqueue(PT x) {
  if (IsFull()) {
    cout << "FULL" << endl;
    exit(1);
  }
  a[tail] = x;
  ++tail;
  if (tail == M + 1)
    tail = 0;
}
int main() {
  PT in;
  int i, n, q, time = 0;
  cin >> n >> q;
  for (i = 0; i < n; i++) {
    // cin>>in.name>>in.time;
    scanf("%s%d", in.name, &in.time);
    enqueue(in);
  }
  while (!IsEmpty()) {
    in = dequeue();
    if (in.time <= q) {
      time += in.time;
      // cout<<in.name<<' '<<time<<endl;
      printf("%s %d\n", in.time, time);
    } else {
      time += q;
      in.time -= q;
      enqueue(in);
    }
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define M 100000
typedef struct PT {
  char name[11];
  int time;
} PT;
PT a[M + 1];
int head = 0, tail = 0;
void Init() { head = tail = 0; }
bool IsFull() { return tail - head == -1 || tail - head == M; }
bool IsEmpty() { return head == tail; }
PT dequeue() {
  if (IsEmpty()) {
    cout << "EMPTY" << endl;
    exit(1);
  }
  if (head == M) {
    head = 0;
    return a[M];
  }
  ++head;
  return a[head - 1];
}
void enqueue(PT x) {
  if (IsFull()) {
    cout << "FULL" << endl;
    exit(1);
  }
  a[tail] = x;
  ++tail;
  if (tail == M + 1)
    tail = 0;
}
int main() {
  PT in;
  int i, n, q, time = 0;
  cin >> n >> q;
  for (i = 0; i < n; i++) {
    // cin>>in.name>>in.time;
    scanf("%s%d", in.name, &in.time);
    enqueue(in);
  }
  while (!IsEmpty()) {
    in = dequeue();
    if (in.time <= q) {
      time += in.time;
      // cout<<in.name<<' '<<time<<endl;
      printf("%s %d\n", in.name, time);
    } else {
      time += q;
      in.time -= q;
      enqueue(in);
    }
  }
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdlib.h>

#define N_MAX 1000000

using namespace std;

typedef struct _a {
  char name[10];
  int time;
} P;

int main() {

  int i;
  int n;
  int q;
  int head;
  int tail;
  int res_time = 0;
  char temp[10];
  P A[N_MAX];

  cin >> n;
  cin >> q;

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

  head = 0;
  tail = n - 1;

  while (1) {
    // process
    // ??????????????´??°
    A[head].time -= q;

    // ????????????
    if (A[head].time <= 0) {
      res_time += q + A[head].time;
      cout << A[head].name << ' ' << res_time << endl;
      if (tail == head) {
        break;
      }
    } else {
      // ?????£?????´??°
      tail++;
      if (tail == N_MAX) {
        tail = 0;
      }
      A[tail] = A[head];
      res_time += q;
    }

    // ?????´??°
    head++;
    if (head == N_MAX) {
      head = 0;
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <stdlib.h>

#define N_MAX 99999

using namespace std;

typedef struct _a {
  char name[10];
  int time;
} P;

int main() {

  int i;
  int n;
  int q;
  int head;
  int tail;
  int res_time = 0;
  char temp[10];
  P A[N_MAX];

  cin >> n;
  cin >> q;

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

  head = 0;
  tail = n - 1;

  while (1) {
    // process
    // ??????????????´??°
    A[head].time -= q;

    // ????????????
    if (A[head].time <= 0) {
      res_time += q + A[head].time;
      cout << A[head].name << ' ' << res_time << endl;
      if (tail == head) {
        break;
      }
    } else {
      // ?????£?????´??°
      tail++;
      if (tail == N_MAX) {
        tail = 0;
      }
      A[tail] = A[head];
      res_time += q;
    }

    // ?????´??°
    head++;
    if (head == N_MAX) {
      head = 0;
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

struct Task {
  char name[10];
  int rtime;
};

Task task[100000];
int head, tail, n, q, len = 1000001;

void unit() {
  head = 0;
  tail = 0;
  cin >> n >> q;
}
void enquene(Task t) {
  task[tail] = t;
  tail = (tail + 1) % len;
}

Task dequene() {
  Task t = task[head];
  head = (head + 1) % len;
  return t;
}

int main() {
  unit();
  Task t;
  int ftime = 0;
  for (int i = 0; i < n; i++) {
    cin >> t.name;
    cin >> t.rtime;
    enquene(t);
  }
  while (head != tail) {
    t = dequene();
    if (t.rtime > q) {
      t.rtime -= q;
      ftime += q;
      enquene(t);
    } else {
      ftime += t.rtime;
      cout << t.name << " " << ftime << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

struct Task {
  char name[10];
  int rtime;
};

Task task[100000];
int head, tail, n, q, len = 100000;

void unit() {
  head = 0;
  tail = 0;
  cin >> n >> q;
}
void enquene(Task t) {
  task[tail] = t;
  tail = (tail + 1) % len;
}

Task dequene() {
  Task t = task[head];
  head = (head + 1) % len;
  return t;
}

int main() {
  unit();
  Task t;
  int ftime = 0;
  for (int i = 0; i < n; i++) {
    cin >> t.name;
    cin >> t.rtime;
    enquene(t);
  }
  while (head != tail) {
    t = dequene();
    if (t.rtime > q) {
      t.rtime -= q;
      ftime += q;
      enquene(t);
    } else {
      ftime += t.rtime;
      cout << t.name << " " << ftime << endl;
    }
  }
  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Memory Limit Exceeded):
#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;
}

Fixed submission:
#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;
}
Problem p02264 (C++)

Buggy submission (status: Memory Limit Exceeded):
//
//  ALDS1_3_B.cpp
//
//
//  Created by mk on 2015/12/02.
//
//

#include <iostream>
#include <stdio.h>
#include <string.h>
#define LEN 100005

using namespace ::std;

typedef struct pp {
  char name[1000];
  int t;
} P;

P Q[LEN];
int head, tail, n;

void enqueue(P x) {
  Q[tail] = x;
  tail = (tail + 1) % LEN;
}

P dequeue() {
  P x = Q[head];
  head = (head + 1) % LEN;
  return x;
}

int main(int argc, const char *argv[]) {
  int elaps = 0, c;
  int q;
  P u;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%s", Q[i].name);
    scanf("%d", &Q[i].t);
  }
  head = 1;
  tail = n + 1;

  while (head != tail) {
    u = dequeue();
    c = min(q, u.t);
    u.t -= c;
    elaps += c;
    if (u.t > 0)
      enqueue(u);
    else {
      printf("%s %d\n", u.name, elaps);
    }
  }

  return 0;
}

Fixed submission:
//
//  ALDS1_3_B.cpp
//
//
//  Created by mk on 2015/12/02.
//
//

#include <iostream>
#include <stdio.h>
#include <string.h>
#define LEN 100005

using namespace ::std;

typedef struct pp {
  char name[100];
  int t;
} P;

P Q[LEN];
int head, tail, n;

void enqueue(P x) {
  Q[tail] = x;
  tail = (tail + 1) % LEN;
}

P dequeue() {
  P x = Q[head];
  head = (head + 1) % LEN;
  return x;
}

int main(int argc, const char *argv[]) {
  int elaps = 0, c;
  int q;
  P u;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%s", Q[i].name);
    scanf("%d", &Q[i].t);
  }
  head = 1;
  tail = n + 1;

  while (head != tail) {
    u = dequeue();
    c = min(q, u.t);
    u.t -= c;
    elaps += c;
    if (u.t > 0)
      enqueue(u);
    else {
      printf("%s %d\n", u.name, elaps);
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, q;
  cin >> n;
  cin >> q;
  // vector<int> tm[n];
  // vector<string> name[n];
  int tm[100000];
  string name[100000];
  int total = 0; //??????????????¨
  for (int i = 0; i < n; i++) {
    cin >> name[i];
    cin >> tm[i];
    total += tm[i];
  }
  int ptm = 0; // passed time
  while (1) {
    for (int i = 0; i < n; i++) {
      if (tm[i] <= q) {
        if (tm[i] == 0)
          continue;
        ptm += tm[i];
        tm[i] = 0;
        cout << name[i] << " " << ptm << endl;
        if (total == ptm)
          return 0;
      } else {
        ptm += q;
        tm[i] -= q;
      }
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, q;
  cin >> n;
  cin >> q;
  // vector<int> tm[n];
  // vector<string> name[n];
  int tm[100000];
  string name[100000];
  int total = 0; //??????????????¨
  for (int i = 0; i < n; i++) {
    cin >> name[i];
    cin >> tm[i];
    total += tm[i];
  }
  int ptm = 0; // passed time
  while (1) {
    for (int i = 0; i < n; i++) {
      if (tm[i] == 0) {

      } else if (tm[i] <= q) {
        ptm += tm[i];
        tm[i] = 0;
        cout << name[i] << " " << ptm << endl;
        if (total == ptm)
          return 0;
      } else {
        ptm += q;
        tm[i] -= q;
      }
    }
  }

  return 0;
}
Problem p02264 (C++)

Buggy submission (status: Runtime Error):
#include <array>
#include <iostream>

using namespace std;

struct Process {
  string name;
  int time;
};

class ProcessQueue {
public:
  ProcessQueue(){};
  virtual ~ProcessQueue(){};

  bool push(Process &process) {
    if (isFull()) {
      return false;
    }
    queue[head] = process;
    head++;
    if (head == MAX) {
      head = 0;
    }
    return true;
  }

  Process &pop() {
    if (isEmpty()) {
      // TODO: throw exception.
    }
    Process &value = queue[tail];
    tail++;
    if (tail == MAX) {
      tail = 0;
    }
    return value;
  }

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

  bool isFull() { return (head + tail) == MAX; }

private:
  static const int MAX = 100000;
  array<Process, MAX> queue;
  int head;
  int tail;
};

static void inputProcess(ProcessQueue &processQueue, int n) {
  Process in;
  for (int i = 0; i < n; i++) {
    cin >> in.name >> in.time;
    processQueue.push(in);
  }
}

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

  ProcessQueue processQueue;
  inputProcess(processQueue, n);

  int total = 0;
  while (!processQueue.isEmpty()) {
    Process now = processQueue.pop();
    if (now.time <= q) {
      total += now.time;
      cout << now.name << ' ' << total << endl;
    } else {
      now.time -= q;
      total += q;
      processQueue.push(now);
    }
  }

  return 0;
}


Fixed submission:
#include <array>
#include <iostream>

using namespace std;

struct Process {
  string name;
  int time;
};

class ProcessQueue {
public:
  ProcessQueue() : head(), tail(), queue(){};
  virtual ~ProcessQueue(){};

  bool push(Process &process) {
    if (isFull()) {
      return false;
    }
    queue[head] = process;
    head++;
    if (head == MAX) {
      head = 0;
    }
    return true;
  }

  Process &pop() {
    if (isEmpty()) {
      // TODO: throw exception.
    }
    Process &value = queue[tail];
    tail++;
    if (tail == MAX) {
      tail = 0;
    }
    return value;
  }

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

  bool isFull() { return (head + tail) == MAX; }

private:
  static const int MAX = 100000;
  array<Process, MAX> queue;
  int head;
  int tail;
};

static void inputProcess(ProcessQueue &processQueue, int n) {
  Process in;
  for (int i = 0; i < n; i++) {
    cin >> in.name >> in.time;
    processQueue.push(in);
  }
}

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

  ProcessQueue processQueue;
  inputProcess(processQueue, n);

  int total = 0;
  while (!processQueue.isEmpty()) {
    Process now = processQueue.pop();
    if (now.time <= q) {
      total += now.time;
      cout << now.name << ' ' << total << endl;
    } else {
      now.time -= q;
      total += q;
      processQueue.push(now);
    }
  }

  return 0;
}