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

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

const int maxn = 10010;
const int INF = 1 << 29;
struct edge {
  int to, cost, distance;
  edge() {}
  edge(int x, int y, int z) {
    to = x;
    distance = y;
    cost = z;
  }
};

typedef pair<int, int> P;
vector<edge> G[maxn];
bool used[maxn];
int d[maxn];
int N;

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 1; i <= N; i++)
    d[i] = INF;
  for (int i = 1; i <= N; i++)
    used[i] = false;
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (used[v])
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.distance) {
        d[e.to] = d[v] + e.distance;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int M;
  while (~scanf("%d%d", &N, &M) && (N + M)) {
    for (int i = 1; i <= N; i++)
      G[i].clear();
    int a, b, c, v;
    for (int i = 0; i < M; i++) {
      scanf("%d%d%d%d", &a, &b, &c, &v);
      G[a].push_back(edge(b, c, v));
      G[b].push_back(edge(a, c, v));
    }
    dijkstra(1);
    int sum = 0;
    for (int i = 2; i <= N; ++i) {
      int min_cost = INF;
      for (int j = 0; j < G[i].size(); ++j) {
        edge &e = G[i][j];
        if (d[e.to] + e.distance == d[i] && e.cost < min_cost) {
          min_cost = e.cost;
        }
      }
      sum += min_cost;
    }
    printf("%d\\n", sum);
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
using namespace std;
struct edge {
  short dis;
  short cost;
};
const int INF = 1e4;
edge chart[9198][9198];
int N, M;
bool used[10000];
short d[10000];
short cost[10000];
void dijkstra(int start) {
  fill(d, d + N, INF);
  fill(used, used + N, false);
  fill(cost, cost + N, INF);
  d[start] = 0;
  cost[start] = 0;
  while (true) {
    int v = -1;
    for (int i = 0; i < N; i++)
      if (!used[i] && (v == -1 || d[i] < d[v]))
        v = i;
    if (v == -1)
      break;
    used[v] = true;
    for (int i = 0; i < N; i++) {
      if (d[i] == d[v] + chart[v][i].dis) {
        cost[i] = min(cost[i], chart[v][i].cost);
        continue;
      }
      if (d[i] > d[v] + chart[v][i].dis) {
        d[i] = d[v] + chart[v][i].dis;
        cost[i] = chart[v][i].cost;
      }
    }
  }
}
void solve() {
  while (cin >> N >> M) {
    if (N == 0)
      break;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        chart[i][j].dis = INF;
        chart[i][j].cost = INF;
      }
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      cin >> chart[a - 1][b - 1].dis >> chart[a - 1][b - 1].cost;
      chart[b - 1][a - 1] = chart[a - 1][b - 1];
    }
    dijkstra(0);
    int sum = 0;
    for (int i = 0; i < N; i++)
      sum += cost[i];
    cout << sum << endl;
  }
}
int main() { solve(); }
Problem p01366 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int, int> P;
const int maxn = 10005, max_M = 20005;
int n, m;
struct node {
  int to, d, c;
};
int head[max_M * 2], next[max_M * 2], pos;
node mp[max_M * 2];
int d[maxn], ans[maxn];
void init() { memset(head, -1, sizeof(head)); }
void add_Edge(int u, node t) {
  mp[pos] = t;
  next[pos] = head[u];
  head[u] = pos++;
}
void Dijkstra(int s) {
  memset(d, 0x3f, sizeof(d));
  memset(ans, 0x3f, sizeof(ans));
  d[s] = ans[s] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(P(0, s));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int u = p.second;
    for (int i = head[u]; ~i; i = next[i]) {
      node e = mp[i];
      if (d[e.to] > d[u] + e.d) {
        d[e.to] = d[u] + e.d;
        ans[e.to] = e.c;
        q.push(P(d[e.to], e.to));
      } else if (d[e.to] == d[u] + e.d && ans[e.to] > e.c) {
        d[e.to] = d[u] + e.d;
        ans[e.to] = e.c;
        q.push(P(d[e.to], e.to));
      }
    }
  }
}
void solve() {
  int u, v, di, ci;
  while (~scanf("%d%d", &n, &m) && (n || m)) {
    init();
    pos = 0;
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &di, &ci);
      add_Edge(u, (node){v, di, ci});
      add_Edge(v, (node){u, di, ci});
    }
    Dijkstra(1);
    int res = 0;
    for (int i = 1; i <= n; i++)
      res += ans[i];
    printf("%d\\n", res);
  }
}
int main() {
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 2e9;
int n, m;
int gh[10008][10008];
int val[10008][10008];
int vis[10008];
int dis[10008];
int cos[10008];

void spfa(int s) {
  for (int i = 1; i <= n; i++) {
    dis[i] = MAXN;
    cos[i] = MAXN;
    vis[i] = 0;
  }
  dis[s] = 0;
  cos[s] = 0;
  vis[s] = 1;
  queue<int> que;
  que.push(s);
  while (!que.empty()) {
    int t = que.front();
    que.pop();
    for (int i = 1; i <= n; i++) {
      if (dis[i] == dis[t] + gh[t][i])
        cos[i] = min(cos[i], val[t][i]);
      else if (dis[i] > dis[t] + gh[t][i]) {
        dis[i] = dis[t] + gh[t][i];
        cos[i] = val[t][i];
        if (!vis[i]) {
          vis[i] = 1;
          que.push(i);
        }
      }
    }
    vis[t] = 0;
  }
}

int main() {
  while (scanf("%d %d", &n, &m), n || m) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j) {
          gh[i][j] = 0;
          val[i][j] = 0;
        }
        gh[i][j] = MAXN;
        val[i][j] = MAXN;
      }
    }
    int u, v, w, c;
    for (int i = 1; i <= m; i++) {
      scanf("%d %d %d %d", &u, &v, &w, &c);
      gh[u][v] = gh[v][u] = w;
      val[u][v] = val[v][u] = c;
    }
    spfa(1);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      ans += cos[i];
      //            cout<<"dis："<<i<<" "<<dis[i]<<endl;
      //            cout<<"cos："<<i<<" "<<cos[i]<<endl;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p01366 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 10050, maxm = 20050, inf = 0x3f3f3f3f;
struct Edge {
  int to, next, dis, cos;
} edge[maxm * 2];
struct Pair {
  int D, V, C;
  Pair() { ; }
  Pair(int d, int c, int v) { D = d, V = v, C = c; }
  friend bool operator<(const Pair a, const Pair b) {
    if (a.D != b.D)
      return a.D > b.D;
    else
      return a.C > b.C;
  }
};
int tot, head[maxn], dist[maxn], cost[maxn];
bool vis[maxn];
void init() {
  tot = 0;
  memset(head, -1, sizeof(head));
}
void add_edge(int u, int v, int d, int c) {
  edge[tot].to = v;
  edge[tot].dis = d;
  edge[tot].cos = c;
  edge[tot].next = head[u];
  head[u] = tot++;
}
int dijstra(int start) {
  memset(vis, false, sizeof(vis));
  memset(dist, inf, sizeof(dist));
  int ans = 0;
  dist[start] = 0;
  // vis[start]=true;
  priority_queue<Pair> que;
  que.push(Pair(0, 0, start));
  while (!que.empty()) {
    Pair now = que.top();
    que.pop();
    if (vis[now.V])
      continue;
    vis[now.V] = true;
    ans += now.C;
    for (int i = head[now.V]; i != -1; i = edge[i].next) {
      int to = edge[i].to;
      que.push(Pair(now.D + edge[i].dis, edge[i].cos, to));
    }
  }
  return ans;
}
int main() {
  int u, v, d, c;
  int n, m;
  while (scanf("%d%d", &n, &m) != -1) {
    init();
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &d, &c);
      add_edge(u, v, d, c);
      add_edge(v, u, d, c);
    }
    printf("%d\\n", dijstra(1));
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

const int INF = 99999999;

struct Edge {
  int v, dis, cost, next;
  Edge(){};
  Edge(int a, int b, int c, int d) {
    v = a;
    dis = b;
    cost = c;
    next = d;
  }
} edge[20002];

struct Pair {};

int ma[10002][10002];

int head[10002], vis[10002];
int n, m, top, ans;

bool operator<(Edge a, Edge b) {
  return a.dis == b.dis ? a.cost < b.cost : a.dis < b.dis;
}

void init() {
  for (int i = 0; i <= n; i++) {
    head[i] = -1;

    vis[i] = 0;
  }
  top = 1;
  ans = 0;
}

void add(int a, int b, int c, int d) {
  edge[top].v = b;
  edge[top].dis = c;
  edge[top].cost = d;
  edge[top].next = head[a];
  head[a] = top++;
}

void dijkstra() {
  priority_queue<Edge> que;
  Edge a(1, 0, 0, 0);

  que.push(a);
  while (!que.empty()) {
    Edge u = que.top();
    que.pop();
    if (vis[u.v])
      continue;
    vis[u.v] = 1;
    ans += u.cost;
    for (int x = head[u.v]; x != -1; x = edge[x].next) {
      que.push(
          Edge(edge[x].v, u.dis + edge[x].dis, edge[x].cost, edge[x].next));
    }
  }
}

int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    init();
    for (int i = 1; i <= m; i++) {
      int a, b, c, d;
      scanf("%d %d %d %d", &a, &b, &c, &d);
      add(a, b, c, d);
      add(b, a, c, d);
    }

    dijkstra();

    printf("%d\\n", ans);
  }
}
Problem p01366 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <vector>
#define INT_MAX 100000000
#define N_MAX 10001

using namespace std;

class road {
public:
  int to, dist, cost;
  road(int to, int dist, int cost) : to(to), dist(dist), cost(cost) {}
};

int n, m;
int before[N_MAX], ans;
vector<road> roads[N_MAX];

int main() {
  while (scanf("%d %d", &n, &m), n) {
    int i, j;

    ans = 0;
    for (i = 0; i < N_MAX; i++) {
      roads[i].clear();
      before[i] = 0;
    }

    for (i = 0; i < m; i++) {
      int u, v, d, c;
      scanf("%d %d %d %d", &u, &v, &d, &c);
      roads[u].push_back(road(v, d, c));
      roads[v].push_back(road(u, d, c));
    }

    bool visited[n + 1];
    int d[n + 1];
    for (i = 1; i <= n; i++) {
      d[i] = INT_MAX;
      visited[i] = false;
    }

    d[1] = 0;

    for (;;) {
      int u;
      int mincost = INT_MAX;
      for (i = 1; i <= n; i++)
        if (!visited[i] && d[i] < mincost) {
          mincost = d[i];
          u = i;
        }

      if (mincost == INT_MAX)
        break;

      visited[u] = true;
      for (i = 0; i < roads[u].size(); i++)
        if (roads[u][i].to == before[u])
          ans += roads[u][i].cost;

      for (i = 0; i < roads[u].size(); i++) {
        int v = roads[u][i].to, dist = roads[u][i].dist,
            cost = roads[u][i].cost;

        if (visited[v])
          continue;

        int cost1;
        for (j = 0; j < roads[v].size(); j++)
          if (roads[v][j].to == before[v])
            cost1 = roads[v][j].cost;

        if (d[v] > d[u] + dist || (d[v] == d[u] + dist && cost1 > cost)) {
          d[v] = d[u] + dist;
          before[v] = u;
        }
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#define INT_MAX 100000000

int n, m;
int dist[1001][1001], cost[1001][1001], before[1001], ans;

int main() {
  while (scanf("%d %d", &n, &m), n) {
    int i, j;

    ans = 0;
    for (i = 0; i < 1001; i++) {
      for (j = 0; j < 1001; j++) {
        dist[i][j] = INT_MAX;
        cost[i][j] = 0;
      }
      before[i] = 0;
    }

    for (i = 0; i < m; i++) {
      int u, v, d, c;
      scanf("%d %d %d %d", &u, &v, &d, &c);
      dist[u][v] = d;
      cost[u][v] = c;
      dist[v][u] = d;
      cost[v][u] = c;
    }

    bool visited[n + 1];
    int d[n + 1];
    for (i = 1; i <= n; i++) {
      d[i] = INT_MAX;
      visited[i] = false;
    }

    d[1] = 0;

    for (;;) {
      int u;
      int mincost = INT_MAX;
      for (i = 1; i <= n; i++)
        if (!visited[i] && d[i] < mincost) {
          mincost = d[i];
          u = i;
        }

      if (mincost == INT_MAX)
        break;

      visited[u] = true;
      ans += cost[u][before[u]];

      for (i = 1; i <= n; i++) {
        if (visited[i] || dist[u][i] == INT_MAX)
          continue;

        if (d[i] > d[u] + dist[u][i] ||
            (d[i] == d[u] + dist[u][i] && cost[i][before[i]] > cost[i][u])) {
          d[i] = d[u] + dist[u][i];
          before[i] = u;
        }
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}
Problem p01367 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int m[1000], hang[1000], next[1000];
bool vis[1000];
int last[1000], tim[1000];
bool check(int p, int n, int t) {
  memset(vis, 0, n);
  memset(tim, 0, 4 * p);
  memset(last, 0, 4 * n);
  int ret = 0;
  for (int i = 0; i <= t; ++i) {
    for (int j = 0, k = 0; j < p && k < n; ++j)
      if (tim[j] <= i) {
        while (k < n && (last[k] > i || last[k] + hang[k] < i || vis[k] == 1))
          ++k;
        if (k < n) {
          tim[j] = i + m[k];
          if (tim[j] > t)
            return 0;
          vis[k] = 1;
          ++ret;
        }
      }
    for (int k = 0; k < n; ++k)
      if (!vis[k] && last[k] + hang[k] <= i)
        last[k] += hang[k] + next[k];
  }
  return ret == n;
}
int main() {
  int n, t;
  while (scanf("%d%d", &n, &t), n) {
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      scanf("%d%d%d", &m[i], &hang[i], &next[i]);
      sum += m[i];
    }
    for (int i = max(1, sum / t); i <= n; ++i)
      if (check(i, n, t)) {
        printf("%d\\n", i);
        break;
      }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int m[1000], l[1000], k[1000];
int tim[1000];
bool check(int p, int n, int t) {
  set<pair<int, int>> ev, ev2;
  multiset<int> per;
  set<int> tod;
  for (int i = 0; i < n; ++i)
    ev.insert(make_pair(0, i));
  int now = 0;
  while (now <= t) {
    if (!per.empty() && *per.begin() == now)
      per.erase(now);
    while (!ev.empty() && (*ev.begin()).first == now) {
      pair<int, int> tmp;
      int id = (*ev.begin()).second;
      tim[id] = tmp.first = (*ev.begin()).first + l[id];
      tmp.second = id;
      ev2.insert(tmp);
      tod.insert(id);
      ev.erase(ev.begin());
    }
    while (!tod.empty() && (int)per.size() < p) {
      int id = *tod.begin();
      per.insert(now + m[id]);
      tod.erase(tod.begin());
      ev2.erase(make_pair(tim[id], id));
    }
    while (!ev2.empty() && (*ev2.begin()).first == now) {
      pair<int, int> tmp;
      int id = (*ev2.begin()).second;
      tmp.first = (*ev2.begin()).first + k[id];
      tmp.second = id;
      ev.insert(tmp);
      ev2.erase(ev2.begin());
      tod.erase(id);
    }
    if (ev.empty() && per.empty())
      break;
    now = t + 1;
    if (!ev.empty())
      now = min(now, (*ev.begin()).first);
    if (!ev2.empty())
      now = min(now, (*ev2.begin()).first);
    if (!per.empty())
      now = min(now, *per.begin());
  }
  return now <= t;
}
int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("ans.txt", "w", stdout);
  int n, t;
  while (scanf("%d%d", &n, &t), n) {
    for (int i = 0; i < n; ++i)
      scanf("%d%d%d", &m[i], &l[i], &k[i]);
    for (int i = 1; i <= n; ++i)
      if (check(i, n, t)) {
        printf("%d\\n", i);
        break;
      }
  }
  return 0;
}
Problem p01367 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

bool simulate(const vector<int> &L, const vector<pair<int, int>> &vp, int T,
              int M) {
  int N = vp.size();
  vector<int> lest(T + 1, 0);
  vector<bool> ok(vp.size(), false);
  for (int i = 0; i < M; i++)
    lest[L[i]]++;
  int cnt = N - M;
  for (int i = 1; i < T; i++) {
    if (cnt == 0)
      return true;
    if (lest[i] == 0)
      continue;
    for (int j = M; j < N; j++) {
      if (ok[j])
        continue;
      if (i % vp[j].second <= vp[j].first) {
        if (i + L[j] > T)
          return false;
        cnt--;
        ok[j] = true;
        lest[i]--;
        lest[i + L[j]]++;
      }
      if (lest[i] == 0)
        break;
    }
    lest[i + 1] += lest[i];
  }
  return cnt == 0;
}

int main() {
  int N, T;
  while (cin >> N >> T, N) {
    vector<int> L(N);
    vector<pair<int, int>> vp(N);
    for (int i = 0; i < N; i++) {
      cin >> L[i] >> vp[i].first >> vp[i].second;
      vp[i].second += vp[i].first;
    }
    for (int i = 1; i <= N; i++) {
      if (simulate(L, vp, T, i)) {
        cout << i << endl;
        break;
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

bool simulate(const vector<int> &L, const vector<pair<int, int>> &vp, int T,
              int M) {
  int N = vp.size();
  vector<int> lest(T + 1, 0);
  vector<bool> ok(vp.size(), false);
  for (int i = 0; i < M; i++)
    lest[L[i]]++;
  int cnt = N - M;
  for (int i = 1; i < T; i++) {
    if (cnt == 0)
      return true;
    if (lest[i] == 0)
      continue;
    for (int j = M; j < N; j++) {
      if (ok[j])
        continue;
      if (i % vp[j].second <= vp[j].first) {
        if (i + L[j] > T)
          return false;
        cnt--;
        ok[j] = true;
        lest[i]--;
        lest[i + L[j]]++;
      }
      if (lest[i] == 0)
        break;
    }
    lest[i + 1] += lest[i];
  }
  return cnt == 0;
}

int main() {
  int N, T;
  ifstream fin("I.txt");
  while (fin >> N >> T, N) {
    vector<int> L(N);
    vector<pair<int, int>> vp(N);
    for (int i = 0; i < N; i++) {
      fin >> L[i] >> vp[i].first >> vp[i].second;
      vp[i].second += vp[i].first;
    }
    for (int i = 1; i <= N; i++) {
      if (simulate(L, vp, T, i)) {
        cout << i << endl;
        break;
      }
    }
  }
}
Problem p01367 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int n, T, talk[1000], call[1000], wait[1000];

bool solve(int p) {
  priority_queue<int> pq;
  rep(i, p) pq.push(0);

  int n_cus = 0;
  bool cus[1000] = {};
  while (!pq.empty()) {
    int t = -pq.top();
    pq.pop();

    int i, next = T + 1;
    for (i = 0; i < n; i++)
      if (!cus[i]) {
        int total = call[i] + wait[i];
        if (t % total <= call[i])
          break;
        else
          next = min(next, t + total - t % total);
      }
    if (i < n) {
      n_cus++;
      cus[i] = true;

      if (t + talk[i] <= T)
        pq.push(-(t + talk[i]));
      else
        return false;

      if (n_cus == n)
        return true;
    } else {
      if (next <= T)
        pq.push(-next);
      else
        return false;
    }
  }

  return false;
}

int main() {
  for (; scanf("%d%d", &n, &T), n;) {
    rep(i, n) scanf("%d%d%d", talk + i, call + i, wait + i);

    int p;
    for (p = 1; !solve(p); p++)
      ;

    printf("%d\\n", p);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int n, T, talk[1000], call[1000], wait[1000];

bool solve(int p) {
  priority_queue<int> pq;
  rep(i, p) pq.push(0);

  int n_cus = 0;
  bool cus[1000] = {};
  while (1) {
    int t = -pq.top();
    pq.pop();

    int i;
    for (i = 0; i < n; i++)
      if (!cus[i] && t % (call[i] + wait[i]) <= call[i])
        break;
    if (i < n) {
      n_cus++;
      cus[i] = true;
      pq.push(-(t + talk[i]));

      if (t + talk[i] > T)
        return false;
      if (n_cus == n)
        break;
    } else {
      pq.push(-(t + 1));
    }
  }

  return true;
}

int main() {
  for (; scanf("%d%d", &n, &T), n;) {
    rep(i, n) scanf("%d%d%d", talk + i, call + i, wait + i);

    int p;
    for (p = 1; !solve(p); p++)
      ;

    printf("%d\\n", p);
  }

  return 0;
}
Problem p01367 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, t;
vector<int> m, l, k;

bool solve(int ope) {
  vector<int> opeEnd(t + 1, 0);
  opeEnd[0] = ope;
  vector<bool> check(n, false);
  int opeRest = 0;
  int cusRest = n;

  for (int i = 0; i <= t; ++i) {
    opeRest += opeEnd[i];
    for (int j = 0; j < n; ++j) {
      if (opeRest == 0)
        break;
      if (!check[j] && i % (l[j] + k[j]) <= l[j]) {
        if (i + m[j] > t)
          return false;
        ++opeEnd[i + m[j]];
        check[j] = true;
        --opeRest;
        if (--cusRest == 0)
          return true;
      }
    }
  }

  return false;
}

int main() {
  for (;;) {
    cin >> n >> t;
    if (n == 0)
      return 0;

    m.resize(n);
    l.resize(n);
    k.resize(n);
    for (int i = 0; i < n; ++i)
      cin >> m[i] >> l[i] >> k[i];

    int ret = 1;
    while (!solve(ret))
      ++ret;

    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, t;
vector<int> m, l, k;

bool solve(int ope) {
  multiset<int> ms;
  for (int i = 0; i < ope; ++i)
    ms.insert(0);

  vector<bool> check(n, false);
  for (int i = 0; i < n; ++i) {
    int currTime = *ms.begin();
    ms.erase(ms.begin());

    int minTime = INT_MAX;
    int select;
    for (int j = 0; j < n; ++j) {
      if (check[j])
        continue;
      int time = currTime / (l[j] + k[j]) * (l[j] + k[j]);
      if (currTime > time + l[j])
        time += l[j] + k[j];
      else
        time = currTime;
      if (time < minTime) {
        select = j;
        minTime = time;
      }
    }

    check[select] = true;
    ms.insert(minTime + m[select]);
  }

  if (*ms.rbegin() > t)
    return false;
  else
    return true;
}

int main() {
  for (;;) {
    cin >> n >> t;
    if (n == 0)
      return 0;

    m.resize(n);
    l.resize(n);
    k.resize(n);
    for (int i = 0; i < n; ++i)
      cin >> m[i] >> l[i] >> k[i];

    int ret = 1;
    while (!solve(ret))
      ++ret;

    cout << ret << endl;
  }
}
Problem p01367 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define M 1003
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
int n, t, m[M], l[M], k[M], add[M];
bool used[M];
bool ch(int x) {
  memset(add, 0, sizeof(add));
  memset(used, 0, sizeof(used));
  int cnt = 0;
  r(i, t + 1) {
    x += add[i];
    r(j, n) if (!used[j] && x) {
      int p = l[j] + k[j];
      if ((i % p <= l[j]) && m[j] + i <= t) {
        add[i + m[j]]++;
        used[j] = 1;
        x--;
        cnt++;
      }
    }
  }
  return cnt == n;
}
int main() {
  while (cin >> n >> t, t) {
    r(i, n) cin >> m[i] >> l[i] >> k[i];
    r(i, M) {
      if (ch(i)) {
        cout << i << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define M 1001
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
int n, t, m[M], l[M], k[M], add[M];
bool used[M];
bool ch(int x) {
  memset(add, 0, sizeof(add));
  memset(used, 0, sizeof(used));
  int cnt = 0;
  r(i, t + 1) {
    x += add[i];
    r(j, n) if (!used[j]) {
      int p = l[j] + k[j];
      if (i % p <= l[j] && x) {
        if (m[j] + i < M)
          add[i + m[j]]++;
        used[j] = 1;
        x--;
        cnt++;
      }
    }
  }
  return cnt == n;
}
int main() {
  while (cin >> n >> t, t) {
    r(i, n) scanf("%d%d%d", &m[i], &l[i], &k[i]);
    r(i, M) {
      if (ch(i)) {
        cout << i << endl;
        break;
      }
    }
  }
}