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

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

using namespace std;

int p[10];
int field[10][10];
int a[10];

int main() {
  const int INF = 1000000000;
  int n;
  while (scanf("%d", &n), n != 0) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        scanf("%d", &field[i][j]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        field[i][j] = max(field[i][j], field[j][i]);
    for (int i = 0; i < 10; i++)
      a[i] = i;
    int minLength = INF;
    do {
      // »ÝÌÔÅÅáÀKvÈ·³
      int least = 0;
      for (int i = 1; i < n; i++)
        least += field[a[i]][a[i - 1]];
      if (least >= minLength)
        continue;
      // aÌÔÉ¨¢Ä¢­
      for (int i = 0; i < n; i++) {
        int rightPlace = 0;
        if (i == 0)
          rightPlace = 0;
        else {
          // ±±ðÇ¤üP·é©H
          for (int j = 0; j < i; j++)
            rightPlace = max(rightPlace, field[a[i]][a[j]] + p[j]);
        }
        p[i] = rightPlace;
        if (p[i] >= minLength)
          break;
      }
      if (minLength > p[n - 1])
        minLength = p[n - 1];
    } while (next_permutation(a, a + n));
    printf("%d\\n", minLength);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int p[10];
int field[10][10];
int a[10];

int main() {
  const int INF = 1000000000;
  int n;
  while (scanf("%d", &n), n != 0) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        scanf("%d", &field[i][j]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        field[i][j] = max(field[i][j], field[j][i]);
    for (int i = 0; i < 10; i++)
      a[i] = i;
    int minLength = INF;
    do {
      // »ÝÌÔÅÅáÀKvÈ·³
      int least[10];
      least[0] = 0;
      for (int i = 1; i < n; i++)
        least[i] = least[i - 1] + field[a[i]][a[i - 1]];
      if (least[n - 1] >= minLength) {
        continue;
      }
      // aÌÔÉ¨¢Ä¢­
      for (int i = 0; i < n; i++) {
        int rightPlace = 0;
        if (i == 0)
          rightPlace = 0;
        else {
          // ±±ðÇ¤üP·é©H
          for (int j = 0; j < i; j++)
            rightPlace = max(rightPlace, field[a[i]][a[j]] + p[j]);
        }
        p[i] = rightPlace;
        if (p[i] >= minLength)
          break;
      }
      if (minLength > p[n - 1])
        minLength = p[n - 1];
    } while (next_permutation(a, a + n));
    printf("%d\\n", minLength);
  }
  return 0;
}
Problem p00635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int n;
int a[10][10];

int dfs(const vector<pair<int, int>> &v, int b, int m) {
  if (b + 1 == 1 << n)
    return v.back().second;
  for (int i = 0; i < n; i++) {
    if (!(b >> i & 1)) {
      int p = 0;
      for (auto e : v) {
        p = max(p, e.second + a[e.first][i]);
      }
      if (m <= p)
        continue;
      auto nv = v;
      nv.emplace_back(i, p);
      m = min(m, dfs(nv, b | 1 << i, m));
    }
  }
  return m;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        a[i][j] = a[j][i] = max(a[i][j], a[j][i]);
      }
    }
    cout << dfs(vector<pair<int, int>>(), 0, 1 << 29) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int n;
int a[10][10];

int dfs(const vector<pair<int, int>> &v, int b) {
  if (b + 1 == 1 << n)
    return v.back().second;
  int m = 1 << 29;
  for (int i = 0; i < n; i++) {
    if (!(b >> i & 1)) {
      int p = 0;
      for (auto e : v) {
        p = max(p, e.second + a[e.first][i]);
      }
      auto nv = v;
      nv.emplace_back(i, p);
      m = min(m, dfs(nv, b | 1 << i));
    }
  }
  return m;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        a[i][j] = a[j][i] = max(a[i][j], a[j][i]);
      }
    }
    cout << dfs(vector<pair<int, int>>(), 0) << endl;
  }
}
Problem p00635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF (1 << 29)

int a[10][10], res;

void dfs(int n, int S, int cost, vector<int> &p, vector<int> &pos) {
  if (S == (1 << n) - 1) {
    res = min(res, cost);
    return;
  }

  if (res <= cost)
    return;

  for (int i = 0; i < n; i++) {
    if (S >> i & 1)
      continue;
    int npos = 0;
    for (int j = 0; j < (int)pos.size(); j++) {
      npos = max(npos, pos[j] + a[p[j]][i]);
    }
    p.push_back(i);
    pos.push_back(npos);

    dfs(n, S | (1 << i), npos, p, pos);

    p.pop_back();
    pos.pop_back();
  }
}

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

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        a[i][j] = max(a[i][j], a[j][i]);
      }
    }

    res = INF;
    vector<int> p, pos;
    dfs(n, 0, 0, p, pos);
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF (1 << 29)

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

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        a[i][j] = max(a[i][j], a[j][i]);
      }
    }

    vector<int> p(n);
    for (int i = 0; i < n; i++)
      p[i] = i;

    int res = INF;
    do {
      vector<int> pos(n);
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
          pos[p[i]] = max(pos[p[i]], pos[p[j]] + a[p[j]][p[i]]);
        }
      }
      res = min(res, pos[p[n - 1]]);
    } while (next_permutation(p.begin(), p.end()));

    cout << res << endl;
  }
  return 0;
}
Problem p00635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int main() {
  int N, A[10][10];
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        cin >> A[i][j];
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        A[i][j] = max(A[i][j], A[j][i]);
      }
    }

    int ret = INF;
    vector<int> s(N), p(N, 0);
    iota(begin(s), end(s), 0);
    int loop = 0;
    do {
      if (loop++ >= 3000000)
        break;
      int i = 1;
      while (i < s.size()) {
        p[i] = 0;
        for (int j = i - 1; j >= 0; j--) {
          p[i] = max(p[i], A[s[j]][s[i]] + p[j]);
        }
        if (p[i++] >= ret)
          break;
      }
      ret = min(ret, p.back());
    } while (next_permutation(begin(s), end(s)));

    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int main() {
  int N, A[10][10];
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        cin >> A[i][j];
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        A[i][j] = max(A[i][j], A[j][i]);
      }
    }

    int ret = INF;
    vector<int> s(N), p(N, 0);
    iota(begin(s), end(s), 0);

    do {
      if (rand() % 2 == 0)
        continue;
      int i = 1;
      while (i < s.size()) {
        p[i] = 0;
        for (int j = i - 1; j >= 0; j--) {
          p[i] = max(p[i], A[s[j]][s[i]] + p[j]);
        }
        if (p[i++] >= ret)
          break;
      }
      ret = min(ret, p.back());
    } while (next_permutation(begin(s), end(s)));

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

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

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

const int inf = 1 << 29;
int a[10][10], ans, n, res;
int perm[10], d[10];
bool use[10];

void dist(int sum, bool *used, int *p, int *data, int now) {
  if (sum > ans)
    return;
  if (n == now) {
    ans = sum;
    return;
  }

  int i, j;
  rep(i, n) {
    if (used[i])
      continue;
    used[i] = true;
    int tmp = 0;
    rep(j, now) tmp = max(tmp, data[j] + a[i][p[j]]);

    data[now] = tmp;
    p[now] = i;
    dist(data[now], used, p, data, now + 1);
    used[i] = false;
  }
}

int main() {
  int i, j, t;
  while (scanf("%d", &n), n) {
    memset(a, 0, sizeof(a));
    memset(perm, 0, sizeof(perm));
    memset(d, 0, sizeof(d));
    memset(use, false, sizeof(use));
    rep(i, n) rep(j, n) scanf("%d", &a[i][j]);

    rep(i, n) { rep(j, n) a[i][j] = max(a[i][j], a[j][i]); }
    ans = inf;

    dist(0, use, perm, d, 0);
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

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

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

const int inf = 1 << 29;
int a[10][10], ans, n, res;

int dist(vector<int> &perm) {
  int i, j, res = 0, k;
  int tmp[10] = {};
  rep(j, n) {
    REP(i, j + 1, n) {
      if (tmp[i] - tmp[j] < a[perm[j]][perm[i]]) {
        REP(k, i, n) { tmp[i] += a[perm[j]][perm[i]] - (tmp[i] - tmp[j]); }
      }
    }
    if (tmp[n - 1] > ans)
      return ans;
  }

  return tmp[n - 1];
}

int main() {
  int i, j, t;
  while (scanf("%d", &n), n) {
    memset(a, 0, sizeof(a));
    rep(i, n) rep(j, n) scanf("%d", &a[i][j]);

    vector<int> perm;
    rep(i, n) {
      rep(j, n) a[i][j] = max(a[i][j], a[j][i]);
      perm.push_back(i);
    }
    ans = inf;

    do {
      ans = min(ans, dist(perm));
    } while (next_permutation(perm.begin(), perm.end()));
    printf("%d\\n", ans);
  }
}
Problem p00635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int perm[11];
int g[11][11];
int d[11];
int v[11];
int ret;
int n;
void solve(int a) {
  if (a == n) {
    ret = d[n - 1];
    return;
  }
  for (int i = 0; i < n; i++) {
    if (v[i])
      continue;
    int at = 0;
    for (int j = 0; j < a; j++)
      at = max(at, d[j] + g[i][perm[j]]);
    if (at < ret) {
      v[i] = 1;
      perm[a] = i;
      d[a] = at;
      solve(a + 1);
      v[i] = 0;
    }
  }
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    n = a;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < a; j++) {
        scanf("%d", &g[i][j]);
      }
    }
    for (int i = 0; i < a; i++)
      for (int j = i + 1; j < a; j++) {
        g[i][j] = g[j][i] = max(g[i][j], g[j][i]);
      }
    for (int i = 0; i < a; i++)
      perm[i] = i;
    ret = 999999999;
    for (int i = 0; i < a; i++)
      v[i] = 0;
    solve(0);
    printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int perm[11];
int g[11][11];
int d[11];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < a; j++) {
        scanf("%d", &g[i][j]);
      }
    }
    for (int i = 0; i < a; i++)
      for (int j = i + 1; j < a; j++) {
        g[i][j] = g[j][i] = max(g[i][j], g[j][i]);
      }
    for (int i = 0; i < a; i++)
      perm[i] = i;
    int ret = 999999999;
    do {
      bool dame = false;
      for (int i = 1; i < a; i++) {
        d[i] = 0;
        for (int j = 0; j < i; j++)
          d[i] = max(d[i], d[j] + g[perm[i]][perm[j]]);
        if (d[i] >= ret) {
          dame = true;
          break;
        }
      }
      if (!dame)
        ret = min(ret, d[a - 1]);
    } while (next_permutation(perm, perm + a));
    printf("%d\\n", ret);
  }
}
Problem p00635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

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

using namespace std;

const int INF = 1 << 29;

int n, d[10][10];

int pos[10], ans;
bool used[10];
void dfs(int right) {
  if (right >= ans)
    return;

  int na = 0, a[10], nb = 0, b[10];
  rep(i, n) {
    if (!used[i])
      a[na++] = i;
    else
      b[nb++] = i;
  }

  if (na == 0) {
    ans = right;
    return;
  }

  int lb = right;
  rep(i, na) rep(j, nb) {
    int u = a[i], v = b[j];
    lb = max(lb, pos[v] + d[v][u]);
  }
  if (lb >= ans)
    return;

  rep(i, na) {
    int u = a[i], v;
    rep(j, nb) v = b[j], pos[u] = max(pos[u], pos[v] + d[v][u]);
    used[u] = true;

    dfs(pos[u]);

    pos[u] = 0;
    used[u] = false;
  }
}

int main() {
  for (; scanf("%d", &n), n;) {
    rep(i, n) rep(j, n) scanf("%d", d[i] + j);
    rep(i, n) rep(j, n) d[i][j] = max(d[i][j], d[j][i]);

    ans = INF;
    dfs(0);

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

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

using namespace std;

const int INF = 1 << 29;

int n, d[10][10];

int pos[10], ans;
bool used[10];
void dfs(int right) {
  if (right >= ans)
    return;

  if (count(used, used + n, true) == n) {
    ans = right;
    return;
  }

  int lb = right;
  rep(i, n) if (!used[i]) rep(j, n) if (used[j]) lb = max(lb, pos[j] + d[j][i]);
  if (lb >= ans)
    return;

  rep(i, n) if (!used[i]) {
    rep(j, n) if (used[j]) pos[i] = max(pos[i], pos[j] + d[j][i]);
    used[i] = true;

    dfs(pos[i]);

    pos[i] = 0;
    used[i] = false;
  }
}

int main() {
  for (; scanf("%d", &n), n;) {
    rep(i, n) rep(j, n) scanf("%d", d[i] + j);
    rep(i, n) rep(j, n) d[i][j] = max(d[i][j], d[j][i]);

    ans = INF;
    dfs(0);

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int main() {
  int N;
  while (cin >> N && N) {
    bool f = false;
    vector<int> v(N);
    REP(i, N) cin >> v[i];
    SORT(v);
    int l = 0, r = 0;
    REP(i, N) {
      if (r + 1 != N && v[i] + 1 == v[i + 1]) {
        ++r;
        continue;
      } else {
        if (f)
          cout << " ";
        f = true;
        if (l != r)
          cout << v[l] << "-" << v[r];
        else
          cout << v[r];
        l = i + 1;
        r = i + 1;
      }
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int MAX_N = 60;

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    bool nlist[MAX_N];
    for (int i = 0; i < MAX_N; i++) {
      nlist[i] = false;
    }
    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      nlist[tmp] = true;
    }
    int flag = 0;
    bool firflag = false;
    for (int i = 1; i < MAX_N; i++) {
      // cout  <<i <<" " <<"i - 1 = " <<nlist[i - 1] <<" i = " <<nlist[i]
      // <<endl;
      if (nlist[i - 1] == true) {
        if (nlist[i] == true) {
          // cout <<"no1" <<endl;
          if (firflag == false) {
            cout << i - 1;
            firflag = true;
          } else if (nlist[i - 2] == false && nlist[i - 1] == true) {
            cout << " " << i - 1;
          }
          flag++;
          if (flag == 1)
            cout << "-";
        } else {
          // cout <<"no2" <<endl;
          if (n != 1) {
            if (firflag == true && flag == 0) {
              cout << " ";
            }
            cout << i - 1;
            flag = 0;
            firflag = true;
          }
        }
      } else {
        if (nlist[i] == true) {
          // cout <<"no3" <<endl;
          if (n == 1) {
            cout << i;
            firflag = true;
          }
        } else {
          // cout <<"no4" <<endl;
        }
      }
    }
    cout << endl;
  }
  return 0;
}
Problem p00637 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    int first, last;
    int p = 0;
    first = a[p];
    while (p + 1 < n && a[p] + 1 == a[p + 1]) {
      p++;
    }
    last = a[p];
    if (first == last)
      cout << first;
    else
      cout << first << "-" << last;

    while (p + 1 < n) {
      p++;
      first = a[p];
      while (p + 1 < n && a[p] + 1 == a[p + 1]) {
        p++;
      }
      last = a[p];
      if (first == last)
        cout << " " << first;
      else
        cout << " " << first << "-" << last;
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      return 0;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    int first, last;
    int p = 0;
    first = a[p];
    while (p + 1 < n && a[p] + 1 == a[p + 1]) {
      p++;
    }
    last = a[p];
    if (first == last)
      cout << first;
    else
      cout << first << "-" << last;

    p++;
    while (p + 1 < n) {
      first = a[p];
      while (p + 1 < n && a[p] + 1 == a[p + 1]) {
        p++;
      }
      last = a[p];
      if (first == last)
        cout << " " << first;
      else
        cout << " " << first << "-" << last;
    }
    cout << endl;
  }
}
Problem p00637 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int no[50001], a, b, n;

  while (1) {
    int flg = 0;
    memset(no, 0, sizeof(no));
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a;
      no[a] = 1;
      if (i == 0)
        b = a;
    }
    for (int i = b; i <= 50000; i++) {
      if (no[i - 1] == 0 && no[i] == 1 && no[i + 1] == 0 && i == b) {
        if (flg == 1)
          cout << " ";
        flg = 1;
        cout << i;
        for (i++; i <= 50000; i++) {
          if (no[i + 1] == 1) {
            b = i + 1;
            break;
          }
        }
      } else if (no[i] == 1 && no[i + 1] == 0 && i != b) {
        if (flg == 1)
          cout << " ";
        flg = 1;
        cout << b << "-" << i;
        for (i++; i <= 50000; i++) {
          if (no[i + 1] == 1) {
            b = i + 1;
            break;
          }
        }
      }
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int no[51], a, b, n;

  while (1) {
    int flg = 0;
    memset(no, 0, sizeof(no));
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a;
      no[a] = 1;
      if (i == 0)
        b = a;
    }
    for (int i = b + 1; i <= 50; i++) {
      if (no[i] == 1 && no[i + 1] == 0) {
        if (flg == 1)
          cout << " ";
        flg = 1;
        cout << b << "-" << i;
        for (i++; i <= 50; i++) {
          if (no[i] == 1) {
            b = i;
            break;
          }
        }
      }
    }
    cout << endl;
  }
  return 0;
}
Problem p00637 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
using namespace std;
int a[151];
int main() {
  for (int n; cin >> n;) {
    if (n == 0)
      break;
    int i, j, k;
    for (i = 1; i <= n; ++i)
      cin >> a[i];
    int m1 = 1, m2 = 1, num = 0, an = 1;
    for (i = 2; i <= n; ++i) {
      if (a[i] != a[m2] + 1) {
        if (num > 0)
          cout << " ";
        if (an == 1)
          cout << a[m1];
        else
          cout << a[m1] << "-" << a[m2];
        num++;
        m1 = i;
        m2 = i;
        an = 1;
      } else {
        m2++;
        an++;
      }
      if (i == n) {
        if (num > 0)
          cout << " ";
        if (an == 1)
          cout << a[m1];
        else
          cout << a[m1] << "-" << a[n];
      }
    }
    if (n == 1)
      cout << a[1];
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int i, n, first, second;
  while (cin >> n, n != 0) {
    vector<int> a(n);
    for (i = 1; i <= n; i++)
      cin >> a[i];
    first = a[1];
    second = a[1];
    for (i = 2; i <= n; i++) {
      if (a[i] == second + 1)
        second++;
      else {
        if (first == second) {
          cout << first;
          if (i < n)
            cout << ' ';
          second = first = a[i];
        }
        if (first != second) {
          cout << first << '-' << second;
          if (i < n)
            cout << ' ';
          second = first = a[i];
        }
      }
    }

    if (first == second) {
      cout << first;

    } else if (first != second) {
      cout << first << '-' << second;
    }
    cout << endl;
  }
  return 0;
}
Problem p00637 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MAX 10000

int main() {
  int n, num;

  while (cin >> n, n) {
    int array[MAX] = {0};
    for (int i = 0; i < n; i++) {
      cin >> num;
      array[num] = 1;
    }

    int s, g, flg = 0, t = 0, cnt = 0;
    for (int i = 1; i < MAX; i++) {
      if (array[i]) {
        if (!t) {
          s = i;
          t++;
        }
        g = i;
        cnt++;
      } else {
        if (cnt == 1) {
          cnt = 0;
          if (t && !flg) {
            cout << s;
            flg++;
            t = 0;
          } else if (t && flg) {
            cout << ' ' << s;
            t = 0;
          }
        } else {
          cnt = 0;
          if (t && !flg) {
            cout << s << '-' << g;
            flg++;
            t = 0;
          } else if (t && flg) {
            cout << ' ' << s << '-' << g;
            t = 0;
          } else
            continue;
        }
      }
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 52

int main() {
  int n, array[MAX + 1], num;

  while (cin >> n, n) {
    fill(array, array + 52, 0);
    for (int i = 0; i < n; i++) {
      cin >> num;
      array[num] = 1;
    }

    int s, g, flg = 0, t = 0, cnt = 0;
    for (int i = 1; i < MAX; i++) {
      if (array[i]) {
        if (!t) {
          s = i;
          t++;
        }
        g = i;
        cnt++;
      } else {
        if (cnt == 1) {
          if (t && !flg) {
            cout << s;
            flg++;
            t = 0;
          } else if (t && flg) {
            cout << ' ' << s;
            t = 0;
          }
        } else {
          if (t && !flg) {
            cout << s << '-' << g;
            flg++;
            t = 0;
          } else if (t && flg) {
            cout << ' ' << s << '-' << g;
            t = 0;
          } else
            continue;
        }
      }
    }
    cout << endl;
  }

  return 0;
}