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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define BP                                                                     \\
  while (1) {                                                                  \\
  }
typedef pair<int, int> pii;
static const int INF = 1LL << 61;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  while (cin >> n >> k, n) {
    bool c[100005] = {false};
    for (int i = 0; i < k; ++i) {
      int t;
      cin >> t;
      c[t] = true;
    }

    int ans = 0, number[100005] = {0}, sum = 0;
    bool flg = false;
    for (int i = 1; i <= n; ++i) {
      if (!c[i])
        continue;
      number[i] += 1 + number[i - 1];
    }

    for (int i = 1; i <= n; ++i) {
      if (c[i]) {
        sum++;
      } else if (!flg && c[0]) {
        flg = true;
        sum++;
      } else {
        flg = true;
        ans = max(ans, sum);
        if (c[0]) {
          sum = number[i - 1] + 1;
        } else
          sum = 0;
      }
    }

    cout << max(ans, sum) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// #define int long long

signed main() {
  while (1) {
    int n, k, a[100001];
    bool isBlank = false;
    cin >> n >> k;
    if (n == 0)
      return 0;
    for (int i = 0; i < k; ++i) {
      cin >> a[i];
      if (a[i] == 0) {
        isBlank = true;
        i--;
        k--;
      }
    }
    sort(a, a + k);

    int max = 0;
    for (int i = 0; i < k; ++i) {
      bool useBlank = false;

      int count = 1;
      while (i + count < k) {
        if (a[i + count] != a[i + count - 1] + 1) {
          if (isBlank && !useBlank && (a[i + count] - a[i + count - 1] == 2)) {
            int temp = a[i + count] - 1;
            for (int j = 0; j < k; ++j) {
              if (a[j] == temp)
                goto end;
            }
            useBlank = true;
            count++;
          } else {
            break;
          }
        }
        count++;
      }
    end:;
      if (max < count) {
        max = count;
      }
    }

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

#include <algorithm>
#include <cstdio>
using namespace std;
int n, k;
int card[100000];
int main() {
  while (scanf("%d%d", &n, &k)) {
    if (!n && !k)
      break;
    for (int i = 0; i < k; i++)
      scanf("%d", &card[i]);
    sort(card, card + k);
    int res = 0, cnt = 1;
    if (card[0]) {
      for (int i = 1; i < k; i++) {
        if (card[i] - card[i - 1] == 1)
          cnt++;
        else {
          res = max(res, cnt);
          cnt = 1;
        }
      }
      printf("%d\\n", max(res, cnt));
    } else {
      int flag = 1, pos = 0;
      for (int i = 2; i < k; i++) {
        if (card[i] - card[i - 1] == 1) {
          cnt++;
        } else if (flag && card[i] - card[i - 1] == 2) {
          pos = i;
          flag = 0;
          cnt += 2;
        } else if (card[i] - card[i - 1] == 2) {
          res = max(res, cnt);
          cnt = 1;
          i = pos;
          flag = 1;
        } else {
          res = max(res, cnt);
          cnt = 1;
          flag = 1;
        }
      }
      printf("%d\\n", max(res, cnt));
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
struct card {
  int num, flag;
};
int main() {
  int n, k;
  while (scanf("%d%d", &n, &k)) {
    int deck[k];
    for (int i = 0; i < k; i++)
      scanf("%d", &deck[i]);
    sort(deck, deck + k);
    int flag = deck[0], res = 0;
    if (flag) {
      int dp[k + 1];
      dp[0] = 1;
      for (int i = 1; i < k; i++) {
        if (deck[i - 1] == deck[i] - 1) {
          dp[i] = dp[i - 1] + 1;
        } else {
          dp[i] = 1;
        }
        res = max(res, dp[i]);
      }
    } else {
      card dp[k + 1];
      dp[0].num = 1;
      dp[0].flag = 1;
      for (int i = 1; i < k; i++) {
        if (deck[i - 1] == deck[i] - 1) {
          dp[i].num = dp[i - 1].num + 1;
          dp[i].flag = dp[i - 1].flag;
        } else if (dp[i - 1].flag && deck[i - 1] == deck[i] - 2) {
          dp[i].num = dp[i - 1].num + 1;
          dp[i].flag = 0;
        } else {
          dp[i].num = 1;
          dp[i].flag = 1;
        }
        res = max(res, dp[i].num);
      }
    }
    printf("%d\\n", res);
  }
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, k;

  while (cin >> n >> k, n || k) {
    int C[100002] = {0};
    int dp[100002] = {0};

    for (int i = 0; i < k; i++) {
      int j;
      cin >> j;
      C[j] = 1;
    }
    if (C[1] == 1) {
      dp[1] = 1;
    }
    for (int i = 2; i <= n; i++) {
      if (C[i] == 1) {
        if (dp[i - 1] > 0) {
          dp[i] = dp[i - 1] + 1;
        } else {
          dp[i] = 1;
        }
      }
    }
    for (int i = n; i > 0; i--) {
      if (C[i] == 1) {
        if (dp[i + 1] > 0) {
          dp[i] = dp[i + 1];
        }
      }
    }
    int ans = 0;
    if (C[0] == 1) {
      for (int i = 2; i <= n; i++) {
        ans = max(ans, dp[i]);
        if (C[i] == 0) {
          ans = max(ans, dp[i - 1] + dp[i + 1] + 1);
        }
      }
    } else {
      for (int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n, k;

  while (cin >> n >> k, n || k) {
    int C[10002] = {0};
    int dp[10002] = {0};

    for (int i = 0; i < k; i++) {
      int j;
      cin >> j;
      C[j] = 1;
    }
    if (C[1] == 1) {
      dp[1] = 1;
    }
    for (int i = 2; i <= n; i++) {
      if (C[i] == 1) {
        if (dp[i - 1] > 0) {
          dp[i] = dp[i - 1] + 1;
        } else {
          dp[i] = 1;
        }
      }
    }
    for (int i = n; i > 0; i--) {
      if (C[i] == 1) {
        if (dp[i + 1] > 0) {
          dp[i] = dp[i + 1];
        }
      }
    }
    int ans = 0;
    if (C[0] == 1) {
      for (int i = 2; i <= n; i++) {
        ans = max(ans, dp[i]);
        if (C[i] == 0) {
          ans = max(ans, dp[i - 1] + dp[i + 1] + 1);
        }
      }
    } else {
      for (int i = 1; i <= n; i++) {
        ans = max(ans, dp[i]);
      }
    }
    cout << ans << endl;
  }
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

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

#pragma warning(disable : 4996)

using namespace std;

int main() {
  int n, k, c;
  bool t;

  while (true) {
    t = false;

    scanf("%d", &n);
    scanf("%d", &k);

    if (n == 0 && k == 0) {
      break;
    }

    vector<bool> r(n, false);

    for (int i = 0; i < k; i++) {
      scanf("%d", &c);

      if (c != 0) {
        r[c - 1] = true;
      } else {
        t = true;
      }
    }

    vector<bool> d;
    vector<int> e;
    int sum = 1;

    for (int i = 1; i < n; i++) {
      if (r[i - 1] != r[i]) {
        d.push_back(r[i - 1]);
        e.push_back(sum);

        sum = 1;
      } else {
        sum++;
      }
    }

    d.push_back(r[n - 1]);
    e.push_back(sum);

    int ret = 0;

    if (t) {
      if (d.size() >= 3) {
        for (int i = 0; i < d.size(); i++) {
          if (d[i] && i >= 2) {
            if (e[i - 1] == 1) {
              ret = max(ret, e[i - 2] + e[i] + 1);
            } else {
              ret = max(ret, max(e[i - 2], e[i]) + 1);
            }
          } else if (d[i]) {
            ret = max(ret, e[i] + i);
          }
        }
      } else {
        for (int i = 0; i < d.size(); i++) {
          if (d[i]) {
            ret += e[i];
          }
        }

        ret += 1;
      }
    } else {
      for (int i = 0; i < d.size(); i++) {
        if (d[i]) {
          ret = max(ret, e[i]);
        }
      }
    }

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

  return 0;
}

Error: TLE

Fixed submission:

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

#pragma warning(disable : 4996)

using namespace std;

int main() {
  int n, k, c;
  bool t;

  while (true) {
    scanf("%d", &n);
    scanf("%d", &k);

    if (n == 0 && k == 0) {
      break;
    }

    vector<bool> r(n, false);

    for (int i = 0; i < n; i++) {
      scanf("%d", &c);

      if (c != 0) {
        r[c - 1] = true;
      } else {
        t = true;
      }
    }

    vector<bool> d;
    vector<int> e;
    int sum = 1;

    for (int i = 1; i < n; i++) {
      if (r[i - 1] != r[i]) {
        d.push_back(r[i - 1]);
        e.push_back(sum);

        sum = 1;
      } else {
        sum++;
      }
    }

    d.push_back(r[k - 1]);
    e.push_back(sum);

    int ret = 0;

    if (t) {
      if (d.size() >= 3) {
        for (int i = 2; i < d.size(); i++) {
          if (d[i]) {
            if (e[i - 1] == 1) {
              ret = max(ret, e[i - 2] + e[i] + 1);
            } else {
              ret = max(e[i - 2], e[i]) + 1;
            }
          }
        }
      } else {
        for (int i = 0; i < d.size(); i++) {
          if (d[i]) {
            ret += e[i];
          }
        }

        ret += 1;
      }
    } else {
      for (int i = 0; i < d.size(); i++) {
        if (d[i]) {
          ret = max(ret, e[i]);
        }
      }
    }

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

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

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a, 0, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1 << 29
using namespace std;

int a[101010];

void solve() {
  int n, k;
  for (;;) {
    scanf("%d %d", &n, &k);
    if (n == 0)
      break;
    FOR(i, k) scanf("%d", &a[i]);
    sort(a, a + k);
    int ma = 0;
    FOR(i, k) {
      if (a[i] == 0)
        continue;
      FORR(j, i + 1, k) {
        if (!a[0]) {
          if (a[j] - a[i] <= j - i + 1)
            ma = max(ma, a[j] - a[i] + 1);
          else
            break;
        } else {
          if (a[j] - a[i] == j - i)
            ma = max(ma, a[j] - a[i] + 1);
          else
            break;
        }
      }
    }
    printf("%d\\n", ma);
  }
}

int main() {
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a, 0, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1 << 29
using namespace std;

int a[1000];

void solve() {
  int n, k;
  for (;;) {
    scanf("%d %d", &n, &k);
    if (n == 0)
      break;
    FOR(i, k) scanf("%d", &a[i]);
    sort(a, a + k - 1);
    int ma = 0;
    FOR(i, k) {
      if (a[i] == 0)
        continue;
      FORR(j, i + 1, k) {
        if (!a[0]) {
          if (a[j] - a[i] <= j - i + 1)
            ma = max(ma, a[j] - a[i] + 1);
          else
            break;
        } else {
          if (a[j] - a[i] == j - i)
            ma = max(ma, a[j] - a[i] + 1);
          else
            break;
        }
      }
    }
    printf("%d\\n", ma);
  }
}

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

#include <iostream>
using namespace std;

int main() {
  while (1) {
    bool a[100000 + 2];
    fill(a, a + 100002, false);
    int n, k, b, ans = 0;
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < k; i++) {
      scanf("%d", &b);
      a[b] = true;
    }
    int c = 0, d = 0;
    if (a[0]) {
      for (int i = 1; i < n + 1; i++) {
        if (a[i])
          d++;
        if (!a[i]) {
          if (c + d + 1 > ans)
            ans = c + d + 1;
          if (a[i + 1]) {
            c = d;
            d = 1;
          }
          if (!a[i + 1]) {
            c = 0;
            d = 0;
          }
          i++;
        }
      }
      if (c + d + 1 > ans)
        ans = c + d + 1;
    }
    if (!a[0]) {
      for (int i = 1; i < n + 1; i++) {
        if (a[i])
          d++;
        if (!a[i]) {
          if (ans < d)
            ans = d;
          d = 0;
        }
      }
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int solve(int k, bool a[100002]) {
  int c = 0, ans = 0;
  for (int i = 1; i < k + 1; i++) {
    if (a[i]) {
      c++;
      if (ans < c)
        ans = c;
    }
    if (!a[i]) {
      if (ans < c)
        ans = c;
      c = 0;
    }
  }
  return ans;
}

int main() {
  int n, k, c, b, ans = 0;
  bool h;
  bool a[100002];

  while (cin >> n >> k) {
    ans = 0;
    for (int i = 0; i < 100002; i++)
      a[i] = false;
    for (int i = 0; i < k; i++) {
      cin >> b;
      a[b] = true;
    }
    if (!a[0])
      ans = solve(n, a);
    if (a[0]) {
      ans = solve(n, a) + 1;
      for (int i = 1; i < n + 1; i++) {
        while (1) {
          if (a[i])
            i++;
          if (!a[i])
            break;
        }
        if (!a[i] && (a[i - 1] && a[i + 1])) {
          a[i] = true;
          c = solve(n, a);
          if (c > ans)
            ans = c;
          a[i] = false;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    if (!N)
      break;
    vector<int> oks(N + 1);
    bool flag = false;
    for (int i = 0; i < K; ++i) {
      int a;
      cin >> a;
      oks[a] = true;
      if (!a)
        flag = true;
    }
    int a = 0;
    int b = 0;
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
      if (oks[i]) {
        a++;
        b++;
        ans = max(ans, a);
        if (flag)
          ans = max(ans, b);
      } else {
        b = a + 1;
        a = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    vector<int> oks(N + 1);
    bool flag = false;
    for (int i = 0; i < K; ++i) {
      int a;
      cin >> a;
      oks[a] = true;
      if (!a)
        flag = true;
    }
    int a = 0;
    int b = 0;
    int ans = 0;
    for (int i = 1; i <= N; ++i) {
      if (oks[i]) {
        a++;
        b++;
        ans = max(ans, a);
        if (flag)
          ans = max(ans, b);
      } else {
        b = a + 1;
        a = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
int legth(int a) {}
int main() {
  int x, y, a, b = 0, c = 0, d = 0, e = 0, ans = 0, k = 0;
  int g[101000], r[101000];
  while (1) {
    fill(g, g + 101000, 0);
    fill(r, r + 101000, 0);
    cin >> x >> y;
    if (x == 0 && y == 0)
      break;
    for (int u = 0; u < y; u++) {
      cin >> a;
      g[a] = 1;
    }

    for (int y = 1; y <= x; y++) {
      if (g[y] == 1) {
        b++;
        k = 0;
        if (y == x) {
          r[y] = b;
          r[y - b - 1] = b;
        }
      } else {
        if (k == 0) {
          r[y] = b;
          r[y - b + 1] = b;
        }
        k = 1;
        b = 0;
      }

      if (c < b)
        c = b;
    }
    ans = c;
    if (g[0] == 1) {

      for (int j = 1; j < x + 1; j++) {
        if (g[j] != 1) {

          d = r[j] + r[j + 2] + 1;

          if (e < d)
            e = d;
        }
      }
    }
    if (ans < e)
      ans = e;

    b = 0;
    cout << ans << endl;
    c = 0;
    ans = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
int legth(int a) {}
int main() {
  int x, y, a, b = 0, c = 0, d = 0, e = 0, ans = 0;
  int g[101000], r[101000];
  while (1) {
    cin >> x >> y;
    if (x == 0 && y == 0)
      break;
    for (int u = 0; u < y; u++) {
      cin >> a;
      g[a] = 1;
    }
    if (g[0] == 1) {

      for (int j = 1; j < x + 1; j++) {
        if (g[j] != 1) {
          g[j] = 1;
          for (int y = 1; y <= x; y++) {

            if (g[y] == 1) {
              b++;

            } else {
              b = 0;
            }
          }
          if (c < b)
            c = b;
          if (ans < c)
            ans = c;
          g[j] = 0;
        }
      }
    } else {
      for (int y = 1; y <= x; y++) {
        if (g[y] == 1) {
          b++;
        } else {
          if (c < b)
            c = b;
          b = 0;
        }
      }
      ans = c;
    }
    b = 0;
    cout << ans << endl;
    c = 0;
    ans = 0;
  }
  return 0;
}
Problem p00440 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#define _3_ true
using namespace std;

int main() {
  int n, k, index, ini;
  int in[100005];

  bool ZeroE;
  while (_3_) {
    scanf("%d %d", &n, &k);
    if (!(n | k))
      break;
    ZeroE = false;
    index = 0;

    for (int i = 0; i < k; i++) {
      scanf("%d", &ini);
      if (ini == 0)
        ZeroE = true;
      else {
        in[index] = ini;
        index++;
      }
    }

    sort(in, in + index);

    int ans = 0;
    int count = 1;
    bool used = false;
    for (int i = 1; i < index; i++) {
      // cout << "i = " << i << ", cout = " << count  << ", in[i] = " << in[i]<<
      // endl;
      if (in[i] == in[i - 1] + 1)
        count++;
      else {
        if (ZeroE && !used && in[i] == in[i - 1] + 2) {
          count += 2;
          used = true;
        } else if (ZeroE && used && in[i] == in[i - 1] + 2) {
          ans = max(ans, count);
          count = 3;
        } else {
          ans = max(ans, count);
          count = 1;
          used = false;
        }
      }
      if (in[i] == in[i - 1] + 1 && i == index - 1 && !used && ZeroE)
        count++;
    }
    if (index == 1 && ZeroE)
      count = 2;

    ans = max(ans, count);

    cout << ans << endl;
  }
  return 0;
}

Error: 178

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define _3_ true
using namespace std;

int main() {
  int n, k, index, ini;
  int in[100005];

  bool ZeroE;
  while (_3_) {
    scanf("%d %d", &n, &k);
    if (!(n | k))
      break;
    ZeroE = false;
    index = 0;

    for (int i = 0; i < k; i++) {
      scanf("%d", &ini);
      if (ini == 0)
        ZeroE = true;
      else {
        in[index] = ini;
        index++;
      }
    }

    sort(in, in + index);

    int ans = 0;
    int count = 1;
    bool used = false;
    for (int i = 1; i < index; i++) {
      // cout << "i = " << i << ", cout = " << count  << ", in[i] = " << in[i]<<
      // endl;
      if (in[i] == in[i - 1] + 1)
        count++;
      else {
        if (ZeroE && !used && in[i] == in[i - 1] + 2) {
          count += 2;
          used = true;
        } else if (ZeroE && used && in[i] == in[i - 1] + 2) {
          ans = max(ans, count);
          count = 3;
        } else {
          ans = max(ans, count);
          count = 1;
          used = false;
        }
      }
      if (in[i] == in[i - 1] + 1 && i == index - 1 && !used && ZeroE)
        count++;
    }
    if (index == 1 && ZeroE)
      count = 2;

    ans = max(ans, count);

    cout << ans << endl;
  }
  return 434;
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>

constexpr int MAX_N = 100000;

int n, k;
int dp
    [MAX_N + 1]
    [2]; //???????????????????????????????????°???????????????????´?????????????????????£???????????????
bool IsNum[MAX_N + 1];
bool IsWhiteCard;

int main() {
  while (std::cin >> n >> k) {
    if (n + k == 0)
      break;
    std::memset(dp, 0, sizeof(dp));
    std::memset(IsNum, 0, sizeof(IsNum));
    int a;
    for (int i = 0; i < k; ++i) {
      std::cin >> a;
      if (a == 0)
        IsWhiteCard = true;
      else
        IsNum[a] = true;
    }

    for (int i = 1; i <= n; ++i) {
      if (IsNum[i]) {
        dp[i][0] = dp[i - 1][0] + 1;
        dp[i][1] = dp[i - 1][1] + 1;
      } else if (IsWhiteCard)
        dp[i][1] = dp[i - 1][0] + 1;
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      ans = std::max(ans, dp[i][1]);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

constexpr int MAX_N = 100000;

int n, k;
int a[MAX_N];
bool flag;

int main() {
  while (1) {
    std::cin >> n >> k;

    if (n + k == 0)
      break;

    flag = false;
    for (int i = 0; i < k; ++i) {
      int aa;
      std::cin >> aa;
      if (!aa)
        flag = true;
      else
        a[i] = aa;
    }
    if (flag)
      std::sort(a, a + k);
    else
      std::sort(a, a + k - 1);

    int prev = a[0];
    int s = 0, t = 1, res = 0;

    while (1) {
      while (t < n && prev == a[t]) {
        prev = a[t];
        ++t;
      }

      if (t == n) {
        res = std::max(t - s, res);
        break;
      }

      if (flag) {
        prev = a[t];
        ++t;
        while (t < n && prev == a[t]) {
          prev = a[t];
          ++t;
        }
      }

      res = std::max(res, t - s);
      ++s;
    }
    std::cout << res << std::endl;
  }
  return 0;
}
Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int n, k;
int dat[100002];
int main(void) {
  scanf("%d%d", &n, &k);
  int i, j;
  int no = 0, ls = 0, ans = 0;
  bool lf = false, cf = false;
  while (n != 0) {
    for (i = 0; i < k; i++)
      scanf("%d", &(dat[i]));
    sort(dat, dat + k);
    dat[k] = 100005;
    i = 0;
    if (dat[i] == 0) {
      cf = true;
      i++;
      if (dat[i] != 1)
        lf = true;
    }
    while (i < k) {
      j = i;
      while (j < k && dat[j + 1] == dat[j] + 1)
        j++;
      ls = no;
      no = j - i + 1;
      i = j;
      if (cf && lf) {
        ans = ans < no + ls + 1 ? no + ls + 1 : ans;
      } else {
        ans = ans < no ? no : ans;
      }
      if (dat[j + 1] == dat[j] + 2)
        lf = true;
      i++;
    }
    printf("%d\\n", ans);
    scanf("%d%d", &n, &k);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int N[100001];
int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 1; i <= k; i++) {
      cin >> N[i];
    }
    for (int i = 1; i < k; i++) {
      for (int j = i + 1; j <= k; j++) {
        if (N[i] > N[j]) {
          swap(N[i], N[j]);
        }
      }
    }
    int GK[k + 1];
    GK[0] = 1;
    GK[1] = 1;
    for (int i = 1; i <= k; i++) {
      if (N[1] != 0) {
        if (N[i] + 1 == N[i + 1])
          GK[i] = GK[i - 1] + 1;
        else
          GK[i] = 1;
      } else {
        if (i == 1)
          continue;
        else {
          if (N[i] + 1 == N[i + 1])
            GK[i] = GK[i - 1] + 1;
          else
            GK[i] = 1;
        }
      }
    }
    if (N[1] != 0) {
      int max = 0;
      for (int i = 1; i <= k; i++) {
        if (max < GK[i])
          max = GK[i];
      }
      cout << max << endl;
    } else {
      int maxl = 0;
      for (int i = 2; i < k; i++) {
        if (maxl < GK[i] + GK[i + 1] + 1) {
          maxl = GK[i] + GK[i + 1] + 1;
        }
      }
      cout << maxl << endl;
    }
  }
}