alexjercan/bugnet huggingface dataset:
Problem p00859 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int N, M;
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
};

struct uni {
  int d[111];
  void init() { memset(d, -1, sizeof(d)); }
  int find(int a) { return d[a] < 0 ? a : (d[a] = find(d[a])); }
  void marge(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b)
      return;
    if (d[a] > d[b])
      swap(a, b);
    d[a] += d[b];
    d[b] = a;
  }
  bool same(int a, int b) { return find(a) == find(b); }
  int size(int a) { return -d[find(a)]; }
};
#define INF (1 << 29)
edge E[11111];
edge Es[11111];
uni U;
int main() {
  while (cin >> N >> M && (N || M)) {
    for (int i = 0; i < M; i++) {
      int a, b, w;
      cin >> a >> b >> w;
      E[i] = (edge){a, b, w};
    }
    sort(E, E + M);
    int resu = INF;
    for (int i = 0; i < M; i++) {
      U.init();
      int k, id;
      for (id = 0, k = i; k < M; id++, k++) {
        Es[id] = (edge){E[k].from, E[k].to, abs(E[i].cost - E[k].cost)};
      }
      // cout << id << endl;
      int res = 0;
      for (int j = 0; j < id; j++) {
        if (U.same(Es[j].from, Es[j].to))
          continue;
        // cout << Es[j].from << " " << Es[j].to << " "<< Es[j].cost << endl;
        U.marge(Es[j].from, Es[j].to);
        res = max(res, Es[j].cost);
      }
      if (U.size(1) == N)
        resu = min(res, resu);
    }
    if (resu == INF)
      cout << -1 << endl;
    else
      cout << resu << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, M;
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
};

struct uni {
  int d[111];
  void init() { memset(d, -1, sizeof(d)); }
  int find(int a) { return d[a] < 0 ? a : (d[a] = find(d[a])); }
  void marge(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b)
      return;
    if (d[a] > d[b])
      swap(a, b);
    d[a] += d[b];
    d[b] = a;
  }
  bool same(int a, int b) { return find(a) == find(b); }
  int size(int a) { return -d[find(a)]; }
};
#define INF (1 << 29)
edge E[111];
edge Es[111];
uni U;
int main() {
  while (cin >> N >> M && (N || M)) {
    for (int i = 0; i < M; i++) {
      int a, b, w;
      cin >> a >> b >> w;
      E[i] = (edge){a, b, w};
    }
    sort(E, E + M);
    int resu = INF;
    for (int i = 0; i < M; i++) {
      U.init();
      int k, id;
      Es[0] = (edge){E[i].from, E[i].to, abs(E[i].cost - E[i].cost)};
      ;
      for (id = 1, k = i + 1; k < M; id++) {
        Es[id] = (edge){E[k].from, E[k].to, abs(E[i].cost - E[k].cost)};
        k++;
      }
      // cout << id << endl;
      int res = 0;
      for (int j = 0; j < id; j++) {
        if (U.same(Es[j].from, Es[j].to))
          continue;
        // cout << Es[j].from << " " << Es[j].to << " "<< Es[j].cost << endl;
        U.marge(Es[j].from, Es[j].to);
        res = max(res, Es[j].cost);
      }
      if (U.size(1) == N)
        resu = min(res, resu);
    }
    if (resu == INF)
      cout << -1 << endl;
    else
      cout << resu << endl;
  }
}
Problem p00859 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int f[120][120], s[10000], sp, dis[120];
bool b[120];
const int maxdis = 1000000000;
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) == 2) {
    if (n == 0 && m == 0)
      break;
    int i, j, k;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++)
        f[i][j] = 0;
    }
    int v1, v2, e;
    sp = 0;
    for (i = 0; i < m; i++)
      scanf("%d%d%d", &v1, &v2, &e), f[v1][v2] = e, f[v2][v1] = e, s[sp++] = e;
    sort(s, s + sp);
    int diff = maxdis;
    for (i = 0; i < sp; i++) {
      if (i && s[i] == s[i - 1])
        continue;
      bool bb = 1;
      for (j = 2; j <= n; j++)
        b[j] = 0;
      b[1] = 1;
      for (j = 2; j <= n; j++) {
        if (f[1][j] >= s[i])
          dis[j] = f[1][j];
        else
          dis[j] = maxdis;
      }
      int minind, mindis, maxd = 0, mind = maxdis;
      for (j = 2; j <= n; j++) {
        mindis = maxdis;
        for (k = 1; k <= n; k++) {
          if (b[k])
            continue;
          if (mindis > dis[k] && dis[k] >= s[i])
            mindis = dis[k], minind = k;
        }
        if (mindis == maxdis) {
          bb = 0;
          break;
        }
        if (maxd < mindis)
          maxd = mindis;
        if (mind > mindis)
          mind = mindis;
        b[minind] = 1;
        for (k = 1; k <= n; k++) {
          if (b[k])
            continue;
          if (dis[k] > f[k][minind] && f[k][minind] >= s[i])
            dis[k] = f[k][minind];
        }
      }
      if (!bb)
        break;
      if (diff > maxd - mind)
        diff = maxd - mind;
    }
    if (!i)
      puts("-1");
    else
      printf("%d\\n", diff);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 9999999
int n, m, fa[505], r[505];
struct PP {
  int a, b;
  double len;
  friend bool operator<(PP x, PP y) { return x.len < y.len; }
} edges[500005];

int find_(int x) {
  if (x != fa[x])
    fa[x] = find_(fa[x]);
  return fa[x];
}

void union_(int a, int b) {
  a = find_(a);
  b = find_(b);
  if (r[a] > r[b])
    swap(a, b);
  if (r[a] == r[b])
    r[a]++;
  fa[a] = b;
}

int kruskal(int i) {
  int en = i, cnt = 0, tot = 0;
  while (cnt < n - 1) {
    PP e = edges[en];
    en++;
    if (find_(e.a) != find_(e.b)) {
      union_(e.a, e.b);
      tot = e.len;
      cnt++;
    }
  }
  return tot - edges[i].len;
}

int main() {
  int min_ = INF;
  int a, b, len;
  while (~scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    memset(edges, 0, sizeof(edges));
    min_ = INF;
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d", &a, &b, &len);
      edges[i].a = a;
      edges[i].b = b;
      edges[i].len = len;
    }
    sort(edges + 1, edges + 1 + m);
    for (int i = 1; i <= m + 2 - n; i++) {
      memset(r, 0, sizeof(r));
      for (int j = 1; j <= n; j++) {
        fa[j] = j;
      }
      int tmp = kruskal(i);
      min_ = min(tmp, min_);
    }
    if (min_ == INF)
      min_ = -1;
    printf("%d\\n", min_);
  }

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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

const int INF = 1e8;

const int MAX_V = 105;
const int MAX_C = 10005;

struct Edge {
  int to, cost;
};

int M[MAX_V][MAX_V];

bool C(int n, int low, int high) {
  bool used[MAX_V] = {0};
  queue<int> q; //?¨??????????????????\\??????

  q.push(0);
  used[0] = true;

  int u, cnt = 1;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    rep(v, n) {
      if (M[u][v] >= low && M[u][v] <= high && used[v] == false) {
        used[v] = true;
        cnt++;
        q.push(v);
      }
    }
  }
  return n == cnt;
}

int binarySearch(int n, int low, int &ans) {
  int right = MAX_C, left = low;
  rep(i, 20) {
    int mid = (right + left) / 2;
    if (C(n, low, mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    memset(M, -1, sizeof(M));
    vector<int> v;
    rep(i, m) {
      int a, b, w;
      cin >> a >> b >> w;
      a--;
      b--;
      M[a][b] = M[b][a] = w;
      v.emplace_back(w);
    }
    sort(all(v));

    int ans = INF;
    rep(i, v.size()) {
      int high = binarySearch(n, v[i], ans);
      if (C(n, v[i], high))
        ans = min(ans, high - v[i]);
      else
        break;
    }

    // int ans = INF;
    cout << (ans == INF ? -1 : ans) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

const int INF = 1e8;

const int MAX_V = 105;
const int MAX_C = 10005;

struct Edge {
  int to, cost;
};

int M[MAX_V][MAX_V];

bool C(int n, int low, int high) {
  bool used[MAX_V] = {0};
  queue<int> q; //?¨??????????????????\\??????

  q.push(0);

  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    used[u] = true;
    rep(v, n) {
      if (M[u][v] >= low && M[u][v] <= high && used[v] == false) {
        q.push(v);
      }
    }
  }
  rep(i, n) {
    if (not used[i])
      return 0;
  }
  return 1;
}

int binarySearch(int n, int low, int &ans) {
  int right = MAX_C, left = low;
  rep(i, 100) {
    int mid = (right + left) / 2;
    if (C(n, low, mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    memset(M, -1, sizeof(M));
    vector<int> v;
    rep(i, m) {
      int a, b, w;
      cin >> a >> b >> w;
      a--;
      b--;
      M[a][b] = M[b][a] = w;
      v.emplace_back(w);
    }

    int ans = INF;
    rep(i, v.size()) {
      int high = binarySearch(n, v[i], ans);
      if (C(n, v[i], high))
        ans = min(ans, high - v[i]);
    }

    // int ans = INF;
    cout << (ans == INF ? -1 : ans) << endl;
  }
}
Problem p00859 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

struct Edge {
  int to, w;
};

typedef vector<vector<Edge>> Graph;

#define INF 1e9

bool f(Graph &G, int m, int M) {
  vector<bool> flag(G.size(), false);
  stack<int> S;
  S.push(0);
  int cnt = 0;
  while (!S.empty()) {
    int n = S.top();
    S.pop();

    if (flag[n])
      continue;
    flag[n] = true;
    ++cnt;

    for (int i = 0; i < G[n].size(); ++i) {
      Edge &e = G[n][i];
      if (m <= e.w && e.w <= M) {
        S.push(e.to);
      }
    }
  }
  return cnt == G.size();
}

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    Graph G(N);
    vector<int> v;
    for (int i = 0; i < M; ++i) {
      int a, b, w;
      cin >> a >> b >> w;
      --a, --b;
      G[a].push_back((Edge){b, w});
      G[b].push_back((Edge){a, w});
      v.push_back(w);
    }

    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    int ans = -1;
    for (int i = 0; i < v.size(); ++i) {
      int l = i - 1, r = v.size() - 1; // (l, r]
      while (l < r - 1) {
        int m = (l + r) / 2;
        if (f(G, v[i], v[m])) {
          r = m;
        } else {
          l = m;
        }
      }

      int m = v[i], M = v[r];
      if ((ans < 0 || M - m < ans) && f(G, m, M))
        ans = M - m;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

struct Edge {
  int to, w;
};

typedef vector<vector<Edge>> Graph;

#define INF 1e9

bool f(Graph &G, int m, int M) {
  vector<bool> flag(G.size(), false);
  stack<int> S;
  S.push(0);
  int cnt = 0;
  while (!S.empty()) {
    int n = S.top();
    S.pop();

    if (flag[n])
      continue;
    flag[n] = true;
    ++cnt;

    for (int i = 0; i < G[n].size(); ++i) {
      Edge &e = G[n][i];
      if (m <= e.w && e.w <= M) {
        S.push(e.to);
      }
    }
  }
  return cnt == G.size();
}

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    Graph G(N);
    vector<int> v;
    for (int i = 0; i < M; ++i) {
      int a, b, w;
      cin >> a >> b >> w;
      --a, --b;
      G[a].push_back((Edge){b, w});
      G[b].push_back((Edge){a, w});
      v.push_back(w);
    }

    sort(v.begin(), v.end());

    int ans = -1;
    for (int i = 0; i < v.size(); ++i) {
      for (int j = i; j < v.size(); ++j) {
        int m = v[i], M = v[j];
        if (f(G, m, M)) {
          if (ans < 0 || M - m < ans)
            ans = M - m;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00859 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

const int INF = 1000000000;
const int maxn = 100 + 10;
int pa[maxn];

int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); }

struct Edge {
  int u, v, w;
  Edge(int u, int v, int w) : u(u), v(v), w(w) {}
  bool operator<(const Edge &rhs) const { return w < rhs.w; }
};

vector<Edge> G;

int main() {
  // freopen("in.txt", "r", stdin);
  int n, m;
  while (scanf("%d%d", &n, &m) == 2 && n) {
    G.clear();
    int u, v, w;
    for (int i = 0; i < m; ++i) {
      scanf("%d%d%d", &u, &v, &w);
      G.push_back(Edge(u, v, w));
    }
    sort(G.begin(), G.end());
    int ans = INF;
    for (int L = 0; L <= m - n + 1; ++L) {
      for (int i = 1; i <= n; ++i)
        pa[i] = i;
      int cnt = n;
      for (int R = L; R < m; ++R) {
        int u = findset(G[R].u), v = findset(G[R].v);
        if (u != v) {
          pa[u] = v;
          if (--cnt == 1) {
            ans = min(ans, G[R].w - G[L].w);
          }
        }
      }
    }
    if (ans == INF)
      ans = -1;

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

  return 0;
}

Error: 0

Fixed submission:

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

const int maxn = 100 + 5;
const int INF = 0x3f3f3f3f;

struct graph {
  int a, b, w;
} g[maxn];

int n, m, group[maxn];

int Find(int v) { return group[v] = (group[v] == v) ? v : Find(group[v]); }

void Union(int a, int b) { group[Find(a)] = Find(b); }

bool cmp(const graph &a, const graph &b) { return a.w < b.w; }

int main() {
  while (scanf("%d%d", &n, &m) && (n || m)) {
    bool ok = false;
    int mini = INF;

    for (int i = 0; i < m; i++)
      scanf("%d%d%d", &g[i].a, &g[i].b, &g[i].w);

    sort(g, g + m, cmp);
    for (int l = 0; l + n - 2 < m; l++) {
      for (int i = 1; i <= n; i++)
        group[i] = i;
      int r = l, cnt = 0;
      while (r < m) {
        int a = g[r].a, b = g[r].b;
        if (Find(a) != Find(b)) {
          Union(a, b);
          cnt++;
        }
        if (cnt == n - 1) {
          ok = true;
          break;
        }
        r++;
      }
      mini = min(mini, g[r].w - g[l].w);
    }

    if (ok)
      printf("%d\\n", mini);
    else
      puts("-1");
  }

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

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

typedef pair<int, int> P;

int n, m;
int a[11000], b[11000], w[11000];
bool use[110];
vector<P> g[110];

int dfs(int x, int low, int up) {
  if (use[x])
    return 0;
  use[x] = true;

  int res = 0;
  for (int i = 0; i < g[x].size(); i++) {
    if (g[x][i].first >= low && g[x][i].first <= up)
      res += dfs(g[x][i].second, low, up);
  }
  return res + 1;
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; i++)
      g[i].clear();
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i] >> w[i];
      a[i]--;
      b[i]--;
      g[a[i]].push_back(P(w[i], b[i]));
      g[b[i]].push_back(P(w[i], a[i]));
    }

    int l = -1, r = 10001;
    for (int i = 0; i < m; i++) {
      while (r - l > 1) {
        int mid = (l + r) / 2;
        for (int j = 0; j < n; j++)
          use[j] = false;
        int tmp = dfs(a[i], w[i] - mid, w[i]);
        if (tmp == n)
          r = mid;
        else
          l = mid;
      }
      l = -1;
    }
    if (r == 10001)
      cout << -1 << endl;
    else
      cout << r << endl;
  }
}


Error: 0

Fixed submission:

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

typedef pair<int, int> P;

int n, m;
int a[110], b[110], w[110];
bool use[110];
vector<P> g[110];

int dfs(int x, int low, int up) {
  if (use[x])
    return 0;
  use[x] = true;

  int res = 0;
  for (int i = 0; i < g[x].size(); i++) {
    if (g[x][i].first >= low && g[x][i].first <= up)
      res += dfs(g[x][i].second, low, up);
  }
  return res + 1;
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; i++)
      g[i].clear();
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i] >> w[i];
      a[i]--;
      b[i]--;
      g[a[i]].push_back(P(w[i], b[i]));
      g[b[i]].push_back(P(w[i], a[i]));
    }

    int l = -1, r = 10001;
    for (int i = 0; i < m; i++) {
      while (r - l > 1) {
        int mid = (l + r) / 2;
        for (int j = 0; j < n; j++)
          use[j] = false;
        int tmp = dfs(a[i], w[i] - mid, w[i]);
        if (tmp == n)
          r = mid;
        else
          l = mid;
      }
      l = -1;
    }
    if (r == 10001)
      cout << -1 << endl;
    else
      cout << r << endl;
  }
}
Problem p00861 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

map<char, int> declaration;
map<pair<char, int>, int> assignment;

int expression(string &s, int &idx, int now) {
  char array_name = s[idx];
  if (isalpha(array_name)) {
    ++idx;
    ++idx;
    int get = expression(s, idx, now);
    if (declaration[array_name] <= get)
      throw(now);
    if (!assignment.count({array_name, get}))
      throw(now);
    ++idx;
    return (assignment[{array_name, get}]);
  } else {
    int ret = 0;
    while (isdigit(s[idx])) {
      ret = ret * 10 - '0' + s[idx++];
    }
    return (ret);
  }
}

void exec(string &s, int now) {
  char array_name = s[0];

  if (s.find('=') == string::npos) {
    int number = 0, ptr = 2;
    while (isdigit(s[ptr])) {
      number = number * 10 - '0' + s[ptr++];
    }

    declaration.emplace(array_name, number);
  } else {
    int ptr = 2;
    int number = expression(s, ptr, now);
    if (declaration[array_name] <= number)
      throw(now);
    ++ptr;
    ++ptr;
    int get = expression(s, ptr, now);
    assignment[{array_name, number}] = get;
  }
}

int main() {
  string S[1005];
  while (cin >> S[0], S[0] != ".") {
    for (int i = 1; S[i - 1] != "."; ++i) {
      cin >> S[i];
    }

    try {
      for (int i = 0; S[i] != "."; i++) {
        exec(S[i], i + 1);
      }
      throw 0;
    } catch (int x) {
      cout << x << endl;
    }

    declaration.clear();
    assignment.clear();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

map<char, int> declaration;
map<pair<char, int>, int> assignment;

int expression(string &s, int &idx, int now) {
  char array_name = s[idx];
  if (isalpha(array_name)) {
    ++idx;
    ++idx;
    int get = expression(s, idx, now);
    if (declaration[array_name] <= get)
      throw(now);
    if (!assignment.count({array_name, get}))
      throw(now);
    ++idx;
    return (assignment[{array_name, get}]);
  } else {
    int ret = 0;
    while (isdigit(s[idx])) {
      ret = ret * 10 - '0' + s[idx++];
    }
    return (ret);
  }
}

void exec(string &s, int now) {
  char array_name = s[0];

  if (s.find('=') == string::npos) {
    int number = 0, ptr = 2;
    while (isdigit(s[ptr])) {
      number = number * 10 - '0' + s[ptr++];
    }

    declaration.emplace(array_name, number);
  } else {
    int ptr = 2;
    int number = expression(s, ptr, now);
    if (declaration[array_name] <= number)
      throw(now);
    ++ptr;
    ++ptr;
    int get = expression(s, ptr, now);
    assignment[{array_name, number}] = get;
  }
}

int main() {
  string S[1000];
  while (cin >> S[0], S[0] != ".") {
    for (int i = 1; S[i - 1] != "."; ++i) {
      cin >> S[i];
    }

    try {
      for (int i = 0; S[i] != "."; i++) {
        exec(S[i], i + 1);
      }
      throw 0;
    } catch (int x) {
      cout << x << endl;
    }

    declaration.clear();
    assignment.clear();
  }
}
Problem p00861 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<string, ll> P;
map<P, ll> M;
map<string, ll> upper;
string s;
int p, flg;
ll program();

string getName() {
  string res;
  while (s[p] != '[')
    res += s[p++];
  return res;
}

ll getNum() {
  ll res = 0;
  while (isdigit(s[p]))
    res = res * 10 + s[p++] - '0';
  return res;
}

ll getIdx() {
  ll res = 0;
  if (isdigit(s[p]))
    res = getNum(), p++;
  else
    res = program(), p++;
  return res;
}

ll program() {
  if (isdigit(s[p]))
    return getNum();
  string name = getName();
  p++;
  int idx = getIdx();
  if (!M.count(P(name, idx)))
    flg = 0;
  return M[P(name, idx)];
}

void start() {
  flg = 1;
  p = 0;
  string name = getName();
  p++;
  ll idx = getIdx();
  ll cnt = upper.count(name);
  if ((cnt && idx > upper[name]) || (!cnt && p != s.size()))
    flg = 0;
  if (!cnt)
    upper[name] = idx - 1;
  if (p == s.size())
    return;
  p++;
  ll res = program();
  M[P(name, idx)] = res;
}

int main() {
  while (1) {
    int ans = 0;
    M.clear();
    upper.clear();
    for (int i = 1;; i++) {
      cin >> s;
      if (i == 1 && s == ".")
        return 0;
      if (s == ".")
        break;
      if (ans)
        continue;
      start();
      if (!flg)
        ans = i;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<string, ll> P;
map<P, ll> M;
map<string, ll> upper;
string s;
int p, flg;
ll program();

string getName() {
  string res;
  while (s[p] != '[')
    res += s[p++];
  return res;
}

ll getNum() {
  ll res = 0;
  while (isdigit(s[p]))
    res = res * 10 + s[p++] - '0';
  p++;
  return res;
}

ll getIdx() {
  ll res = 0;
  if (isdigit(s[p]))
    res = getNum(), p++;
  else
    res = program(), p++;
  return res;
}

ll program() {
  if (isdigit(s[p]))
    return getNum();
  string name = getName();
  int idx = getIdx();
  if (!M.count(P(name, idx)))
    flg = 0;
  return M[P(name, idx)];
}

void start() {
  flg = 1;
  p = 0;
  string name = getName();
  ll idx = getIdx(), cnt = upper.count(name);
  if ((cnt && idx > upper[name]) || (!cnt && p != s.size()))
    flg = 0;
  if (!cnt)
    upper[name] = idx - 1;
  if (p == s.size())
    return;
  p++;
  int res = program();
  M[P(name, idx)] = res;
}

int main() {
  while (1) {
    int ans = 0;
    M.clear();
    upper.clear();
    for (int i = 1;; i++) {
      cin >> s;
      if (i == 1 && s == ".")
        return 0;
      if (s == ".")
        break;
      if (ans)
        continue;
      start();
      if (!ans && !flg)
        ans = i;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00864 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int n, w;
int dat[101];
int maxi;
int l, r;
double calc() {
  double ans = 0;
  l = 0;
  for (int i = l; i <= r; ++i) {
    ans += ((double)(r - i) / (r - l)) * ((double)dat[i] / maxi);
  }
  return ans + 0.01;
}

int main() {
  while ((cin >> n >> w) && (n || w)) {
    fill(dat, dat + 101, 0);
    for (int i = 0; i < n; ++i) {
      int v;
      cin >> v;
      v /= w;
      ++dat[v];
      l = i == 0 ? v : min(l, v);
      r = i == 0 ? v : max(r, v);
      maxi = i == 0 ? dat[v] : max(maxi, dat[v]);
    }
    cout << calc() << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int n, w;
int dat[100];
int maxi;
int l, r;
double calc() {
  double ans = 0;
  l = 0;
  for (int i = l; i <= r; ++i) {
    ans += ((double)(r - i) / (r - l)) * ((double)dat[i] / maxi);
  }
  return ans + 0.01;
}

int main() {
  while ((cin >> n >> w) && (n || w)) {
    fill(dat, dat + 101, 0);
    for (int i = 0; i < n; ++i) {
      int v;
      cin >> v;
      v /= w;
      ++dat[v];
      l = i == 0 ? v : min(l, v);
      r = i == 0 ? v : max(r, v);
      maxi = i == 0 ? dat[v] : max(maxi, dat[v]);
    }
    cout << calc() << endl;
  }
  return 0;
}