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

#include <bits/stdc++.h>
#define LLI long long int

using namespace std;

LLI left(LLI here, LLI lsum);
LLI right(LLI here, LLI rsum);
LLI po();
LLI ko();
LLI ans, sum, x, k, n, num[110000];

int main() {

  while (1) {

    cin >> n >> k;
    if (n == 0 && k == 0)
      break;

    for (int i = 0; i < k; i++) {
      cin >> x;
      num[x] = 1;
    }

    cout << po() << endl;

    ans = 0;
    sum = 0;

    for (int i = 0; i < n; i++) {
      num[i] = 0;
    }
  }

  return 0;
}

LLI po() {

  if (num[0] == 1) {

    for (int i = 0; i < n; i++) {
      if (num[i] == 0) {
        sum = left(i - 1, 0) + right(i + 1, 0) + 1;
        if (ans < sum)
          ans = sum;
      }
    }

  } else
    ans = ko();

  return ans;
}

LLI right(LLI here, LLI rsum) {
  if (num[here] == 1)
    return right(here + 1, rsum + 1);
  else
    return rsum;
}

LLI left(LLI here, LLI lsum) {
  if (num[here] == 1)
    return left(here - 1, lsum + 1);
  else
    return lsum;
}

LLI ko() {

  LLI a = 0;

  for (int i = 1; i < n + 1; i++) {

    if (num[i] == 1)
      sum++;
    else {
      if (a < sum)
        a = sum;
      sum = 0;
    }
  }

  return a;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LLI long long int

using namespace std;

LLI ko(int card);

LLI sum, ans, num[1000000], n, x, mo, k;

LLI po(int card) {
  if (num[0] == 1) {
    for (int i = 1; i < n + 1; i++) {
      if (num[i] == 0) {
        mo = ko(i);
      }
      if (mo > ans)
        ans = mo;
    }
  } else
    ans = ko(0);
  return ans;
}

LLI ko(int card) {

  LLI a = 0, sum = 0;

  for (int i = 1; i < n + 1; i++) {

    if (num[i] == 1 || card == i)
      sum++;
    else {
      if (a < sum) {
        a = sum;
      }
      sum = 0;
    }
    if (a < sum)
      a = sum;
  }
  return a;
}

int main() {

  while (1) {

    cin >> n >> k;
    if (n == 0 && k == 0)
      break;

    for (int i = 0; i < k; i++) {
      cin >> x;
      num[x] = 1;
    }

    cout << po(0) << endl;
    mo = 0, sum = 0, ans = 0;

    for (int i = 0; i < n; i++) {

      num[i] = 0;
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
int num[100000], a[100000], cou = 1, buf, l, ma;
// bool b[100000];
using namespace std;
int main() {
  for (;;) {
    int n, k, c = 0, d = 0;
    ma = 0, buf = 0, l = 0, ma = 0;
    bool b[100000] = {false};
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    fill(num, num + k, 0);
    fill(a, a + k, 0);
    for (int i = 0; i < k; i++)
      b[i] = false;
    for (int i = 0; i < k; i++)
      cin >> num[i];
    sort(num, num + k);
    l = 1;
    if (num[0] == 0) {
      buf = 1;
      l = 2;
    }
    for (int i = l; i < k + 1; i++) {
      //            if(num[i]==0)break;
      if (num[i] == num[i - 1] + 1) {
        cou++;
        continue;
      } else {
        a[c] = cou;
        c++;
        a[c] = num[i] - num[i - 1];
        c++;
      }
      cou = 1;
    }
    for (int i = 0; i < k; i++) {
      if (i % 2 == 1) {
        if (a[i] == 2)
          b[i] = true;
      }
    }
    //        for(int i=0;i<k;i++)cout<<a[i]<<" ";
    ma = a[0];
    for (int i = 1; i < k; i++) {
      if (buf == 1) {
        if (b[i]) {
          ma = max(ma, a[i - 1] + 1 + a[i + 1]);
          continue;
        }
      }
      if (i % 2 != 1)
        ma = max(ma, a[i]);
    }
    // for(int i=0;i<k;i++)cout<<a[i]<<" ";
    cout << ma << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int K, N, cou, c, _cou;
  while (cin >> N >> K, N || K) {
    _cou = 0, cou = 0;
    bool card[100001] = {false};
    for (int i = 0; i < K; i++) {
      cin >> c;
      card[c] = true;
    }
    for (int k = 1; k < N; k++) {
      if (card[k])
        card[k] = false;
      else
        card[k] = true;
      for (int i = 1; i <= N; i++) {
        cou = 0;
        for (int j = i; j <= N + 1; j++) {
          if (card[j]) {
            cou++;
          } else {
            if (_cou <= cou) {
              _cou = cou;
            }
            break;
          }
        }
      }
      if (card[k])
        card[k] = false;
      else
        card[k] = true;
    }
    cout << _cou << endl;
  }
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main() {
  int n, k;
  vector<int> num;
  int temp;
  bool wildcard;
  int ans;

  cin >> n >> k;
  while (n != 0 && k != 0) {
    ans = 0;
    for (int i = 0; i < k; ++i) {
      cin >> temp;
      num.push_back(temp);
    }

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

    if (num[0] == 0) {
      wildcard = true;
    } else {
      wildcard = false;
    }

    if (wildcard) {
      for (int i = 1; i < (int)num.size(); ++i) {
        for (int j = 0; i + j < (int)num.size(); ++j) {
          if (num[i + j] == num[i] + j) {
            if (num[i] + j != n || num[i] != 1) {
              ans = MAX(ans, j + 2);
            } else {
              ans = MAX(ans, j + 1);
            }
          } else if (num[i + j] == num[i] + j + 1) {
            ans = MAX(ans, j + 2);
          } else {
            break;
          }
        }
      }
    } else {
      for (int i = 0; i < (int)num.size(); ++i) {
        for (int j = 0; i + j < (int)num.size(); ++j) {
          if (num[i + j] == num[i] + j) {
            ans = MAX(ans, j + 1);
          } else {
            break;
          }
        }
      }
    }

    cout << ans << endl;
    num.clear();
    cin >> n >> k;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main() {
  int n, k;
  vector<int> num;
  int temp;
  bool wildcard;
  int ans;

  cin >> n >> k;
  while (n != 0 && k != 0) {
    ans = 0;
    for (int i = 0; i < k; ++i) {
      cin >> temp;
      num.push_back(temp);
    }

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

    if (num[0] == 0) {
      wildcard = true;
    } else {
      wildcard = false;
    }

    if (wildcard) {
      for (int i = 1; i < (int)num.size(); ++i) {
        for (int j = 0; i + j < (int)num.size(); ++j) {
          if (num[i + j] == num[i] + j) {
            if (num[i] + j != n || num[i] != 1) {
              ans = MAX(ans, j + 2);
            } else {
              ans = MAX(ans, j + 1);
            }
          } else if (num[i + j] == num[i] + j + 1) {
            ans = MAX(ans, j + 2);
          }
        }
      }
    } else {
      for (int i = 0; i < (int)num.size(); ++i) {
        for (int j = 0; i + j < (int)num.size(); ++j) {
          if (num[i + j] == num[i] + j) {
            ans = MAX(ans, j + 1);
          }
        }
      }
    }

    cout << ans << endl;
    num.clear();
    cin >> n >> k;
  }
  return 0;
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (1) {
    int n, k;
    scanf("%d %d", &n, &k);
    if (n == 0 && k == 0)
      break;
    vector<int> v;
    for (int i = 0; i < k; i++) {
      int t;
      scanf("%d", &t);
      v.push_back(t);
    }
    sort(v.begin(), v.end());
    int ans = 1;

    if (v[0] != 0) {
      int dp[k];
      dp[0] = 1;
      for (int i = 1; i < k; i++) {
        if (v[i] - v[i - 1] == 1)
          dp[i] = dp[i - 1] + 1;
        else
          dp[i] = 1;
        ans = max(ans, dp[i]);
      }
    } else {
      vector<int> w;
      int dp[k];
      dp[1] = 1;
      for (int i = 2; i < k; i++) {
        if (v[i] - v[i - 1] == 1)
          dp[i] = dp[i - 1] + 1;
        else {
          w.push_back(dp[i - 1]);
          dp[i] = 1;
        }
      }
      w.push_back(dp[k - 1]);
      for (int i = 0; i < w.size() - 1; i++) {
        ans = max(ans, w[i] + 1 + w[i + 1]);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n, k;
    scanf("%d %d", &n, &k);
    if (n == 0 && k == 0)
      break;
    vector<int> v;
    for (int i = 0; i < k; i++) {
      int t;
      scanf("%d", &t);
      v.push_back(t);
    }
    sort(v.begin(), v.end());
    int dp[k];
    int ans = 1;

    if (v[0] != 0) {
      dp[0] = 1;
      for (int i = 1; i < k; i++) {
        if (v[i] - v[i - 1] == 1)
          dp[i] = dp[i - 1] + 1;
        else
          dp[i] = 1;
        ans = max(ans, dp[i]);
      }
    } else {
      for (int i = 1; i < n + 1; i++) {
        vector<int> w;
        for (int j = 0; j < k; j++) {
          w.push_back(v[j]);
        }
        w[0] = i;
        sort(w.begin(), w.end());
        /*				printf("%d\\n", i);
                                        for(int j=0; j<k; j++){
                                                printf("%d ", w[j]);
                                        }
                                        printf("\\n");
        */
        dp[0] = 1;
        for (int l = 1; l < k; l++) {
          if (w[l] - w[l - 1] == 1)
            dp[l] = dp[l - 1] + 1;
          else
            dp[l] = 1;
          ans = max(ans, dp[l]);
        }
        //				printf("%d\\n", ans);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int card[100000];
bool shiro = false;
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 and m == 0) {
      break;
    }
    shiro = false;
    for (int i = 0; i < 100000; i++) {
      card[i] = 0;
    }
    for (int i = 0; i < m; i++) {
      int u;
      cin >> u;
      if (u != 0) {
        card[u - 1]++;
      } else {
        shiro = true;
      }
    }
    if (shiro == false) {
      int ans = 0;
      int poyo = 0;
      for (int i = 0; i < n; i++) {
        if (card[i] == 1) {
          poyo++;
        } else {
          ans = max(ans, poyo);
          poyo = 0;
        }
      }
      cout << ans << endl;
    } else {
      int ans = 0;
      int poyo = 0;
      int piyo = 0;
      for (int i = 0; i < n; i++) {
        if (card[i] == 1) {
          poyo++;
        } else {
          ans = max(ans, poyo + piyo + 1);
          piyo = poyo;
          poyo = 0;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int card[100000];
bool shiro = false;
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 and m == 0) {
      break;
    }
    shiro = false;
    for (int i = 0; i < 100000; i++) {
      card[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int u;
      cin >> u;
      if (u != 0) {
        card[u - 1]++;
      } else {
        shiro = true;
      }
    }
    if (shiro == false) {
      int ans = 0;
      int poyo = 0;
      for (int i = 0; i < n; i++) {
        if (card[i] == 1) {
          poyo++;
        } else {
          ans = max(ans, poyo);
          poyo = 0;
        }
      }
      cout << ans << endl;
    } else {
      int ans = 0;
      int poyo = 0;
      int piyo = 0;
      for (int i = 0; i < n; i++) {
        if (card[i] == 1) {
          poyo++;
        } else {
          ans = max(ans, poyo + piyo + 1);
          piyo = poyo;
          poyo = 0;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
typedef unsigned long long ull;
#define rep(i, a) for (int i = 0; i < a; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

int main(void) {
  int n, k;
  while (cin >> n >> k, n + k) {
    vector<bool> in(n + 1, 0);
    vector<bool> used;
    rep(i, k) {
      int num;
      cin >> num;
      in[num] = true;
    }

    vector<int> dp(n);
    vector<int> box;
    int maxv = 0;
    loop(i, 1, n + 1) {
      dp[i] = dp[i - 1] + in[i];
      maxv = max(maxv, dp[i]);
      if (in[i] == 0) {
        dp[i] = 0;
        if (in[i - 1] && in[i + 1])
          box.push_back(i);
      }
    }
    if (in[0]) {
      for (int i = n - 1; i > 0; i--) {
        if (in[i])
          dp[i] = max(dp[i], dp[i + 1]);
      }
      rep(i, box.size()) {
        maxv = max(maxv, dp[box[i] - 1] + dp[box[i] + 1] + 1);
      }
    }
    cout << maxv << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
typedef unsigned long long ull;
#define rep(i, a) for (int i = 0; i < a; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

int main(void) {
  int n, k;
  while (cin >> n >> k, n + k) {
    vector<bool> in(n + 1, 0);
    vector<bool> used;
    rep(i, k) {
      int num;
      cin >> num;
      in[num] = true;
    }

    vector<int> dp(n);
    vector<int> box;
    int maxv = 0;
    loop(i, 1, n + 1) {
      dp[i] = dp[i - 1] + in[i];
      maxv = max(maxv, dp[i]);
      if (in[i] == 0) {
        dp[i] = 0;
        if (in[i - 1] && in[i + 1])
          box.push_back(i);
      }
    }

    if (in[0]) {
      in[box[0]] = true;
      loop(i, 1, box.size()) {
        in[box[i - 1]] = false;
        in[box[i]] = true;
        loop(i, 1, n + 1) {
          dp[i] = dp[i - 1] + in[i];
          maxv = max(maxv, dp[i]);
          if (in[i] == 0) {
            dp[i] = 0;
          }
        }
      }
    }
    cout << maxv << endl;
  }
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, k;
  while (1) {
    cin >> n >> k;
    int data[100300];
    for (int i = 0; i < n + 3; i++) {
      data[i] = 0;
    }
    int all = 0, ans = 0;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < k; i++) {
      int card;
      cin >> card;
      if (card == 0)
        all++;
      else
        data[card] = 1;
    }
    for (int i = 1; i <= n; i++) {
      int tmp = all;
      int cnt = 0;
      for (int j = i; j <= n; j++) {
        if (data[j] == 1)
          cnt++;
        else if (tmp != 0) {
          cnt++;
          tmp--;
        } else
          break;
      }
      ans = max(ans, cnt);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n, k;
  while (1) {
    cin >> n >> k;
    int data[1003];
    for (int i = 0; i < n + 3; i++) {
      data[i] = 0;
    }
    int all = 0, ans = 0;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < k; i++) {
      int card;
      cin >> card;
      if (card == 0)
        all++;
      else
        data[card] = 1;
    }
    for (int i = 1; i <= n; i++) {
      int tmp = all;
      int cnt = 0;
      for (int j = i; j <= n; j++) {
        if (data[j] == 1)
          cnt++;
        else if (tmp != 0) {
          cnt++;
          tmp--;
        } else
          break;
      }
      ans = max(ans, cnt);
    }
    cout << ans << endl;
  }
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int n, k;
bool used[100001] = {};
int num[100001];
int main() {
  while (1) {
    fill(used, used + 100001, false);
    scanf("%d %d", &n, &k);
    if (!n)
      break;
    for (int i = 0; i < k; i++) {
      scanf("%d", &num[i]);
      used[num[i]] = true;
    }
    sort(num, num + k);
    int s = 0, e = 0, sum = 0, res = 0;
    if (used[0]) {
      for (;;) {
        while (e <= n && sum <= 1) {
          if (!used[e]) {
            sum++;
            e++;
          } else {
            e++;
          }
        }
        if (sum <= 1) {
          res = max(res, e - s);
          break;
        }
        res = max(res, e - s);
        if (!used[s]) {
          sum--;
        }
        s++;
      }
    } else {
      e = 1;
      s = 1;
      for (;;) {
        while (e <= n && sum <= 0) {
          if (!used[e]) {
            sum++;
            e++;
          } else {
            e++;
          }
        }
        if (sum <= 0) {
          break;
        }
        res = max(res, e - s);
        if (!used[s]) {
          sum--;
        }
        s++;
      }
    }
    printf("%d\\n", res - 1);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int n, k;
bool used[100001] = {};
int num[100001];
int main() {
  while (1) {
    fill(used, used + 100001, false);
    scanf("%d %d", &n, &k);
    for (int i = 0; i < k; i++) {
      scanf("%d", &num[i]);
      used[num[i]] = true;
    }
    sort(num, num + k);
    int s = 0, e = 0, sum = 0, res = 0;
    if (used[0]) {
      for (;;) {
        while (e <= n && sum <= 1) {
          if (!used[e]) {
            sum++;
            e++;
          } else {
            e++;
          }
        }
        if (sum <= 1) {
          res = max(res, e - s);
          break;
        }
        res = max(res, e - s);
        if (!used[s]) {
          sum--;
        }
        s++;
      }
    } else {
      e = 1;
      s = 1;
      for (;;) {
        while (e <= n && sum <= 0) {
          if (!used[e]) {
            sum++;
            e++;
          } else {
            e++;
          }
        }
        if (sum <= 0) {
          break;
        }
        res = max(res, e - s);
        if (!used[s]) {
          sum--;
        }
        s++;
      }
    }
    printf("%d\\n", res - 1);
  }
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

//
//  main.cpp

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

// 0???????´?????????????

int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    bool array[100002] = {};
    int input;
    for (int i = 0; i < k; i++) {
      cin >> input;
      array[input] = true;
    }
    int ruiseki[100002] = {};
    for (int i = 1; i <= n; i++) {
      if (array[i]) {
        ruiseki[i] = ruiseki[i - 1] + 1;
      }
    }

    vector<int> vec;
    for (int i = 1; i <= n; i++) {
      if (ruiseki[i] == 0) {
        vec.push_back(0);
      } else if (ruiseki[i] > 0 && ruiseki[i + 1] == 0) {
        vec.push_back(ruiseki[i]);
      }
    }

    if (array[0]) {
      vector<pair<int, int>> vecpr;
      for (int i = 1; i < vec.size() - 1; i++) {
        if (vec[i - 1] > 0 && vec[i] == 0 && vec[i + 1] > 0) {
          vecpr.push_back(make_pair(vec[i - 1], vec[i + 1]));
        }
      }

      int answer = 0;
      for (int i = 0; i < vecpr.size(); i++) {
        answer = max(answer, vecpr[i].first + vecpr[i].second + 1);
      }

      cout << answer << endl;
    } else {
      int answer = 0;
      for (int i = 0; i < vec.size(); i++) {
        answer = max(answer, vec[i]);
      }
      cout << answer << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

//
//  main.cpp

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

// 0???????´?????????????

int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    bool array[101] = {};
    int input;
    for (int i = 0; i < k; i++) {
      cin >> input;
      array[input] = true;
    }
    int ruiseki[101] = {};
    for (int i = 1; i <= n; i++) {
      if (array[i]) {
        ruiseki[i] = ruiseki[i - 1] + 1;
      }
    }

    vector<int> vec;
    for (int i = 1; i <= n; i++) {
      if (ruiseki[i] == 0) {
        vec.push_back(0);
      } else if (ruiseki[i] > 0 && ruiseki[i + 1] == 0) {
        vec.push_back(ruiseki[i]);
      }
    }

    if (array[0]) {
      vector<pair<int, int>> vecpr;
      for (int i = 1; i < vec.size() - 1; i++) {
        if (vec[i - 1] > 0 && vec[i] == 0 && vec[i + 1] > 0) {
          vecpr.push_back(make_pair(vec[i - 1], vec[i + 1]));
        }
      }

      int answer = 0;
      for (int i = 0; i < vecpr.size(); i++) {
        answer = max(answer, vecpr[i].first + vecpr[i].second + 1);
      }

      cout << answer << endl;
    } else {
      int answer = 0;
      for (int i = 0; i < vec.size(); i++) {
        answer = max(answer, vec[i]);
      }
      cout << answer << endl;
    }
  }
  return 0;
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int n = 0, k = 0, a[100000] = {}, sam, b[100000] = {}, roop = 0,
    save[100000] = {0}, ans[100000] = {}, w = 0;
int main(void) {
  while (1) {
    int n = 0, k = 0, a[100000] = {}, sam, b[100000] = {}, roop = 0,
        save[100000] = {0}, ans[100000] = {}, w = 0;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < k; i++) {
      cin >> a[i];
    }
    sort(a, a + k);
    for (int i = 0; i < k; i++) {
      if (a[i + 1] - a[i] == 1) {
        sam++;
      } else if (a[i + 1] - a[i] != 1) {
        b[roop] = sam + 1;
        if (a[0] == 0) {
          if (a[i + 1] - a[i] == 2) {
            save[w] = roop;
            w++;
          }
        }
        sam = 0;
        roop++;
      }
    }
    for (int i = 0; i <= roop; i++) {
      if (save[i] != 0) {
        ans[i] = b[save[i]];
        ans[i] += b[save[i] + 1] + 1;
      }
    }
    sort(b, b + roop + 1);
    sort(ans, ans + roop + 1);
    if (ans[roop] > b[roop])
      cout << ans[roop] << endl;
    if (ans[roop] <= b[roop]) {
      if (a[0] == 0) {
        cout << b[roop] + 1 << endl;
      } else {
        cout << b[roop] << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n = 0, k = 0, a[100000] = {}, sam, b[1000] = {}, roop = 0, save[1000] = {0},
    ans[1000] = {}, w = 0;
int main(void) {
  while (1) {
    int n = 0, k = 0, a[100000] = {}, sam, b[1000] = {}, roop = 0,
        save[1000] = {0}, ans[1000] = {}, w = 0;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < k; i++) {
      cin >> a[i];
    }
    sort(a, a + k);
    for (int i = 0; i < k; i++) {
      if (a[i + 1] - a[i] == 1) {
        sam++;
      } else if (a[i + 1] - a[i] != 1) {
        b[roop] = sam + 1;
        if (a[0] == 0) {
          if (a[i + 1] - a[i] == 2) {
            save[w] = roop;
            w++;
          }
        }
        sam = 0;
        roop++;
      }
    }
    for (int i = 0; i <= roop; i++) {
      if (save[i] != 0) {
        ans[i] = b[save[i]];
        ans[i] += b[save[i] + 1] + 1;
      }
    }
    sort(b, b + roop + 1);
    sort(ans, ans + roop + 1);
    if (ans[roop] > b[roop])
      cout << ans[roop] << endl;
    if (ans[roop] <= b[roop]) {
      if (a[0] == 0) {
        cout << b[roop] + 1 << endl;
      } else {
        cout << b[roop] << endl;
      }
    }
  }
  return 0;
}