prithivMLmods/Coder-Stat huggingface dataset:
Problem p00451 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
string s[2];
int search(int id) {
  int id2 = (id + 1) % 2;
  int ret = 0;
  int a[40004];
  for (int j = (int)s[id2].size() - 1; j > -(int)s[id].size(); j--) {
    memset(a, 0, sizeof(a));
    for (int i = 0; i < (int)s[id].size(); i++) {
      if (i + j < 0)
        continue;
      if (i + j >= (int)s[id2].size())
        continue;
      if (s[id][i] == s[id2][i + j])
        a[i] = 1;
    }
    int co = 0;
    for (int i = 0; i < (int)s[id].size() + 1; i++) {
      if (a[i])
        co++;
      else {
        ret = max(ret, co);
        co = 0;
      }
    }
  }
  return ret;
}
int main() {
  while (cin >> s[0]) {
    cin >> s[1];
    printf("%d\n", max(search(0), search(1)));
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
string s[2];
int search(int id) {
  int id2 = (id + 1) % 2;
  int ret = 0;
  int a[40004];
  for (int j = (int)s[id2].size() - 1; j > -(int)s[id].size(); j--) {
    memset(a, 0, sizeof(a));
    for (int i = 0; i < (int)s[id].size(); i++) {
      if (i + j < 0)
        continue;
      if (i + j >= (int)s[id2].size())
        continue;
      if (s[id][i] == s[id2][i + j])
        a[i] = 1;
    }
    int co = 0;
    for (int i = 0; i < (int)s[id].size() + 1; i++) {
      if (a[i])
        co++;
      else {
        ret = max(ret, co);
        co = 0;
      }
    }
  }
  return ret;
}
int main() {
  while (cin >> s[0]) {
    cin >> s[1];
    printf("%d\n", search(0));
  }
}
Problem p00451 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\n", x);

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

char str1[4010];
char str2[4010];

int main() {
  while (~scanf("%s %s", str1, str2)) {
    int ans = 0;
    int len1 = strlen(str1);
    int len2 = strlen(str2);

    for (int i = 0; i + ans < len1; i++) {
      for (int j = 0; j + ans < len2; j++) {
        if (str1[i] == str2[j]) {
          int tmp = 0;
          int ii = i;
          int jj = j;
          while (str1[ii] && str1[ii++] == str2[jj++])
            ++tmp;
          ans = max(ans, tmp);
        }
      }
    }
    print(ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\n", x);

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

char str1[4010];
char str2[4010];

int main() {
  while (~scanf("%s %s", str1, str2)) {
    int ans = 0;
    int len1 = strlen(str1);
    int len2 = strlen(str2);

    for (int off = -len1; off <= len2; off++) {
      int len = 0;
      for (int i = max(off, 0); i < min(len1 + off, len2); i++) {
        if (str1[i - off] == str2[i])
          ans = max(ans, ++len);
        else
          len = 0;
      }
    }
    print(ans);
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <string.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int INF = 1001001001;
char A[4002], B[4002];
int DP[4002];

int main() {
  while (1) {
    scanf("%s", A);
    scanf("%s", B);
    int ans = 0;
    rep(i, strlen(A)) {
      for (int j = strlen(B); j > -1; j--) {
        if (A[i] == B[j]) {
          if (i != 0 && j != 0) {
            DP[j] = DP[j - 1];
          } else
            DP[j] = 0;
          DP[j]++;
        } else {
          DP[j] = 0;
        }
        ans = max(ans, DP[j]);
      }
    }
    printf("%d\n", ans);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <string.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int INF = 1001001001;
char A[4002], B[4002];
int DP[4002];

int main() {
  while (scanf("%s", A) != EOF) {
    scanf("%s", B);
    int ans = 0;
    rep(i, strlen(A)) {
      for (int j = strlen(B); j > -1; j--) {
        if (A[i] == B[j]) {
          if (i != 0 && j != 0) {
            DP[j] = DP[j - 1];
          } else
            DP[j] = 0;
          DP[j]++;
        } else {
          DP[j] = 0;
        }
        ans = max(ans, DP[j]);
      }
    }
    printf("%d\n", ans);
  }
}
Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  const int MAX_N = 105;
  int n, m, p[MAX_N];
  int x[MAX_N * MAX_N];
  while (true) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < n; i++)
      scanf("%d", &p[i]);
    p[n] = 0;
    n++;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        x[i * n + j] = p[i] + p[j];
      }
    }
    sort(x, x + n * n);

    int ans = 0;
    for (int i = 0; i < n * n; i++) {
      int mp = m - x[i];
      if (mp < 0)
        continue;
      int l = 0, r = n * n - 1;
      int y = 0;
      while (r - l >= 1) {
        int c = (r + l) / 2;
        if (x[c] == mp) {
          y = x[c];
          break;
        } else if (x[c] < mp) {
          y = x[c];
          l = c + 1;
        } else
          r = c;
      }
      ans = max(ans, y + x[i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  const int MAX_N = 1005;
  int n, m, p[MAX_N];
  int x[MAX_N * MAX_N];
  while (true) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < n; i++)
      scanf("%d", &p[i]);
    p[n] = 0;
    n++;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        x[i * n + j] = p[i] + p[j];
      }
    }
    sort(x, x + n * n);

    int ans = 0;
    for (int i = 0; i < n * n; i++) {
      int mp = m - x[i];
      if (mp < 0)
        continue;
      int l = 0, r = n * n - 1;
      int y = 0;
      while (r - l >= 1) {
        int c = (r + l) / 2;
        if (x[c] == mp) {
          y = x[c];
          break;
        } else if (x[c] < mp) {
          y = x[c];
          l = c + 1;
        } else
          r = c;
      }
      ans = max(ans, y + x[i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef int INT;
typedef vector<INT> VI;
typedef set<INT> SI;

INT P[1002];
INT Q[1002001];

INT solve(int N, INT M) {
  INT answer = 0;
  for (int i = 0; i < N; ++i) {
    int q = Q[i];
    int L = 0;
    int R = N;
    INT m = M - q;
    while (L < R) {
      int p = (R - L) / 2 + L;
      INT t = q + Q[p];
      if (t <= M)
        answer = max(answer, t);
      if (Q[p] > m) {
        R = p - 1;
      } else {
        L = p + 1;
      }
    }
  }
  return answer;
}

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; ++i) {
      INT p;
      cin >> p;
      P[i] = p;
    }
    P[n] = 0;
    for (int i = 0; i < n + 1; ++i) {
      for (int j = 0; j < n + 1; ++j) {
        Q[i * (n + 1) + j] = P[i] + P[j];
      }
    }
    int length = (n + 1) * (n + 1);
    sort(Q, Q + length);
    cout << solve(length, m) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef int INT;
typedef vector<INT> VI;
typedef set<INT> SI;

INT P[1002];
INT Q[1002001];

INT solve(int N, INT M) {
  INT answer = 0;
  for (int i = 0; i < N; ++i) {
    int q = Q[i];
    int L = i;
    int R = N;
    INT m = M - q;
    while (L < R) {
      int p = (R - L) / 2 + L;
      INT t = q + Q[p];
      if (t <= M)
        answer = max(answer, t);
      if (Q[p] > m) {
        R = p - 1;
      } else {
        L = p + 1;
      }
    }
  }
  return answer;
}

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; ++i) {
      INT p;
      cin >> p;
      P[i] = p;
    }
    P[n] = 0;
    for (int i = 0; i < n + 1; ++i) {
      for (int j = 0; j < n + 1; ++j) {
        Q[i * (n + 1) + j] = P[i] + P[j];
      }
    }
    int length = (n + 1) * (n + 1);
    sort(Q, Q + length);
    cout << solve(length, m) << endl;
  }
  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include "stdio.h"
#include <algorithm>
#include <iostream>

using namespace std;

long long int gten[1000000];
long long int p[1111];

long long int ans[1000];

int main() {
  int ww = 0;
  while (1) {
    long long int n, m;
    scanf("%lld %lld", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    p[0] = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &(p[i]));
    }
    int nw = 0;
    for (int i = 0; i <= n; i++) {
      for (int ii = 0; ii <= n; ii++) {
        gten[nw] = p[i] + p[ii];
        nw++;
      }
    }
    sort(gten, gten + nw);
    long long int max = 0;
    int ai = 0;
    for (int i = nw - 1; i >= 0; i--) {
      while (1) {
        if (ai + 1 >= nw) {
          break;
        }
        if (gten[i] + gten[ai + 1] <= m) {
          ai++;
          if (max < gten[i] + gten[ai]) {
            max = gten[i] + gten[ai];
          }
        } else {
          break;
        }
      }
    }
    ans[ww] = max;
    ww++;
  }
  for (int i = 0; i < ww; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}

Fixed submission:
#include "stdio.h"
#include <algorithm>
#include <iostream>

using namespace std;

long long int gten[1111111];
long long int p[1111];

long long int ans[1000];

int main() {
  int ww = 0;
  while (1) {
    long long int n, m;
    scanf("%lld %lld", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    p[0] = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &(p[i]));
    }
    int nw = 0;
    for (int i = 0; i <= n; i++) {
      for (int ii = 0; ii <= n; ii++) {
        gten[nw] = p[i] + p[ii];
        nw++;
      }
    }
    sort(gten, gten + nw);
    long long int max = 0;
    int ai = 0;
    for (int i = nw - 1; i >= 0; i--) {
      while (1) {
        if (ai + 1 >= nw) {
          break;
        }
        if (gten[i] + gten[ai + 1] <= m) {
          ai++;
          if (max < gten[i] + gten[ai]) {
            max = gten[i] + gten[ai];
          }
        } else {
          break;
        }
      }
    }
    ans[ww] = max;
    ww++;
  }
  for (int i = 0; i < ww; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(void) {

  unsigned int N, M;
  while (cin >> N >> M, N || M) {
    vector<unsigned int> P(N + 1);
    P[0] = 0;
    for (int i = 1; i <= N; i++)
      cin >> P[i];

    vector<unsigned int> P2;
    for (int i = 0; i <= N; i++) {
      for (int j = i; j <= N; j++) {
        P2.push_back(P[i] + P[j]);
      }
    }
    sort(P2.begin(), P2.end());

    unsigned int ans = 0;
    for (int i = 0; i < P2.size(); i++) {
      if (P2[i] > M)
        continue;
      unsigned int left = 0, right = P2.size() - 1;
      unsigned int mid;
      for (int j = 0; j < 100; j++) {
        mid = (left + right) / 2;
        if (P2[i] + P2[mid] > M) {
          right = mid;
        } else {
          left = mid;
        }
      }
      ans = max(ans, P2[i] + P2[mid]);
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(void) {

  unsigned int N, M;
  while (cin >> N >> M, N || M) {
    vector<unsigned int> P(N + 1);
    P[0] = 0;
    for (int i = 1; i <= N; i++)
      cin >> P[i];

    vector<unsigned int> P2;
    for (int i = 0; i <= N; i++) {
      for (int j = i; j <= N; j++) {
        P2.push_back(P[i] + P[j]);
      }
    }
    sort(P2.begin(), P2.end());

    unsigned int ans = 0;
    for (int i = 0; i < P2.size(); i++) {
      if (P2[i] > M)
        break;
      unsigned int left = 0, right = P2.size() - 1;
      unsigned int mid;
      for (int j = 0; j < 100; j++) {
        mid = (left + right) / 2;
        if (P2[i] + P2[mid] > M) {
          right = mid;
        } else {
          left = mid;
        }
      }
      ans = max(ans, P2[i] + P2[mid]);
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
int n, m;
int a[1001];
int b[51000];
bool f[1001][1001];
int i, j;
int ans;
int c;

int max(int a, int b) {
  if (a >= b)
    return a;
  else
    return b;
}

int comp(const void *a, const void *b) {
  int p = *(int *)a;
  int q = *(int *)b;
  return p - q;
}

int search(int num) {
  int r = 0;
  int l = c;
  int mid;
  for (int k = 0; k < 25; k++) {
    mid = (int)(r + l) / 2;
    if (num - b[mid + 1] >= 0 && num - b[mid] > num - b[mid + 1])
      mid = mid + 1;
    if (b[mid] > num)
      l = mid;
    else
      r = mid;
  }
  return mid;
}

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    ans = 0;
    c = 0;
    for (i = 0; i < n; i++)
      scanf("%d", &a[i]);
    a[n] = 0;
    for (i = 0; i < n + 1; i++)
      for (j = 0; j < n + 1; j++)
        f[i][j] = false;
    for (i = 0; i < n + 1; i++)
      for (j = 0; j < n + 1; j++) {
        if (!f[j][i] && a[i] + a[j] <= m) {
          b[c++] = a[i] + a[j];
          f[i][j] = true;
        }
      }
    qsort(b, c, sizeof(int), comp);
    for (i = 0; i < c; i++) {
      if (m - b[i] >= 0) {
        ans = max(ans, b[search(m - b[i])] + b[i]);
      }
    }
    printf("%d\n", ans);
  }
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>
int n, m;
int a[1001];
int b[510000];
bool f[1001][1001];
int i, j;
int ans;
int c;

int max(int a, int b) {
  if (a >= b)
    return a;
  else
    return b;
}

int comp(const void *a, const void *b) {
  int p = *(int *)a;
  int q = *(int *)b;
  return p - q;
}

int search(int num) {
  int r = 0;
  int l = c;
  int mid;
  for (int k = 0; k < 25; k++) {
    mid = (int)(r + l) / 2;
    if (num - b[mid + 1] >= 0 && num - b[mid] > num - b[mid + 1])
      mid = mid + 1;
    if (b[mid] > num)
      l = mid;
    else
      r = mid;
  }
  return mid;
}

int main() {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    ans = 0;
    c = 0;
    for (i = 0; i < n; i++)
      scanf("%d", &a[i]);
    a[n] = 0;
    for (i = 0; i < n + 1; i++)
      for (j = 0; j < n + 1; j++)
        f[i][j] = false;
    for (i = 0; i < n + 1; i++)
      for (j = 0; j < n + 1; j++) {
        if (!f[j][i] && a[i] + a[j] <= m) {
          b[c++] = a[i] + a[j];
          f[i][j] = true;
        }
      }
    qsort(b, c, sizeof(int), comp);
    for (i = 0; i < c; i++) {
      if (m - b[i] >= 0) {
        ans = max(ans, b[search(m - b[i])] + b[i]);
      }
    }
    printf("%d\n", ans);
  }
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int tsP[1001 * 1001];
int main() {
  int N, M;
  int P[1001];
  int ans;
  int cnt;

  P[0] = 0;

  // cin >> N >> M;
  while (cin >> N >> M, N && M) {
    ans = 0;
    cnt = 0;
    for (int i = 1; i <= N; i++) {
      cin >> P[i];
    }

    for (int i = 0; i <= N; i++) {
      for (int j = i; j <= N; j++) {
        if (P[i] + P[j] <= M) {
          tsP[cnt++] = P[i] + P[j];
        }
      }
    }
    sort(tsP, tsP + cnt);
    for (int i = 0; i < cnt; i++) {
      for (int j = i; j < cnt; j++) {
        if (tsP[i] + tsP[j] <= M) {
          ans = max(ans, tsP[i] + tsP[j]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int tsP[1001 * 1001];
int main() {
  int N, M;
  int P[1001];
  int ans;
  int cnt;

  P[0] = 0;

  // cin >> N >> M;
  while (cin >> N >> M, N && M) {
    ans = 0;
    cnt = 0;
    for (int i = 1; i <= N; i++) {
      cin >> P[i];
    }

    for (int i = 0; i <= N; i++) {
      for (int j = i; j <= N; j++) {
        if (P[i] + P[j] <= M) {
          tsP[cnt++] = P[i] + P[j];
        }
      }
    }
    sort(tsP, tsP + cnt);
    for (int i = 0; i < cnt; i++) {
      // for (int j = i; j < cnt; j++) {
      // if (tsP[i] + tsP[j] <= M) {
      ans = max(ans, /*tsP[i] + tsP[j]*/ tsP[i] +
                         *(upper_bound(tsP, tsP + cnt, M - tsP[i]) - 1));
      //}
      //}
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  while (1) {
    int n, m, a, ans = 0;
    vector<int> p, pp;
    scanf("%d%d", &n, &m);
    p.push_back(0);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a);
      p.push_back(a);
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        pp.push_back(p[i] + p[j]);
      }
    }
    sort(pp.begin(), pp.end());
    int s = pp.size() - 1;
    for (int i = 0; i <= s; i++) {
      while (pp[i] + pp[s] > m)
        s--;
      if (ans < pp[i] + pp[s])
        ans = pp[i] + pp[s];
    }
    printf("%d\n", ans);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  while (1) {
    int n, m, a, ans = 0;
    vector<int> p, pp;
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    p.push_back(0);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a);
      p.push_back(a);
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        pp.push_back(p[i] + p[j]);
      }
    }
    sort(pp.begin(), pp.end());
    int s = pp.size() - 1;
    for (int i = 0; i <= s; i++) {
      while (pp[i] + pp[s] > m)
        s--;
      if (ans < pp[i] + pp[s])
        ans = pp[i] + pp[s];
    }
    printf("%d\n", ans);
  }
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define int long long
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
static const int INF = 1 << 24;

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

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

signed main() {
  int n;
  int m;
  while (cin >> n >> m, n || m) {
    // int n;
    // cin>>n;
    // int m;
    // cin>>m;
    vint v(n);
    rep(i, n) { cin >> v[i]; }
    v.PB(0);
    vint tmp;
    rep(i, n + 1) {
      rep(j, n + 1) { tmp.PB(v[i] + v[j]); }
    }
    sort(ALL(tmp));
    int ans = 0;
    tmp.erase(unique(ALL(tmp)), tmp.end());
    rep(i, tmp.size()) {
      rep(j, tmp.size()) {
        if (tmp[i] + tmp[j] <= m) {
          ans = max(ans, tmp[i] + tmp[j]);
        }
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define int long long
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
static const int INF = 1 << 24;

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

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

signed main() {
  int n;
  int m;
  while (cin >> n >> m, n || m) {
    // int n;
    // cin>>n;
    // int m;
    // cin>>m;
    vint v(n);
    rep(i, n) { cin >> v[i]; }
    v.PB(0);
    vint tmp;
    rep(i, n + 1) {
      rep(j, n + 1) { tmp.PB(v[i] + v[j]); }
    }
    sort(ALL(tmp));
    int ans = 0;
    tmp.erase(unique(ALL(tmp)), tmp.end());
    rep(i, tmp.size()) {
      vint::iterator it = upper_bound(ALL(tmp), m - tmp[i]);
      it--;
      if (*it + tmp[i] <= m) {
        ans = max(ans, *it + tmp[i]);
      }
    }
    cout << ans << endl;
  }
}
Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
static const int MAX_N = 100;
int main() {
  int N, M;
  int point[MAX_N + 1];
  int a[(MAX_N + 1) * (MAX_N + 1)];
  for (;;) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;
    point[0] = 0;
    for (int i = 1; i <= N; i++)
      scanf("%d", &point[i]);
    int index = 0;
    for (int i = 0; i < N + 1; i++) {
      for (int j = 0; j < N + 1; j++) {
        a[index] = point[i] + point[j];
        index++;
      }
    }
    sort(a, a + (N + 1) * (N + 1));
    int num_a = (N + 1) * (N + 1);
    int ans = 0;
    for (int i = 0; i < num_a; i++) {
      if (a[i] > M)
        break;
      int lb = 0, ub = num_a - 1;
      int mid;
      while (ub - lb > 1) {
        mid = (lb + ub) / 2;
        if (a[i] + a[mid] > M)
          ub = mid;
        else
          lb = mid;
      }
      mid = (lb + ub) / 2;
      if (a[i] + a[mid] > ans) {
        ans = a[i] + a[mid];
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
static const int MAX_N = 1000;
int main() {
  int N, M;
  int point[MAX_N + 1];
  int a[(MAX_N + 1) * (MAX_N + 1)];
  for (;;) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;
    point[0] = 0;
    for (int i = 1; i <= N; i++)
      scanf("%d", &point[i]);
    int index = 0;
    for (int i = 0; i < N + 1; i++) {
      for (int j = 0; j < N + 1; j++) {
        a[index] = point[i] + point[j];
        index++;
      }
    }
    sort(a, a + (N + 1) * (N + 1));
    int num_a = (N + 1) * (N + 1);
    int ans = 0;
    for (int i = 0; i < num_a; i++) {
      if (a[i] > M)
        break;
      int lb = 0, ub = num_a - 1;
      int mid;
      while (ub - lb > 1) {
        mid = (lb + ub) / 2;
        if (a[i] + a[mid] > M)
          ub = mid;
        else
          lb = mid;
      }
      mid = (lb + ub) / 2;
      if (a[i] + a[mid] > ans) {
        ans = a[i] + a[mid];
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

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

int N, M;
ll P[1111];
ll PP[1111];
int p;

int main(int argc, char *argv[]) {
  while (cin >> N >> M, N) {
    int NN = N * N;
    rep(i, N) cin >> P[i];
    rep(i, N) rep(j, N) PP[i + j * N] = P[i] + P[j];
    sort(P, P + N);
    sort(PP, PP + NN);
    ll res = 0;
    res = max(res, P[upper_bound(P, P + N, M) - P - 1]);
    res = max(res, PP[upper_bound(PP, PP + NN, M) - PP - 1]);
    rep(i, N) res =
        max(res, PP[upper_bound(PP, PP + NN, M - P[i]) - PP - 1] + P[i]);
    rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
        res = max(res, PP[p] + PP[i]);
    rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
        res = max(res, PP[p] + PP[i]);
    cout << res << endl;
  }

  return 0;
}

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

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

int N, M;
ll P[1111];
ll PP[11111111];
int p;

int main(int argc, char *argv[]) {
  while (cin >> N >> M, N) {
    int NN = N * N;
    rep(i, N) cin >> P[i];
    rep(i, N) rep(j, N) PP[i + j * N] = P[i] + P[j];
    sort(P, P + N);
    sort(PP, PP + NN);
    ll res = 0;
    res = max(res, P[upper_bound(P, P + N, M) - P - 1]);
    res = max(res, PP[upper_bound(PP, PP + NN, M) - PP - 1]);
    rep(i, N) res =
        max(res, PP[upper_bound(PP, PP + NN, M - P[i]) - PP - 1] + P[i]);
    rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
        res = max(res, PP[p] + PP[i]);
    rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
        res = max(res, PP[p] + PP[i]);
    cout << res << endl;
  }

  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define INF 200000000
using namespace std;

int point2[500500];

int search(int a[], int n, int key) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (a[mid] == key)
      return a[mid];
    else if (a[mid] < key)
      left = mid + 1;
    else
      right = mid;
  }
  return a[left - 1];
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    int point[n + 1];
    for (int i = 0; i < n; i++)
      cin >> point[i];
    point[n] = 0;
    int k = 0;
    for (int i = 0; i < n + 1; i++) {
      for (int j = i; j < n + 1; j++) {
        point2[k] = point[i] + point[j];
        k++;
      }
    }
    int np = k;
    sort(point2, point2 + np);

    int ans = 0;
    for (int i = 0; i < np; i++) {
      if (point2[i] > m)
        break;
      int s = search(point2, np, m - point2[i]);
      int t = s + point2[i];
      if (t > ans)
        ans = t;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define INF 200000000
using namespace std;

int point2[1000000];

int search(int a[], int n, int key) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (a[mid] == key)
      return a[mid];
    else if (a[mid] < key)
      left = mid + 1;
    else
      right = mid;
  }
  return a[left - 1];
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    int point[n + 1];
    for (int i = 0; i < n; i++)
      cin >> point[i];
    point[n] = 0;
    int k = 0;
    for (int i = 0; i < n + 1; i++) {
      for (int j = i; j < n + 1; j++) {
        point2[k] = point[i] + point[j];
        k++;
      }
    }
    int np = k;
    sort(point2, point2 + np);

    int ans = 0;
    for (int i = 0; i < np; i++) {
      if (point2[i] > m)
        break;
      int s = search(point2, np, m - point2[i]);
      int t = s + point2[i];
      if (t > ans)
        ans = t;
    }
    cout << ans << endl;
  }
}
Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, M, P[1001];

int main() {
  while (scanf("%d%d", &N, &M) && N + M) {
    rep(i, N) scanf("%d", P + i);
    P[N] = 0;
    vi p;
    rep(i, N + 1) rep(j, N + 1) if (P[i] + P[j] <= M) p.PB(P[i] + P[j]);
    sort(p.begin(), p.end());

    int res = 0;
    rep(i, p.size()) {
      int d = M - p[i];
      int l = p[0], h = p[p.size() - 1];
      int m;
      while (h - l > 1) {
        m = (l + h) / 2;
        if (p[m] < d)
          l = m;
        else if (p[m] > d)
          h = m;
        else
          break;
      }
      if (p[m] > d)
        m--;
      //      printf("%d %d\n", p[m], p[i]);
      res = max(res, p[m] + p[i]);
    }
    printf("%d\n", res);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N, M, P[1001];

int main() {
  while (scanf("%d%d", &N, &M) && N + M) {
    rep(i, N) scanf("%d", P + i);
    P[N] = 0;
    vi p;
    rep(i, N + 1) rep(j, N + 1) if (P[i] + P[j] <= M) p.PB(P[i] + P[j]);
    sort(p.begin(), p.end());

    int res = 0;
    rep(i, p.size()) {
      int d = M - p[i];
      int l = 0, h = p.size();
      int m;
      while (h - l > 1) {
        m = (l + h) / 2;
        if (p[m] < d)
          l = m;
        else if (p[m] > d)
          h = m;
        else
          break;
      }
      if (p[m] > d)
        m--;
      //      printf("%d %d\n", p[m], p[i]);
      res = max(res, p[m] + p[i]);
    }
    printf("%d\n", res);
  }
  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> a;
vector<int> all;
int n, m;

int main() {

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    a.resize(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    a.push_back(0);
    all.clear();
    for (int i = 0; i < a.size(); i++)
      for (int j = i; j < a.size(); j++)
        all.push_back(a[i] + a[j]);

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

    int ans = 0;
    int allsize = all.size();
    int b = 0;

    for (int i = allsize - 1; i >= 0; i--) {
      for (int j = b; j < allsize; j++) {
        int sum = all[i] + all[j];
        if (sum > m)
          break;
        ans = max(ans, sum);
      }
      if (ans == m)
        break;
    }

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

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> a;
vector<int> all;
int n, m;

int main() {

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    a.resize(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    a.push_back(0);
    all.clear();
    for (int i = 0; i < a.size(); i++)
      for (int j = i; j < a.size(); j++)
        all.push_back(a[i] + a[j]);

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

    int ans = 0;
    int allsize = all.size();
    int b = 0;

    for (int i = allsize - 1; i >= 0; i--) {
      for (int j = b; j < allsize; j++) {
        int sum = all[i] + all[j];
        if (sum > m)
          break;
        b++;
        ans = max(ans, sum);
      }
      if (ans == m)
        break;
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;

  while (cin >> n >> m, n + m) {
    vector<int> v;
    int a;

    for (int i = 0; i < n; i++) {
      cin >> a;
      v.push_back(a);
    }

    int size = v.size();
    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size; j++) {
        v.push_back(v[i] + v[j]);
      }
    }

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

    int ans = 0;
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v.size(); j++) {
        if (v[i] + v[j] <= m)
          ans = max(ans, v[i] + v[j]);
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;

  while (cin >> n >> m, n + m) {
    vector<int> v;
    int a;

    for (int i = 0; i < n; i++) {
      cin >> a;
      v.push_back(a);
    }

    int size = v.size();
    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size; j++) {
        v.push_back(v[i] + v[j]);
      }
    }

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

    int ans = 0;
    for (int i = 0; i < v.size(); i++) {
      int want = m - v[i];
      int l = 0, r = v.size() - 1;

      if (want < 0)
        continue;

      while (r - l > 1) {
        int mid = (l + r) / 2;
        if (v[mid] >= want) {
          r = mid;
        } else {
          l = mid;
        }
        if (v[l] + v[i] <= m) {
          ans = max(ans, v[l] + v[i]);
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;

  while (cin >> n >> m, n && m) {
    vector<int> point(n + 1);
    vector<int> sum;
    for (int i = 0; i < n; i++) {
      cin >> point[i];
    }
    for (int i = 0; i < n + 1; i++) {
      for (int j = i; j < n + 1; j++) {
        sum.push_back(point[i] + point[j]);
      }
    }
    sort(sum.begin(), sum.end());

    int ans = 0;
    for (int i = 0; i < sum.size(); i++) {
      int t = m - sum[i];
      cout << "t = " << t << endl;
      vector<int>::iterator it = upper_bound(sum.begin(), sum.end(), t);
      if (it != sum.begin()) {
        ans = max(ans, sum[i] + *(it - 1));
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;

  while (cin >> n >> m, n && m) {
    vector<int> point(n + 1);
    vector<int> sum;
    for (int i = 0; i < n; i++) {
      cin >> point[i];
    }
    for (int i = 0; i < n + 1; i++) {
      for (int j = i; j < n + 1; j++) {
        sum.push_back(point[i] + point[j]);
      }
    }
    sort(sum.begin(), sum.end());

    int ans = 0;
    for (int i = 0; i < sum.size(); i++) {
      int t = m - sum[i];
      vector<int>::iterator it = upper_bound(sum.begin(), sum.end(), t);
      if (it != sum.begin()) {
        ans = max(ans, sum[i] + *(it - 1));
      }
    }
    cout << ans << endl;
  }
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int N, M;
int NN;
int *dd;
int *d;

int search(int var) {
  int i = 0, j = NN - 1;
  int cut;

  while (i != j) {
    cut = (i + j) / 2;
    if (dd[cut] + var > M) {
      j = cut - 1;
    } else if (dd[cut] + var < M) {
      i = cut + 1;
    } else
      return dd[cut] + var;
  }

  if (var + dd[cut] <= M)
    return dd[cut] + var;

  return -1;
}
int main(void) {

  while (cin >> N >> M, N && M) {

    NN = (N + 1) * (N + 1);
    d = new int[N + 1];
    dd = new int[NN];

    d[0] = 0;

    for (int i = 1; i <= N; i++)
      cin >> d[i];

    int c = 0;
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= N; j++, c++)
        dd[c] = d[i] + d[j];

    sort(dd, dd + NN);

    c = 0;
    int num = 0;
    int MAX = 0;

    for (int i = 0; i < NN; i++) {
      if (dd[i] > M)
        break;

      num = search(dd[i]);

      if (num > MAX)
        MAX = num;
    }
    cout << MAX << endl;

    delete[] d;
    delete[] dd;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int N, M;
int NN;
int *dd;
int *d;

int search(int var) {
  int i = 0, j = NN - 1;
  int cut;

  while (i != j) {
    cut = (i + j) / 2;
    if (dd[cut] + var > M) {
      j = cut;
    } else if (dd[cut] + var < M) {
      i = cut + 1;
    } else
      return dd[cut] + var;
  }

  if (var + dd[cut] <= M)
    return dd[cut] + var;

  return -1;
}
int main(void) {

  while (cin >> N >> M, N && M) {

    NN = (N + 1) * (N + 1);
    d = new int[N + 1];
    dd = new int[NN];

    d[0] = 0;

    for (int i = 1; i <= N; i++)
      cin >> d[i];

    int c = 0;
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= N; j++, c++)
        dd[c] = d[i] + d[j];

    sort(dd, dd + NN);

    c = 0;
    int num = 0;
    int MAX = 0;

    for (int i = 0; i < NN; i++) {
      if (dd[i] > M)
        break;

      num = search(dd[i]);

      if (num > MAX)
        MAX = num;
    }
    cout << MAX << endl;

    delete[] d;
    delete[] dd;
  }
  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;
  int point[1000 + 10] = {0};
  vector<int> sec;

  int sum;

  int i, j;

  while (cin >> n >> m, n != 0 && m != 0) {
    sec.clear();
    sum = 0;
    for (i = 1; i <= n; i++) {
      cin >> point[i];
    }

    for (i = 0; i <= n; i++) {
      for (j = i; j <= n; j++) {
        sec.push_back(point[i] + point[j]);
      }
    }

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

    for (i = sec.size() - 1; i >= 0; i--) {
      for (j = i; j >= 0; j--) {
        if (sec[i] + sec[j] <= m) {
          sum = max(sum, sec[i] + sec[j]);
          break;
        }
      }
    }

    cout << sum << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;
  int point[1000 + 10] = {0};
  vector<int> sec;

  int sum;

  int i, j;

  while (cin >> n >> m, n != 0 && m != 0) {
    sec.clear();
    sum = 0;
    for (i = 1; i <= n; i++) {
      cin >> point[i];
    }

    for (i = 0; i <= n; i++) {
      for (j = i; j <= n; j++) {
        sec.push_back(point[i] + point[j]);
      }
    }

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

    j = sec.size() - 1;
    for (i = 0; i <= sec.size() - 1; i++) {
      // cout <<"A"<< j << " " <<i << endl;
      while (sec[i] + sec[j] > m && j > 0)
        j--;
      if (j == 0 && sec[i] + sec[j] > m)
        j--;

      // cout << "c " << j << endl;
      if (j < 0)
        break;

      // cout << "B" << sec[i] << " " << sec[j] << endl;

      sum = max(sum, sec[i] + sec[j]);
    }

    cout << sum << endl;
  }

  return 0;
}
Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int sum[1001100];

int main() {
  int n, d;
  int m[1111];
  while (cin >> n >> d, n) {
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= n; i++) {
      cin >> m[i];
    }
    m[0] = 0;
    n++;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        sum[i * n + j] = m[i] + m[j];
      }
    }
    sort(sum, sum + n * n);

    int res = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        int ab = m[i] + m[j];
        if (ab <= d) {
          int h = d - ab;
          int tmp = sum[upper_bound(sum, sum + n * n, h) - sum - 1] + ab;
          if (tmp <= d) {
            res = max(res, tmp);
          }
        }
      }
    }
    cout << res << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int sum[1002100];

int main() {
  int n, d;
  int m[1111];
  while (cin >> n >> d, n) {
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= n; i++) {
      cin >> m[i];
    }
    m[0] = 0;
    n++;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        sum[i * n + j] = m[i] + m[j];
      }
    }
    sort(sum, sum + n * n);

    int res = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        int ab = m[i] + m[j];
        if (ab <= d) {
          int h = d - ab;
          int tmp = sum[upper_bound(sum, sum + n * n, h) - sum - 1] + ab;
          if (tmp <= d) {
            res = max(res, tmp);
          }
        }
      }
    }
    cout << res << endl;
  }
}
Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int P[1000], mem[1000 * 1000], N, M;
int main() {
loop:
  cin >> N >> M;
  if (!N)
    return 0;
  N++;
  P[0] = 0;
  for (int i = 1; i < N; i++)
    cin >> P[i];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      mem[N * i + j] = P[i] + P[j];
  sort(mem, mem + N * N);

  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    if (mem[i] > M)
      continue;
    ans = max(ans, mem[i] + *(upper_bound(mem, mem + N * N, M - mem[i]) - 1));
  }
  cout << ans << endl;
  goto loop;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int P[1001], mem[1001 * 1001], N, M;
int main() {
loop:
  cin >> N >> M;
  if (!N)
    return 0;
  N++;
  P[0] = 0;
  for (int i = 1; i < N; i++)
    cin >> P[i];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      mem[N * i + j] = P[i] + P[j];
  sort(mem, mem + N * N);

  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    if (mem[i] > M)
      continue;
    ans = max(ans, mem[i] + *(upper_bound(mem, mem + N * N, M - mem[i]) - 1));
  }
  cout << ans << endl;
  goto loop;
}
Problem p00455 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int h[3] = {0};
  int m[3] = {0};
  int s[3] = {0};
  int H[3] = {0};
  int M[3] = {0};
  int S[3] = {0};
  int j, f, b;
  for (int i = 0; i < 4; i++) {
    cin >> h[i] >> m[i] >> s[i] >> H[i] >> M[i] >> S[i];
    j = (3600 * H[i] + 60 * M[i] + S[i] - 3600 * h[i] - 60 * m[i] - s[i]) /
        3600;
    f = ((3600 * H[i] + 60 * M[i] + S[i] - 3600 * h[i] - 60 * m[i] - s[i]) -
         j * 3600) /
        60;
    b = (3600 * H[i] + 60 * M[i] + S[i] - 3600 * h[i] - 60 * m[i] - s[i]) -
        j * 3600 - f * 60;
    cout << j << " " << f << " " << b << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int sh, sm, ss, eh, em, es;
  int h, m, s;
  for (int i = 0; i < 3; i++) {
    cin >> sh >> sm >> ss >> eh >> em >> es;
    if (es < ss) {
      em = em - 1;
      es = es + 60;
    }
    s = es - ss;
    if (em < sm) {
      eh = eh - 1;
      em = em + 60;
    }
    m = em - sm;
    h = eh - sh;
    cout << h << " " << m << " " << s << endl;
  }
  return 0;
}
Problem p00457 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[10000];
int solve(int p, int n) {
  int l = p - 1, r = p + 1, t;
  int s1, s2, ans = 0, f = 1;
  while (0 <= l) {
    t = a[l];
    s1 = s2 = 0;
    if (f)
      s1 = 1, f = 0;
    // if(p == 5)printf("%d %d %d\n", l, r, t);
    while (0 <= l && a[l] == t) {
      s1++;
      l--;
    }
    while (r < n && a[r] == t) {
      s2++;
      r++;
    }
    if (s1 + s2 >= 4) {
      ans += s1 + s2;
    } else
      break;
  }
  return n - ans;
}

int main() {
  int n, ans;
  while (1) {
    scanf("%d", &n);
    ans = INF;
    rep(i, n) scanf("%d", &a[i]);
    for (int i = 1; i < n - 1; i++) {
      ans = min(ans, solve(i, n));
    }
    if (ans == INF)
      printf("%d\n", n - 1);
    else
      printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[10000];
int solve(int p, int n) {
  int l = p - 1, r = p + 1, t;
  int s1, s2, ans = 0, f = 1;
  while (0 <= l) {
    t = a[l];
    s1 = s2 = 0;
    if (f)
      s1 = 1, f = 0;
    // if(p == 5)printf("%d %d %d\n", l, r, t);
    while (0 <= l && a[l] == t) {
      s1++;
      l--;
    }
    while (r < n && a[r] == t) {
      s2++;
      r++;
    }
    if (s1 + s2 >= 4) {
      ans += s1 + s2;
    } else
      break;
  }
  return n - ans;
}

int main() {
  int n, ans;
  while (1) {
    scanf("%d", &n);
    ans = INF;
    rep(i, n) scanf("%d", &a[i]);
    if (!n)
      break;
    for (int i = 1; i < n - 1; i++) {
      ans = min(ans, solve(i, n));
    }
    if (ans == INF)
      printf("%d\n", n - 1);
    else
      printf("%d\n", ans);
  }
  return 0;
}