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

#include <algorithm>
#include <iostream>
using namespace std;
int n = 0, k = 0, a[200000] = {}, 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[1000] = {}, roop = 0, save[1000] =
    // {0}, ans[1000] = {}, w = 0;
    cin >> n >> k;
    if (n == 0 || k == 0)
      return 0;
    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[200000] = {}, 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[1000] = {}, roop = 0,
        save[1000] = {0}, ans[1000] = {}, w = 0;
    cin >> n >> k;
    if (n == 0 || k == 0)
      return 0;
    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;
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int n = 0, k = 0, a[200000] = {}, 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[1000] = {}, roop = 0, save[1000] =
    //{0}, ans[1000] = {}, w = 0;
    cin >> n >> k;
    if (n == 0 || k == 0)
      return 0;
    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;
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int n, k;
  for (;;) {
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    vector<int> ns(k);
    for (int i = 0; i < k; i++) {
      scanf("%d", &ns[i]);
    }
    sort(ns.begin(), ns.end());
    vector<pair<int, int>> rens;
    int st = 0;
    bool isc = false;
    if (ns[0] == 0) {
      st = 1;
      isc = true;
    }
    for (int i = st + 1; i < k; i++) {
      if (ns[i] != ns[i - 1] + 1) {
        rens.push_back(make_pair(ns[st], ns[i - 1]));
        st = i;
      }
    }
    rens.push_back(make_pair(ns[st], ns[k - 1]));
    int ma = 0;
    if (isc) {
      for (int i = 1; i < rens.size(); i++) {
        if (rens[i - 1].second + 2 == rens[i].first) {
          ma = max(ma, rens[i].second - rens[i - 1].first + 1);
        } else
          ma = max(ma, rens[i].second - rens[i].first + 1);
      }
    } else {
      for (int i = 0; i < rens.size(); i++) {
        ma = max(ma, rens[i].second - rens[i].first + 1);
      }
    }
    printf("%d\\n", ma);
  }
}

Error: WA

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() {
  while (1) {
    int n = 0, k = 0, a[100000] = {}, sam, b[1000] = {}, roop = 0,
        save[1000] = {0}, ans[1000] = {}, w = 0;
    cin >> n >> k;
    cout << n << ' ' << k << endl;
    if (n == 0 || k == 0)
      break;
    for (int i = 0; i < k + 1; 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;
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N = 0;
int K = 0;
bool card[100020] = {0};
bool wildCard = false;

void init() {
  N = K = 0;
  wildCard = false;
  fill(card, card + 100020, 0);
}

bool input() {
  init();
  cin >> N >> K;
  if (!N && !K)
    return false;
  for (int i = 0; i < K; i++) {
    int temp;
    cin >> temp;
    if (temp)
      card[temp] = true;
    else
      wildCard = true;
  }
  return true;
}

int solve() {
  int ans = 0;
  if (wildCard) {
    vector<int> space;
    for (int i = 1; i <= N; i++) {
      if (!card[i])
        space.push_back(i);
    }
    for (int i = 0, len = space.size(); i < len; i++) {
      int temp = 1;
      for (int j = space[i] - 1; j > 0; j--) {
        if (card[j])
          temp++;
        else
          break;
      }
      for (int j = space[i] + 1; j <= N; j++) {
        if (card[j])
          temp++;
        else
          break;
      }
      ans = max(ans, temp);
    }
  } else {
    int temp = 0;
    for (int i = 1; i <= N; i++) {
      if (card[i])
        temp++;
      else {
        ans = max(ans, temp);
        temp = 0;
      }
    }
  }
  return ans;
}

int main() {
  while (input()) {
    printf("%d\\n", solve());
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int N = 0;
int K = 0;
int cardNo[100020] = {0};
bool wildcard = false;

void init() {
  N = 0;
  K = 0;
  fill(cardNo, cardNo + 100020, 0);
  wildcard = false;
}

bool input() {
  init();
  cin >> N >> K;
  if (!N && !K)
    return false;
  for (int i = 0; i < K; i++) {
    int number = 0;
    cin >> number;
    if (!number)
      wildcard = true;
    cardNo[i] = number;
  }
  sort(cardNo, cardNo + K);
  return true;
}

int solve() {
  int ans = 1;
  int wildNum = 0;
  int memo = 1;
  for (int i = 0; i < K; i++) {
    if (!cardNo[i])
      continue;
    int difference = cardNo[i + 1] - cardNo[i];
    if (wildcard) {
      if (difference == 1)
        memo++;
      else if (difference == 2 && !wildNum) {
        wildNum = i;
        memo += 2;
      } else {
        ans = max(ans, memo);
        i = (i == K - 1) ? i : wildNum;
        memo = 1;
        wildNum = 0;
      }
    } else {
      if (difference == 1)
        memo++;
      else {
        ans = max(ans, memo);
        memo = 1;
      }
    }
  }
  return ans;
}

int main() {
  while (input()) {
    printf("%d\\n", solve());
  }
  return 0;
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

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

class Section {
private:
  int _begin, _end;

public:
  Section(int b, int e) {
    _begin = b;
    _end = e;
  }

  int begin() { return _begin; }

  int end() { return _end; }

  int length() { return end() - begin(); }
};

vector<Section> sections;
bool card[100002];
int K, N;
int main() {
loop:
  fill(card, card + 100002, false);
  sections.clear();

  cin >> N >> K;
  if (!N)
    return 0;
  for (int i = 0; i < K; i++) {
    int a;
    cin >> a;
    card[a] = true;
  }

  bool keep = false;
  int st, ans = 0;
  for (int i = 1; i <= N + 1; i++) {
    if (keep && !card[i]) {
      sections.push_back(Section(st, i));
      keep = false;
    }
    if (!keep && card[i]) {
      st = i;
      keep = true;
    }
  }

  for (auto it = sections.begin(); it != sections.end(); ++it) {
    if (card[0] && (it + 1) != sections.end() &&
        it->end() + 1 == (it + 1)->begin())
      ans = max(ans, it->length() + (it + 1)->length() + 1);
    if (card[0])
      ans = max(ans, it->length() + 1);
    ans = max(ans, it->length());
  }
  cout << ans << endl;
  goto loop;
}

Error: 0

Fixed submission:

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

class Section {
private:
  int _begin, _end;

public:
  Section(int b, int e) {
    _begin = b;
    _end = e;
  }

  int begin() { return _begin; }

  int end() { return _end; }

  int length() { return end() - begin(); }
};

vector<Section> sections;
bool card[1002];
int K, N;
int main() {
loop:
  fill(card, card + 1002, false);
  sections.clear();

  cin >> N >> K;
  if (!N)
    return 0;
  for (int i = 0; i < K; i++) {
    int a;
    cin >> a;
    card[a] = true;
  }

  bool keep = false;
  int st, ans = 0;
  for (int i = 1; i <= N + 1; i++) {
    if (keep && !card[i]) {
      sections.push_back(Section(st, i));
      keep = false;
    }
    if (!keep && card[i]) {
      st = i;
      keep = true;
    }
  }

  for (auto it = sections.begin(); it != sections.end(); ++it) {
    if (card[0] && (it + 1) != sections.end() &&
        it->end() + 1 == (it + 1)->begin())
      ans = max(ans, it->length() + (it + 1)->length() + 1);
    if (card[0])
      ans = max(ans, it->length() + 1);
    ans = max(ans, it->length());
  }
  cout << ans << endl;
  goto loop;
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <deque>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

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

int dp[2][100001];
int masu[100001];
int main() {
  while (1) {
    rep(i, 2) rep(j, 100001) { dp[i][j] = 0; }
    rep(j, 100001) { masu[j] = 0; }

    int n, m;
    scanf("%d%d", &n, &m);
    if (n == 0)
      break;

    int fflg = 0;
    rep(i, m) {
      int a;
      scanf("%d", &a);
      if (a == 0) {
        fflg = 1;
        continue;
      }

      masu[a] = 1;
    }

    reps(i, n) {
      if (masu[i] == 1) {
        dp[0][i] = dp[0][i - 1] + 1;
      }
    }

    reps(i, n) {
      if (masu[i] == 1) {
        dp[1][i] = dp[1][i - 1] + 1;
      } else {
        dp[1][i] = dp[0][i - 1] + 1;
      }
    }

    int maxi = -1;
    reps(i, n) { maxi = max(maxi, dp[fflg][i]); }

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

Error: 0

Fixed submission:

#include <cstdio>

int haks[100010];
int use[100010];
using namespace std;

int main() {

  while (1) {
    int n, k;
    scanf("%d%d", &n, &k);
    if (n == 0) {
      break;
    }

    int haku = 0;

    for (int i = 0; i <= n + 8; i++) {
      haks[i] = 0;
    }
    int card;

    for (int i = 0; i < k; i++) {
      scanf("%d", &card);
      if (card == 0) {
        haku++;
      }
      haks[card] = 1;
    }

    haks[n + 1] = 0;

    /*
    for(int i=1;i<=n+10;i++){
            printf("%d ",haks[i]);
    }puts("");
    */

    int start = 1;
    int end = 0;

    int cont;

    cont = 0;

    /*
    while(1){
            if(haks[end]==1){
                    if(cont+1>haku){
                            break;
                    }else{
                            cont++;
                    }
            }
            end++;
    }*/

    for (int i = 1;; i++) {
      if (haks[i] == 1) {
        start = i;
        end = start;
        start -= haku;
        break;
      }
    }
    // printf("s=%d e=%d\\n",start,end);

    int max = 0;

    while (1) {

      start++;
      end++;

      if (end >= n) {
        break;
      }
      while (1) {
        if (haks[start] == 1) {
          break;
        } else {
          start++;
        }
      }

      while (1) {
        if (haks[end + 1] == 0) {
          break;
        } else {
          end++;
        }
      }

      if (max < end - start + 1) {
        max = end - start + 1;
      }

      // printf("s=%d e=%d\\n",start,end);
    }
    printf("%d\\n", max);
  }
}
Problem p00440 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int solve(vector<int> data, int white, int n) {
  int dp[100001] = {0};
  rep(i, 100001) dp[i] = 1;
  rep(i, data.size()) dp[data[i]] = 0;

  dp[0] = 0;
  for (int i = 1; i <= n; i++)
    dp[i] = dp[i - 1] + dp[i];

  int l = 1, r = 1, ans = 0;
  while (r <= n) {
    if (dp[r] - dp[l - 1] <= white) {
      ans = max(ans, r - l + 1);
      r++;
    } else {
      l++;
    }
  }
  return ans;
}
int main() {
  int n, k;
  while (cin >> n >> k, n) {
    vector<int> data;
    int e = 0;
    rep(i, k) {
      int t;
      cin >> t;
      if (t)
        data.push_back(t);
      else
        e++;
    }
    cout << solve(data, e, n) << endl;
  }
}

Error: MLE

Fixed submission:

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

set<vector<int>> data;

void rec(vector<int> d, int last) {
  data.insert(d);
  for (int i = 0; i < last; i++) {
    if (i && d[i] + 1 > d[i - 1])
      continue;
    d[last]--, d[i]++;
    if (data.find(d) == data.end())
      rec(d, d[last] == 0 ? last - 1 : last);
    d[last]++, d[i]--;
  }
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<int> d(n, 1);
    set<vector<int>> emp;
    data = emp;
    rec(d, n - 1);
    for (set<vector<int>>::reverse_iterator it = data.rbegin();
         it != data.rend(); ++it) {
      for (int j = 0; j < (*it).size(); j++)
        if ((*it)[j])
          cout << (j ? " " : "") << (*it)[j];
      cout << endl;
    }
  }
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int s[100000];
int pleft[100000], pright[100000];
int main() {
  int n, k;
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    bool zero = false;
    for (int i = 0; i < k; i++) {
      cin >> s[i];
      if (s[i] == 0)
        zero = true;
    }
    sort(s, s + k);
    if (zero == false) {
      int best = 1;
      int count = 1;
      for (int i = 1; i < k; i++) {
        if (s[i - 1] + 1 == s[i])
          count++;
        else {
          best = max(best, count);
          count = 1;
        }
      }
      best = max(best, count);
      cout << best << endl;
    } else {
      int top = 0;
      bool start = true;
      for (int i = 2; i < k; i++) {
        if (s[i - 1] + 1 == s[i] && start == true) {
          pleft[top] = s[i - 1];
          start = false;
        }
        if (s[i - 1] + 1 != s[i]) {
          if (start == true)
            pleft[top] = s[i - 1];
          pright[top] = s[i - 1];
          top++;
          start = true;
        }
      }
      pright[top] = s[k - 1];
      top++;
      int best = 1;
      for (int i = 1; i < top; i++) {
        if (pright[i - 1] + 2 == pleft[i])
          best = max(best, 1 + pright[i] - pleft[i - 1]);
      }
      cout << best << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int s[100000];
int main() {
  int n, k;
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    bool zero = false;
    for (int i = 0; i < k; i++) {
      cin >> s[i];
      if (s[i] == 0)
        zero = true;
    }

    if (zero == false) {
      int best = 1;
      int count = 1;
      sort(s, s + k);
      for (int i = 1; i < k; i++) {
        if (s[i - 1] + 1 == s[i])
          count++;
        else {
          best = max(best, count);
          count = 1;
        }
      }
      cout << best << endl;
    }

    else {
      int best = 1;
      for (int i = 1; i <= n; i++) {
        s[0] = i;
        sort(s, s + k);
        int count = 1;
        for (int j = 1; j < k; j++) {
          if (s[j - 1] + 1 == s[j])
            count++;
          else {
            best = max(best, count);
            count = 1;
          }
        }
      }
      cout << best << endl;
    }
  }
  return 0;
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (!n && !m)
      break;
    bool card[100001] = {false};
    for (int i = 0; i < m; i++) {
      int a;
      cin >> a;
      card[a] = true;
    }

    int ma = 1;
    for (int i = 1; i <= n; i++) {
      int sum = 0;
      bool flag = card[0];
      int j = i;
      while (j <= n) {
        if (card[j] == true || flag) {
          sum++;
          if (card[j] == false && flag == true)
            flag = false;
          j++;
        } else
          break;
      }
      ma = max(ma, sum);
    }
    cout << ma << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (!n && !m)
      break;
    bool card[10001] = {false};
    for (int i = 0; i < m; i++) {
      int a;
      cin >> a;
      card[a] = true;
    }

    int ma = 1;
    for (int i = 1; i <= n; i++) {
      int sum = 0;
      bool flag = card[0];
      int j = i;
      while (j <= n) {
        if (card[j] == true || flag) {
          sum++;
          if (card[j] == false && flag == true)
            flag = false;
          j++;
        } else
          break;
      }
      ma = max(ma, sum);
    }
    cout << ma << 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: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n, k, 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])
      cout << b[roop] << endl;
  }
  return 0;
}