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

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

using namespace std;

int dp[4002][4002] = {0};

int main() {

  char a[4003];

  char b[4003];

  while (scanf("%s%s", a, b) != EOF) {

    memset(dp, 0, sizeof(dp));
    // int dp[4002][4002] = {0};

    /*
     for(int i = 0;i < 10;i++){
       for(int j = 0;j < 10;j++){
         cout << dp[i][j];
       }
     }
    */

    int ans = 0;
    //  scanf("%s",a);

    int alen = strlen(a);

    //  if(alen == 0) break;

    //  scanf("%s",b);

    int blen = strlen(b);

    //  cout << "ans:" << ans << endl;

    for (int i = 1; i <= alen; i++) {
      for (int j = 1; j <= blen; j++) {
        if (a[i - 1] == b[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        }
        ans = max(ans, dp[i][j]);
      }
    }

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

  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
  char a[4000], b[4000];
  while (scanf("%s", a) != EOF) {
    scanf("%s", b);
    int t[4002][4002];
    for (int i = 0; i <= strlen(a); i++) {
      t[0][i] = 0;
    }
    for (int j = 0; j <= strlen(b); j++) {
      t[j][0] = 0;
    }
    int ans = 0;
    for (int i = 1; i <= strlen(a); i++) {
      for (int j = 1; j <= strlen(b); j++) {
        if (a[i - 1] == b[j - 1])
          t[i][j] = t[i - 1][j - 1] + 1;
        else
          t[i][j] = 0;
        ans = max(ans, t[i][j]);
      }
    }

    cout << ans << "\\n";
  }
  return 0;
}
Problem p00451 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "stdio.h"
#include "string.h"

char st1[5000];
char st2[5000];
char flg[15000];
int ans[1000];

int main() {
  int nw = 0;
  while (1) {
    if (NULL == gets(st1)) {
      break;
    }
    gets(st2);
    int st1l, st2l;
    st1l = strlen(st1);
    st2l = strlen(st2);
    int m = 0;
    for (int i = 0; i < st1l + st2l; i++) {
      for (int ii = 0; ii < 15000; ii++) {
        flg[ii] = 1;
      }
      for (int ii = 0; ii < st1l; ii++) {
        flg[ii + st2l] = st1[ii];
      }
      for (int ii = 0; ii < st2l; ii++) {
        if (flg[i + ii] == st2[ii]) {
          flg[i + ii] = 0;
        } else {
          flg[i + ii] = 1;
        }
      }
      int n = 0;
      for (int ii = 0; ii < 15000; ii++) {
        if (flg[ii] == 0) {
          n++;
          if (n > m) {
            m = n;
          }
        } else {
          n = 0;
        }
      }
    }
    ans[nw] = m;
    nw++;
  }
  for (int i = 0; i < nw; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "stdio.h"
#include "string.h"

char st1[5000];
char st2[5000];
char flg[5000];
int ans[1000];

int main() {
  int nw = 0;
  while (1) {
    scanf("%s", st1);
    if (st1[0] == EOF) {
      break;
    }
    scanf("%s", st2);
    int st1l, st2l;
    st1l = strlen(st1);
    st2l = strlen(st2);
    int m = 0;
    for (int i = 0; i < st1l; i++) {
      for (int ii = 0; ii < 5000; ii++) {
        flg[ii] = 0;
      }
      for (int ii = i; ii < st1l; ii++) {
        flg[ii - i] = st1[ii];
      }
      for (int ii = 0; ii < st2l; ii++) {
        flg[ii] = flg[ii] - st2[ii];
      }
      int n = 0;
      for (int ii = 0; ii < 5000; ii++) {
        if (flg[ii] == 0) {
          n++;
          if (n > m) {
            m = n;
          }
        } else {
          n = 0;
        }
      }
    }
    ans[nw] = m;
    nw++;
  }
  for (int i = 0; i < nw; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<long long> F1;
vector<long long> F2;

long long n, m;
long long a[10000];
long long maxn;
long long l, r, M;

int main() {
  while (true) {
    maxn = 0;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    F1.clear();
    F2.clear();
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        F1.push_back(a[i] + a[j]);
      }
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        F2.push_back(a[i] + a[j]);
      }
    }
    sort(F2.begin(), F2.end());
    for (int i = 0; i < F1.size(); i++) {
      if (F1[i] <= m) {
        l = 0;
        r = F2.size();
        for (int j = 0; j < 21; j++) {
          M = (l + r) / 2;
          if (F1[i] + F2[M] > m) {
            r = M;
          } else if (M == F1.size() - 1) {
            l = M;
          } else if (F1[i] + F2[M + 1] <= m) {
            l = M;
          }
        }
        if (F1[i] + F2[M] <= m) {
          maxn = max(maxn, F1[i] + F2[M]);
        }
      }
    }
    cout << maxn << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

vector<long long> F1;
vector<long long> F2;

long long n, m;
long long a[10000];
long long maxn;
long long l, r, M;

int main() {
  while (true) {
    maxn = 0;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    F1.clear();
    F2.clear();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        F1.push_back(a[i] + a[j]);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        F2.push_back(a[i] + a[j]);
      }
    }
    sort(F2.begin(), F2.end());
    for (int i = 0; i < F1.size(); i++) {
      if (F1[i] <= m) {
        l = 0;
        r = F2.size();
        for (int j = 0; j < 32; j++) {
          M = (l + r) / 2;
          if (F1[i] + F2[M] > m) {
            r = M;
          } else if (M == F1.size() - 1) {
            l = M;
          } else if (F1[i] + F2[M + 1] <= m) {
            l = M;
          }
        }
        maxn = max(maxn, F1[i] + F2[M]);
      }
    }
    cout << maxn << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define vi vector<int>
#define INF 999999999

int main() {

  int n, m;
  while (cin >> n >> m && n && m) {
    vi p(n);
    rep(i, n) cin >> p[i];
    sort(all(p));

    set<ll> val;
    val.insert(0);
    rep(i, n) {
      if (p[i] > m)
        break;
      val.insert(p[i]);
    }
    rep(i, n) for (ll j = i; j < n; j++) {
      if (p[i] + p[j] > m)
        break;
      val.insert(p[i] + p[j]);
    }

    vector<ll> left, right;

    for (auto &v : val)
      left.pb(v);

    sort(all(left));
    right = left;

    ll maxi = 0;
    rep(i, left.size()) {
      ll num = left[i];
      if (num > m)
        break;

      ll l = 0, r = right.size() + 1, mid = (l + r) / 2LL;
      while (l + 1 < r) {
        if (right[mid] <= m - num)
          l = mid;
        else
          r = mid;
        mid = (l + r) / 2LL;
      }

      maxi = max<ll>(maxi, num + right[mid]);
    }
    cout << maxi << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define vi vector<int>
#define INF 999999999

template <class T> T rev_lower_bound(vector<ll> &vec, T num) {
  ll left = 0, right = vec.size() + 1, mid = (left + right) / 2LL;
  while (left + 1 < right) {
    if (vec[mid] <= num)
      left = mid;
    else
      right = mid;
    mid = (left + right) / 2LL;
  }
  return mid;
}

int main() {

  int n, m;
  while (cin >> n >> m && n && m) {
    vi p(n);
    rep(i, n) cin >> p[i];

    set<ll> val;
    val.insert(0);
    rep(i, n) val.insert(p[i]);
    rep(i, n) for (ll j = i; j < n; j++) val.insert(p[i] + p[j]);

    vector<ll> left, right;

    for (auto &v : val)
      left.pb(v);

    sort(all(left));
    right = left;

    ll maxi = 0;
    rep(i, left.size()) {
      ll num = left[i];
      if (num > m)
        break;

      int pos = rev_lower_bound(right, m - num);

      maxi = max<ll>(maxi, num + right[pos]);
    }
    cout << maxi << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

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

    vector<int> A;

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        if (a[i] + a[j] > m)
          continue;
        A.push_back(a[i] + a[j]);
      }
    }

    sort(A.begin(), A.end());
    A.erase(unique(A.begin(), A.end()), A.end());

    int maxs = 0;
    for (int l = 0, r = A.size() - 1; l <= r; l++) {
      while (A[l] + A[r] > m)
        r--;
      maxs = max(maxs, A[l] + A[r]);
    }

    cout << maxs << endl;
  }
}

Error: MLE

Fixed submission:

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

#define fi first
#define se second

typedef pair<int, int> P;

map<int, bool> f;
priority_queue<P> q;
int a[1010];

int main() {
  int n, m;
  while (cin >> n >> m, n) {

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

    sort(a, a + n);

    for (int i = n - 1; i >= 0; i--) {
      q.push(pair<int, int>(a[i], 1));
    }

    f.clear();
    int maxs = 0;
    while (!q.empty()) {
      P s = q.top();
      q.pop();

      maxs = max(s.fi, maxs);

      if (s.second == 4)
        continue;
      for (int i = n - 1; i >= 0; i--) {
        if (s.fi + a[i] > m)
          continue;
        if (f[s.fi + a[i]])
          continue;

        q.push(P(s.fi + a[i], s.se + 1));
        f[s.fi + a[i]] = true;
      }
    }

    cout << maxs << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define each(e, c) for (auto &e : c)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); ++it)

ll const inf = 1 << 28;
double const pi = acos(-1);

int a[1024];
int d[1024 * 1024];

int solve(int n, int k) {
  int c = 0;
  rep(i, n) loop(j, i, n) {
    int s = a[i] + a[j];
    if (s <= k) {
      d[c++] = s;
    }
  }

  sort(d, d + c);
  int ans = 0;
  rep(i, c) {
    int s1 = d[i];
    int lo = 0, hi = c;
    while (lo + 1 < hi) {
      int mid = (lo + hi) >> 1;
      int s2 = d[mid];
      if (s1 + s2 > k) {
        hi = mid;
      } else {
        lo = mid;
      }
    }
    int s2 = d[lo];
    ans = max(ans, s1 + s2);
  }
  return ans;
}

int main() {
  int n, k;
  while (cin >> n >> k && n | k) {
    a[0] = 0;
    rep(i, n) cin >> a[i + 1];
    n++;
    cout << solve(n, k) << endl;
  }
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (int i = a; i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define each(e, c) for (auto &e : c)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); ++it)

ll const inf = 1 << 28;
double const pi = acos(-1);

int a[1024];
int d[2048];

int solve(int n, int k) {
  int c = 0;
  rep(i, n) loop(j, i, n) {
    int s = a[i] + a[j];
    if (s <= k) {
      d[c++] = s;
    }
  }
  sort(d, d + c);
  int ans = 0;

  rep(i, c) {
    int s1 = d[i];
    int lo = 0, hi = c;
    while (lo + 1 < hi) {
      int mid = (lo + hi) >> 1;
      int s2 = d[mid];
      if (s1 + s2 > k) {
        hi = mid;
      } else {
        lo = mid;
      }
    }
    int s2 = d[lo];
    ans = max(ans, s1 + s2);
  }
  return ans;
}

int main() {
  int n, k;
  while (cin >> n >> k && n | k) {
    a[0] = 0;
    rep(i, n) cin >> a[i + 1];
    n++;
    cout << solve(n, k) << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

#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);
  }
}

Error: 0

Fixed submission:

#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);
  }
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

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

#define INF 100000000
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> v;
    vector<int> sum;
    for (int i = 0; i < n; ++i) {
      int a;
      cin >> a;
      v.push_back(a);
      sum.push_back(a);
    }

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        sum.push_back(v[i] + v[j]);
        // cout << sum[i*n+j] << end;
      }
    }
    sort(sum.begin(), sum.end());
    int ans = 0;
    for (int i = 0; i < sum.size(); i++) {

      int a = sum[lower_bound(sum.begin(), sum.end(), m - sum[i]) -
                  sum.begin() - 1];
      if (a + sum[i] == m) {
        ans = m;
      } else if (sum[i] + a <= m) {
        ans = max(ans, sum[i] + a);
      }
    }
    cout << ans << endl;
    v.clear();
    sum.clear();
  }
}


Error: WA

Fixed submission:

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

#define INF 100000000
int sum[1000 * 1000];
int v[1000];
int n, m;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {

    for (int i = 0; i < n; ++i) {
      int a;
      cin >> a;
      v[i] = a;
    }
    sort(v, v + n);

    int maxV = 0;

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        sum[i * n + j] = v[i] + v[j];
        // cout << sum[i*n+j] << end;
      }
    }
    sort(sum, sum + n * n);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        for (int i = 0; i <= m - v[i] - v[j]; i++) {

          if (v[i] + v[j] <= m) {
            maxV = max(maxV,
                       v[i] + v[j] +
                           sum[upper_bound(sum, sum + n * n, m - v[i] - v[j]) -
                               sum - 1]);
          }
        }
      }
    }
    cout << maxV << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: 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];
      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;
  }
}

Error: TLE

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];
      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;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
int main() {
  int a, b, mato[1000];
  vector<int> nums;
  while (scanf("%d%d", &a, &b), a, b) {
    int res = 0;
    for (int i = 0; i < a; i++) {
      scanf("%d", mato + i);
    }
    nums.clear();
    for (int i = 0; i <= a; i++) {
      for (int j = 0; j <= a; j++) {
        int r = 0;
        if (i != a)
          r += mato[i];
        if (j != a)
          r += mato[j];
        nums.push_back(r);
      }
    }
    sort(nums.begin(), nums.end());
    vector<int>::iterator it = nums.begin();
    while (it != nums.end()) {
      if (b - *it < 0)
        break;
      vector<int>::iterator itt =
          upper_bound(nums.begin(), nums.end(), b - *it);
      itt--;
      res = max(res, *it + *itt);
      it++;
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <set>
#include <stdio.h>
using namespace std;
int main() {
  int a, b, mato[1000];
  set<int> nums;
  while (scanf("%d%d", &a, &b), a, b) {
    int res = 0;
    for (int i = 0; i < a; i++) {
      scanf("%d", mato + i);
    }
    nums.clear();
    for (int i = 0; i <= a; i++) {
      for (int j = 0; j <= a; j++) {
        int r = 0;
        if (i != a)
          r += mato[i];
        if (j != a)
          r += mato[j];
        nums.insert(r);
      }
    }
    set<int>::iterator it = nums.begin();
    while (it != nums.end()) {
      if (b - *it < 0)
        break;
      set<int>::iterator itt = upper_bound(nums.begin(), nums.end(), b - *it);
      itt--;
      res = max(res, *it + *itt);
      it++;
    }
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    int res = 0, tmp;
    vector<int> points, points2;
    for (int i = 0; i < n; i++) {
      scanf("%d", &tmp);
      points.push_back(tmp);
    }
    vector<int>::iterator it1, it2;
    for (it1 = points.begin(); it1 != points.end(); it1++) {
      points2.push_back(*it1);
      for (it2 = points.begin(); it2 != points.end(); it2++) {
        if (*it1 + *it2 <= m)
          points2.push_back(*it1 + *it2);
      }
    }
    sort(points2.begin(), points2.end());
    for (it1 = points2.begin(); it1 != points2.end(); it1++) {
      it2 = upper_bound(points2.begin(), points2.end(), m - *it1);
      if (it2 == points2.begin())
        continue;
      it2--;
      res = max(res, *it1 + *it2);
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    int res = 0, tmp;
    set<int> points, points2;
    for (int i = 0; i < n; i++) {
      scanf("%d", &tmp);
      points.insert(tmp);
    }
    set<int>::iterator it1, it2;
    for (it1 = points.begin(); it1 != points.end(); it1++) {
      for (it2 = points.begin(); it2 != points.end(); it2++) {
        points2.insert(*it1 + *it2);
      }
    }
    for (it1 = points2.begin(); it1 != points2.end(); it1++) {
      it2 = upper_bound(points2.begin(), points2.end(), m - *it1);
      if (it2 == points2.begin())
        continue;
      it2--;
      res = max(res, *it1 + *it2);
    }
    printf("%d\\n", res);
  }
}