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

#include <cstdio>
#include <iostream>
#include <string.h>
#define MAX_N 101
#define INF 10000000
using namespace std;
int d[MAX_N][MAX_N];
int n, k;

void warshall_floyd(int a, int b) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d[j][i] = d[i][j] = min(d[i][j], d[i][a] + d[a][b] + d[b][j]);
    }
  }
}

int main() {

  while (cin >> n >> k, n + k) {

    n++;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          d[j][i] = 0;
        else
          d[j][i] = INF;
      }
    }
    for (int i = 0; i < k; i++) {
      int a, b, cost;
      cin >> a;
      if (a == 0) {
        cin >> a >> b;
        if (d[a][b] < INF)
          cout << d[a][b] << endl;
        else
          cout << -1 << endl;

      } else if (a == 1) {

        cin >> a >> b >> cost;

        if (d[a][b] > cost) {
          d[a][b] = d[b][a] = cost;
          warshall_floyd(a, b);
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string.h>
#define MAX_N 100
#define INF 10000000
using namespace std;
int d[MAX_N][MAX_N];
int n, k;

void warshall_floyd() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

int main() {

  while (cin >> n >> k, n + k) {

    // INFで初期化
    n++;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          d[j][i] = 0;
        else
          d[j][i] = INF;
      }
    }
    for (int i = 0; i < k; i++) {
      int a, b, cost;
      cin >> a;
      if (a == 0) {
        warshall_floyd();
        cin >> a >> b;
        if (d[a][b] != INF)
          cout << d[a][b] << endl;
        else
          cout << -1 << endl;
      } else if (a == 1) {
        cin >> a >> b >> cost;
        if (d[a][b] > cost)
          d[a][b] = d[b][a] = cost;
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<vector<int>> cost(n, vector<int>(n));
    rep(i, n) rep(j, n) cost[i][j] = inf;
    while (m--) {
      int a, b, c;
      cin >> a;
      if (a) {
        cin >> a >> b >> c;
        a--;
        b--;
        cost[a][b] = cost[b][a] = min(c, cost[a][b]);
      } else {
        cin >> b >> c;
        b--;
        c--;
        vector<int> d(n, inf);
        d[b] = 0;
        vector<bool> used(n);
        while (1) {
          int v = -1;
          rep(i, n) if (!used[i] && (v == -1 || d[v] > d[i])) v = i;
          if (v == -1)
            break;
          used[v] = true;
          rep(i, n) d[i] = min(d[i], d[v] + cost[v][i]);
        }
        if (d[c] == inf)
          cout << -1 << endl;
        else
          cout << d[c] << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
const int inf = 1e9;
using namespace std;
int main() {
  int d, n;
  while (cin >> d >> n, d || n) {
    vector<vector<int>> in(d, vector<int>(d));
    rep(i, d) rep(j, d) in[i][j] = inf;
    rep(i, n) {
      int a, b, c;
      cin >> a;
      if (a) {
        cin >> a >> b >> c;
        a--;
        b--;
        in[a][b] = min(in[a][b], c);
        in[b][a] = min(in[b][a], c);
      } else {
        cin >> a >> b;
        a--;
        b--;
        rep(k, d) rep(i, d) rep(j, d) in[i][j] =
            min(in[i][j], in[i][k] + in[k][j]);
        if (in[a][b] != inf)
          cout << in[a][b] << endl;
        else
          cout << -1 << endl;
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1000000000
int main() {

  while (true) {
    int n, k;
    cin >> n >> k;
    if (n == 0)
      break;
    int cost[100][100];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i != j)
          cost[i][j] = INF;
        else
          cost[i][j] = 0;
      }
    }
    int flg = false;
    for (int i = 0; i < k; i++) {
      int a, b, c, d, e, f;
      cin >> f;
      if (!f) {
        if (flg) {
          for (int i1 = 0; i1 < n; i1++) {
            for (int j = 0; j < n; j++) {
              for (int k = 0; k < n; k++) {
                cost[j][k] = min(cost[j][k], cost[j][i1] + cost[i1][k]);
              }
            }
          }
        }
        flg = false;
        cin >> a >> b;
        if (cost[a - 1][b - 1] == INF) {
          cout << -1 << endl;
        } else {
          cout << cost[a - 1][b - 1] << endl;
        }
      } else {
        cin >> c >> d >> e;
        if (cost[d - 1][c - 1] > e) {
          flg = true;
          cost[d - 1][c - 1] = cost[c - 1][d - 1] = e;
        }
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1000000000
int main() {

  while (true) {
    int n, k;
    cin >> n >> k;
    if (n == 0)
      break;
    int cost[100][100];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i != j)
          cost[i][j] = INF;
        else
          cost[i][j] = 0;
      }
    }
    for (int i = 0; i < k; i++) {
      int a, b, c, d, e, f;
      cin >> f;
      if (!f) {
        cin >> a >> b;
        a--;
        b--;
        bool check[n];
        for (int j = 0; j < n; j++) {
          check[j] = false;
        }
        check[a] = true;
        int nowpoint = a;
        priority_queue<pair<int, int>, vector<pair<int, int>>,
                       greater<pair<int, int>>>
            q; // cost,to
        bool flg = false;
        pair<int, int> num;
        num.first = INF;
        num.second = a;
        q.push(num);
        while (!q.empty()) {
          for (int j = 0; j < n; j++) {
            pair<int, int> tmp;
            tmp.first = cost[nowpoint][j] + cost[a][nowpoint];
            tmp.second = j;
            if (check[j])
              continue;
            if (tmp.first >= INF)
              continue;
            q.push(tmp);
          }
          pair<int, int> tmp = q.top();
          q.pop();
          if (check[tmp.second])
            continue;
          cost[a][tmp.second] = tmp.first;
          nowpoint = tmp.second;
          check[nowpoint] = true;
          if (nowpoint == b) {
            cout << tmp.first << endl;
            flg = true;
            break;
          }
        }
        if (!flg)
          cout << -1 << endl;
      } else {
        cin >> c >> d >> e;
        cost[c - 1][d - 1] = cost[d - 1][c - 1] = min(cost[c - 1][d - 1], e);
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define fi first
#define se second
static const int INF = 1e8;

int main(void) {
  while (1) {
    int n, k;
    scanf("%d %d", &n, &k);
    if (n == 0 && k == 0)
      return 0;
    int dist[101][101];
    rep(i, 101) rep(j, 101) {
      if (i == j)
        dist[i][j] = 0;
      else
        dist[i][j] = INF;
    }

    int flag, a, b, c, d, e;
    //????????????????????£???????????????????????¶??????????§??????????
    int kousinnflag =
        false; //?????????????????¢????????????????????´??°??????????????¨?????????????????°
    rep(i, k) {
      scanf("%d", &flag);
      if (flag == 0) {
        scanf("%d %d", &a, &b);
        a--;
        b--;
        //???????????£?????????????????§?????????????????´??°?????????????????????????????£???????????????????¨??????????????????????????????¢?????´??°????????????????????°?¨????????????????????????????
        if (kousinnflag == true) {
          rep(m, n) rep(l, n) rep(j, n) dist[l][j] =
              min(dist[l][j], dist[l][m] + dist[m][j]);
          kousinnflag = false;
        }

        if (dist[a][b] >= 1e8)
          printf("-1\\n"); //?????????????????¨???
        else
          printf("%d\\n", dist[a][b]);
      } else {
        scanf("%d %d %d", &c, &d, &e);
        c--;
        d--;
        if (dist[c][d] > e) {
          dist[c][d] = dist[d][c] = e; //????????????????????????????????°??´??°
          kousinnflag = true;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define fi first
#define se second
static const int INF = 1e8;

int main(void) {
  while (1) {
    int n, k;
    scanf("%d %d", &n, &k);
    if (n == 0 && k == 0)
      return 0;
    int dist[101][101];
    rep(i, 101) rep(j, 101) {
      if (i == j)
        dist[i][j] = 0;
      else
        dist[i][j] = INF;
    }

    int flag, a, b, c, d, e;
    rep(i, k) {
      scanf("%d", &flag);
      if (flag == 0) {
        scanf("%d %d", &a, &b);
        a--;
        b--;
        rep(m, n) rep(l, n) rep(j, n) dist[l][j] =
            min(dist[l][j], dist[l][m] + dist[m][j]);
        if (dist[a][b] >= 1e8)
          printf("-1\\n");
        else
          printf("%d\\n", dist[a][b]);
      } else {
        scanf("%d %d %d", &c, &d, &e);
        c--;
        d--;
        // printf("c %d d %d e %d \\n", c, d, e);
        if (dist[c][d] > e)
          dist[c][d] = dist[d][c] = e;
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#define INFTY 9000000000000000000
#define MAX 105
#define BLACK 2
#define WHITE 0
#define GRAY 1

long long n, M[105][105];
long long dijkstra(long long a, long long b) {
  long long minv;
  long long d[MAX], color[MAX];
  for (int i = 1; i <= n; i++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }
  d[a] = 0;
  color[a] = GRAY;
  while (1) {
    minv = INFTY;
    int u = -1;
    for (int i = 1; i <= n; i++) {
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 1; v <= n; v++) {
      if (color[v] != BLACK && M[u][v] != INFTY) {
        if (d[v] > d[u] + M[u][v]) {
          d[v] = d[u] + M[u][v];
          color[v] = GRAY;
        }
      }
    }
  }
  if (d[b] == INFTY)
    return -1;
  else
    return d[b];
}
int main() {
  while (1) {
    int k;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        M[i][j] = INFTY;
    for (int i = 0; i < k; i++) {
      int whi;
      cin >> whi;
      if (whi == 0) {
        long long a, b, ans;
        cin >> a >> b;
        ans = dijkstra(a, b);
        cout << ans << endl;
      } else if (whi == 1) {
        long long c, d, e;
        cin >> c >> d >> e;
        M[c][d] = min(e, M[c][d]);
        M[d][c] = M[c][d];
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
static const int INFTY = (1 << 21);
using namespace std;
int D[105][105];

int n;
void floyd() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      if (D[i][k] == INFTY)
        continue;
      for (int j = 1; j <= n; j++) {
        if (D[k][j] == INFTY)
          continue;
        D[i][j] = min(D[i][j], D[i][k] + D[k][j]);
        D[j][i] = D[i][j];
      }
    }
  }
}
int main() {
  int k;
  while (1) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    // memset(D, INFTY, sizeof(D));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++)
        D[i][j] = INFTY;
    }
    int a, b, c, d;
    for (int i = 0; i < k; i++) {
      cin >> a >> b >> c;
      if (a == 0)
        cout << ((D[b][c] == INFTY) ? -1 : D[b][c]) << endl;
      else if (a == 1) {
        cin >> d;
        D[b][c] = min(D[b][c], d);
        D[c][b] = D[b][c];
        // cout<<"D["<<b<<"]["<<c<<"] = "<<D[b][c]<<endl;
        floyd();
      }
    }
  }
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define Rep(i, N) for (int i = 0; i < N; i++)
#define F first
#define S second
typedef pair<int, int> Pi;
#define INF 1 << 29
#define pb push_back

inline void chmin(int &a, int b) { a = min(a, b); }

inline void chmax(int &a, int b) { a = max(a, b); }

int main() {
  int N, K;
  while (cin >> N >> K, N || K) {
    int Q, A, B, C;
    int path[105][105];
    fill(path[0], path[105], INF);
    Rep(i, 105) { path[i][i] = 0; }
    while (K--) {
      cin >> Q >> A >> B;
      --A, --B;
      if (Q) {
        cin >> C;
        chmin(path[A][B], C);
        chmin(path[B][A], C);
        Rep(i, N) Rep(j, N) {
          chmin(path[i][j], min(path[i][A] + path[A][B] + path[B][j],
                                path[i][B] + path[B][A] + path[B][j]));
          path[j][i] = path[i][j];
        }
      } else {
        if (path[A][B] == INF) {
          cout << -1 << endl;
        } else {
          cout << path[A][B] << endl;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define Rep(i, N) for (int i = 0; i < N; i++)
#define F first
#define S second
typedef pair<int, int> Pi;
#define INF 1 << 28
#define pb push_back

inline void chmin(int &a, int b) { a = min(a, b); }

inline void chmax(int &a, int b) { a = max(a, b); }

struct Edge {
  int to, cost;
};

int mc[105];
int N, K;

int Dijkstra(vector<Edge> *G, int s, int g) {
  priority_queue<Pi> pq;
  fill(mc, mc + N, INF);
  mc[s] = 0;
  pq.push(Pi(0, s));
  while (!pq.empty()) {
    int c = pq.top().F;
    int v = pq.top().S;
    pq.pop();
    if (mc[v] < c)
      continue;
    Rep(i, G[v].size()) {
      int u = G[v][i].to;
      int nc = G[v][i].cost + c;
      if (nc < mc[u]) {
        mc[u] = nc;
        pq.push(Pi(nc, u));
      }
    }
  }
  if (mc[g] == INF)
    return -1;
  return mc[g];
}

int main() {
  while (cin >> N >> K, N || K) {
    int Q, A, B, C;
    vector<Edge> G[105];
    while (K--) {
      cin >> Q >> A >> B;
      --A, --B;
      if (Q) {
        cin >> C;
        G[A].pb((Edge){B, C});
        G[B].pb((Edge){A, C});
      } else {
        cout << Dijkstra(G, A, B) << endl;
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define INF 1000000000
using namespace std;
int dis[5010][5010];
int gedi(int x, int y) {
  if (x > y)
    return dis[y][x];
  else
    return dis[x][y];
}

void sedi(int x, int y, int v) {
  if (x > y)
    dis[y][x] = v;
  else
    dis[x][y] = v;
}
int N;
void wf() {
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = i; j < N; j++) {
        sedi(i, j, min(gedi(i, j), gedi(i, k) + gedi(k, j)));
      }
    }
  }
}

int main() {
  int K, hog, bak, aho, kso;
  while (1) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        dis[i][j] = INF;
      }
    }
    for (int i = 0; i < K; i++) {
      ////
      // for(int i=0;i<N;i++){
      //	for(int j=0;j<N;j++){
      //		cout<<"dis["<<i<<"]["<<j<<"]="<<gedi(i,j)<<" ";
      //	}
      //	cout<<endl;
      // }
      ////
      cin >> hog;
      if (hog == 0) {
        cin >> bak >> aho;
        bak--;
        aho--;
        if (gedi(bak, aho) != INF) {
          cout << gedi(bak, aho) << endl;
        } else {
          cout << -1 << endl;
        }
      } else {
        cin >> bak >> aho >> kso;
        bak--;
        aho--;
        if (gedi(bak, aho) > kso) {
          sedi(bak, aho, kso);
          wf();
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define INF 1000000000
using namespace std;
int dis[5010][5010];
int gedi(int x, int y) {
  if (x > y)
    return dis[y][x];
  else
    return dis[x][y];
}

void sedi(int x, int y, int v) {
  if (x > y)
    dis[y][x] = v;
  else
    dis[x][y] = v;
}
int N;
void wf() {
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        sedi(i, j, min(gedi(i, j), gedi(i, k) + gedi(k, j)));
      }
    }
  }
}

int main() {
  int K, hog, bak, aho, kso;
  while (1) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        dis[i][j] = INF;
      }
    }
    for (int i = 0; i < K; i++) {
      ////
      // for(int i=0;i<N;i++){
      //	for(int j=0;j<N;j++){
      //		cout<<"dis["<<i<<"]["<<j<<"]="<<gedi(i,j)<<" ";
      //	}
      //	cout<<endl;
      // }
      ////
      cin >> hog;
      if (hog == 0) {
        cin >> bak >> aho;
        bak--;
        aho--;
        if (gedi(bak, aho) != INF) {
          cout << gedi(bak, aho) << endl;
        } else {
          cout << -1 << endl;
        }
      } else {
        cin >> bak >> aho >> kso;
        bak--;
        aho--;
        if (gedi(bak, aho) > kso) {
          sedi(bak, aho, kso);
          wf();
        }
      }
    }
  }
  return 0;
}
Problem p00449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define INF 1000000000
int main() {
  int n, k;
  while (cin >> n >> k, n + k) {
    int d[110][110];
    for (int i = 0; i < 110; i++)
      for (int j = 0; j < 110; j++)
        d[i][j] = i == j ? 0 : INF;
    for (int i = 0; i < k; i++) {
      int a;
      cin >> a;
      int from, to, cost;
      if (a == 1) {
        cin >> from >> to >> cost;
        from--;
        to--;
        if (d[from][to] > cost) {
          d[from][to] = d[to][from] = cost;
          for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
              // from to を経由する
              d[i][j] = min(d[i][j], d[i][from] + d[from][to] + d[to][j]);
              d[i][j] = min(d[i][j], d[i][to] + d[to][from] + d[from][j]);
              d[j][i] = d[i][j];
            }
          }
        }
      } else {
        cin >> from >> to;
        from--;
        to--;
        if (d[from][to] == INF)
          puts("-1");
        else
          cout << d[from][to] << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define INF 1000000000
int main() {
  int n, k;
  while (cin >> n >> k, n + k) {
    int d[110][110];
    for (int i = 0; i < 110; i++)
      for (int j = 0; j < 110; j++)
        d[i][j] = INF;
    for (int i = 0; i < k; i++) {
      int a;
      cin >> a;
      int from, to, cost;
      if (a == 1) {
        cin >> from >> to >> cost;
        from--;
        to--;
        d[from][to] = cost;
        d[to][from] = cost;
        for (int k = 0; k < n; k++) {
          for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
              d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
          }
        }
      } else {
        cin >> from >> to;
        from--;
        to--;
        if (d[from][to] == INF)
          puts("-1");
        else
          cout << d[from][to] << endl;
      }
    }
  }
  return 0;
}
Problem p00450 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <map>
#include <stack>
#define f first
#define s second
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> pp;
// 0=white, 1=black
int main() {
  int n;
  while (scanf("%d", &n)) {
    if (!n)
      break;
    stack<pp> list;
    int in;
    scanf("%d", &in);
    list.push(make_pair(in, make_pair(0, 1)));
    for (int i = 1; i < n; i++) {
      scanf("%d", &in);
      if (i % 2 != 0) {
        pp bef = list.top();
        list.pop();
        // printf("%d %d %d\\n", bef.f, bef.s.f, bef.s.s);
        if (bef.f == in) {
          bef.s.s++;
          list.push(bef);
        } else {
          if (list.empty()) {
            bef.f = in;
            bef.s.s++;
            list.push(bef);
          } else {
            pp bef2 = list.top();
            list.pop();
            bef2.s.s = bef.s.s + 1;
            list.push(bef2);
          }
        }
      } else {
        pp bef = list.top();
        // printf("%d %d %d\\n", bef.f, bef.s.f, bef.s.s);
        if (bef.f == in) {
          list.pop();
          bef.s.s++;
          list.push(bef);
        } else {
          list.push(make_pair(in, make_pair(i, i + 1)));
        }
      }
    }
    int res = 0;
    while (!list.empty()) {
      pp x = list.top();
      list.pop();
      // printf("%d %d %d\\n",x.f,x.s.f,x.s.s);
      if (!x.f)
        res += x.s.s - x.s.f;
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
int main() {
  int n;
  while (scanf("%d", &n)) {
    if (n == 0)
      break;
    int stat[100000];
    for (int i = 0; i < n; i++) {
      if (i % 2 == 0) {
        scanf("%d", &stat[i]);
      } else {
        scanf("%d", &stat[i]);
        if (stat[i] != stat[i - 1]) {
          int j = i - 1;
          while (stat[j] != stat[i] && j >= 0) {
            stat[j--] = stat[i];
          }
        }
      }
    }
    int res = 0;
    for (int i = 0; i < n; i++)
      if (!stat[i])
        res++;
    printf("%d\\n", res);
  }
}
Problem p00450 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  while (cin >> n, n) {
    int a;
    stack<pair<int, int>> st;

    cin >> a;
    st.emplace(a, 1);
    for (int i = 2; i <= n; i++) {
      cin >> a;
      if (i % 2) {
        auto &po = st.top();
        if (po.first == a) {
          po.second++;
        } else {
          st.emplace(a, 1);
        }
      } else {
        auto po = st.top();
        st.pop();
        if (po.first == a) {
          po.second++;
          st.emplace(po);
        } else {
          if (st.empty()) {
            st.emplace(a, po.second + 1);
          } else {
            st.top().second += 1 + po.second;
          }
        }
      }
    }

    int cnt = 0;
    while (!st.empty()) {
      auto po = st.top();
      st.pop();
      if (po.first)
        continue;
      cnt += po.second;
    }

    cout << cnt << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    int cnt = 0;
    for (int i = 1; i < n; i++) {
      if (i % 2 == 0)
        continue;
      if (a[i] != a[i - 1]) {
        for (int j = i; a[j] != a[j - 1]; j--) {
          a[j - 1] = a[j];
          if (j == 0)
            break;
        }
      }
    }

    for (int i = 0; i < n; i++) {
      if (a[i] == 0)
        cnt++;
    }

    cout << cnt << endl;
  }
}