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

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

using namespace std;

int main() {
  int n;
  long long int m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    long long int b;
    vector<long long int> ss;
    ss.push_back(0);
    for (int i = 0; i < n; i++) {
      cin >> b;
      if (b <= m) {
        ss.push_back(b);
      }
    }
    vector<long long int> ss2;
    for (long long int i = 0; i < ss.size(); i++) {
      ss2.push_back(ss[i]);
      for (long long int j = i; j < ss.size(); j++) {
        if (ss[i] + ss[j] <= m) {
          ss2.push_back(ss[i] + ss[j]);
        }
      }
    }
    sort(ss2.begin(), ss2.end());
    /*for (long long int i = 0; i < ss2.size(); i++) {
            cout << ss2[i] << endl;
    }*/
    long long int MAX = 0;
    long long int sz = ss2.size();
    for (long long int j = 0; j < sz; j++) {
      long long int a = m;
      for (long long int i = 0; i < 2; i++) {
        vector<long long int>::iterator ite =
            upper_bound(ss2.begin(), ss2.end(), a);
        if (ite != ss.begin()) {
          ite--;
          a -= *(ite);
        }
      }
      vector<long long int>::iterator ite2 = ss2.end();
      ss2.erase(ite2 - 1);
      MAX = max(MAX, m - a);
    }
    cout << MAX << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int n;
  long long int m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }
    long long int b;
    set<long long int> ss;
    ss.insert(0);
    for (int i = 0; i < n; i++) {
      cin >> b;
      if (b <= m) {
        ss.insert(b);
      }
    }
    set<long long int> ss2;
    for (set<long long int>::iterator ite = ss.begin(); ite != ss.end();
         ite++) {
      ss2.insert(*(ite));
      for (set<long long int>::iterator ite2 = ite; ite2 != ss.end(); ite2++) {
        if (*(ite) + *(ite2) <= m) {
          ss2.insert(*(ite) + *(ite2));
        } else {
          break;
        }
      }
    }
    ss.clear();
    long long int a = m;
    for (long long int i = 0; i < 4; i++) {
      set<long long int>::iterator ite = upper_bound(ss2.begin(), ss2.end(), a);
      ite--;
      a -= *(ite);
    }
    cout << m - a << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      return 0;

    vector<int> p(n);
    for (int i = 0; i < n; ++i)
      cin >> p[i];

    vector<int> num = p;
    num.push_back(0);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j <= i; ++j) {
        num.push_back(p[i] + p[j]);
      }
    }
    sort(num.begin(), num.end());

    int ret = 0;
    unsigned j = num.size() - 1;
    for (unsigned i = 0; i < num.size(); ++i) {
      if (num[i] > m)
        break;
      while (num[i] + num[j] > m)
        --j;
      ret = max(ret, num[i] + num[j]);
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      return 0;

    vector<set<int>> num(5);
    num[0].insert(0);
    int ret = 0;
    for (int i = 0; i < n; ++i) {
      int a;
      cin >> a;
      for (int i = 0; i < 4; ++i) {
        set<int>::iterator it;
        for (it = num[i].begin(); it != num[i].end(); ++it) {
          int b = (*it) + a;
          if (b <= m) {
            num[i + 1].insert(b);
            ret = max(ret, b);
          }
        }
      }
    }
    cout << ret << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

// dpではとけなかった(なんでかは分からん)

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

// long dp[6][1100];
/*
long rec(int a, int b){ //a本目，最後に当てた位置b
        if(dp[a][b]>=0) return dp[a][b];

        long ret=0;
        long tmp=0;

        if(a==4) ret=0;
        else{
                for(int i=b; i<=n; ++i){
                        tmp=rec(a+1, i)+p[i];
                        if(tmp>m) tmp=0;

                        ret=max(ret, tmp);

                }
        }

        return dp[a][b]=ret;
}
*/

int n;
long m, p[1001], q[1100000];

int main() {
  while (1) {

    scanf(" %d %ld", &n, &m);
    if (n == 0)
      break;
    p[0] = 0; // 投げない選択肢
    for (int i = 1; i <= n; ++i)
      scanf(" %ld", &p[i]);
    sort(p, p + n);

    // 半分全列挙(?)
    long now = 0;
    for (int i = 0; i <= n; ++i)
      for (int j = i; j <= n; ++j)
        q[now++] = p[i] + p[j];
    sort(q, q + now);

    // for(long i=0; i<now; ++i) printf("q[%ld]=%ld\\n", i, q[i]);

    /*
    memset(dp, -1, sizeof(dp));
    printf("%ld\\n", rec(0,0));
    */

    long ans = 0;
    for (long i = 0; i < now; ++i) {
      long rem = m - q[i];
      if (rem < 0)
        break;

      // 2分探索
      long left = 0, right = now;
      while (left < right) {
        long center = (left + right) / 2;
        if (rem > q[center]) {
          left = center + 1;
        } else if (rem < q[center]) {
          right = center;
        } else
          break;
      }

      ans = max(ans, q[i] + q[right - 1]);
    }

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

Error: TLE

Fixed submission:

// dpではとけなかった(なんでかは分からん)

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

// long dp[6][1100];
/*
long rec(int a, int b){ //a本目，最後に当てた位置b
        if(dp[a][b]>=0) return dp[a][b];

        long ret=0;
        long tmp=0;

        if(a==4) ret=0;
        else{
                for(int i=b; i<=n; ++i){
                        tmp=rec(a+1, i)+p[i];
                        if(tmp>m) tmp=0;

                        ret=max(ret, tmp);

                }
        }

        return dp[a][b]=ret;
}
*/

int n;
long m, p[1001], q[1100000];

int main() {
  while (1) {

    scanf(" %d %ld", &n, &m);
    if (n == 0)
      break;
    p[0] = 0; // 投げない選択肢
    for (int i = 1; i <= n; ++i)
      scanf(" %ld", &p[i]);
    sort(p, p + n);

    // 半分全列挙(?)
    long now = 0;
    for (int i = 0; i <= n; ++i)
      for (int j = i; j <= n; ++j)
        q[now++] = p[i] + p[j];
    sort(q, q + now);

    // for(long i=0; i<now; ++i) printf("q[%ld]=%ld\\n", i, q[i]);

    /*
    memset(dp, -1, sizeof(dp));
    printf("%ld\\n", rec(0,0));
    */

    long ans = 0;
    for (long i = 0; i < now; ++i) {
      for (long j = i; j < now; ++j) {
        long c = q[i] + q[j];
        if (ans < c && c <= m)
          ans = c;
      }
    }

    printf("%ld\\n", ans);
  }
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

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

int A[2000];
int KK[2000000];
int N, M;
int k;
void getK() {
  k = 0;
  for (int i = 0; i <= N; i++)
    for (int j = i; j <= N; j++)
      KK[k++] = A[i] + A[j];

  sort(KK, KK + k);
}

int binarySearch(int a) {
  int left = 0;
  int right = k;
  int mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (KK[mid] <= a)
      left = mid;
    else
      right = mid;
  }

  return KK[left];
}
void solve() {

  int maxS = 0;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= N; j++) {
      int temp = binarySearch(M - A[i] - A[j]) + A[i] + A[j];
      if (temp > maxS && temp <= M)
        maxS = temp;
    }
  cout << maxS << endl;
}

int main() {
  A[0] = 0;
  while (cin >> N >> M && N || M) {

    for (int i = 1; i <= N; i++)
      cin >> A[i];
    sort(A, A + N + 1);
    getK();
    solve();
  }
}

Error: 0

Fixed submission:

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

int A[2000];
int KK[1000];

int N, M;

int k;
void getK() {
  k = 0;
  for (int i = 0; i <= N; i++)
    for (int j = i; j <= N; j++)
      KK[k++] = A[i] + A[j];

  sort(KK, KK + k);
}

int binarySearch(int a) {
  int left = 0;
  int right = k;
  int mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (KK[mid] <= a)
      left = mid;
    else
      right = mid;
  }

  return KK[left];
}

void solve() {

  int maxS = 0;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= N; j++) {
      int temp = binarySearch(M - A[i] - A[j]) + A[i] + A[j];
      if (temp > maxS && temp <= M)
        maxS = temp;
    }
  cout << maxS << endl;
}

int main() {
  A[0] = 0;
  while (cin >> N >> M && N || M) {

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

    sort(A, A + N + 1);
    getK();

    solve();
  }
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

#include <algorithm>
#include <cstdio>

using namespace std;

int m, n;
int a[1002];
int aa[1002001];

int ans, Max, size;

int main() {

  while (1) {
    scanf("%d%d", &n, &m);
    ans = 0;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    a[n++] = 0;

    size = 0;
    for (int c = 0; c < n; c++) {
      for (int d = c; d < n; d++) {
        aa[size++] = a[c] + a[d];
      }
    }
    sort(aa, aa + size);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (m - a[i] - a[j] > 0) {
          int l = 0, r = size - 1;
          Max = 0;
          while (l <= r) {
            if (aa[size - 1] + a[i] + a[j] < ans) {
              break;
            }
            int k = (l + r) / 2;
            if (aa[k] < (m - a[i] - a[j]) && aa[k] > Max) {
              Max = aa[k];
            }
            if (aa[k] < (m - a[i] - a[j]))
              l = k + 1;
            else
              r = k - 1;
          }
          if (Max + a[i] + a[j] > ans) {
            ans = Max + a[i] + a[j];
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

int m, n;
int a[1002];
int aa[1002001];

int ans, Max;

int main() {

  while (1) {
    scanf("%d%d", &n, &m);
    ans = 0;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    a[n++] = 0;

    int size = 0;
    for (int c = 0; c < n; c++) {
      for (int d = 0; d < n; d++) {
        aa[size++] = a[c] + a[d];
      }
    }
    sort(aa, aa + size);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (m - a[i] - a[i] >= 0) {
          int l = 0, r = size;
          Max = 0;
          while (l <= r) {
            int k = (l + r) / 2;
            if (aa[k] < (m - a[i] - a[j]) && aa[k] > Max) {
              Max = aa[k];
            }
            if (aa[k] < (m - a[i] - a[j]))
              l = k + 1;
            else
              r = k - 1;
          }
          if (Max + a[i] + a[j] > ans) {
            ans = Max + a[i] + a[j];
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p00452 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0)
      break;
    int ans = 0;
    vector<int> a, sum;
    a.push_back(0);
    for (int i = 1; i <= n; i++) {
      int s;
      cin >> s;
      a.push_back(s);
    }
    sort(a.begin(), a.end());
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        if (a[i] + a[j] <= m)
          sum.push_back(a[i] + a[j]);
      }
    }
    sort(sum.begin(), sum.end());
    int suml = sum.size() - 1;
    for (int i = 0; i <= suml; i++) {
      while (sum[i] + sum[suml] > m)
        suml--;
      if (ans < sum[i] + sum[suml])
        ans = sum[i] + sum[suml];
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> sum;
  int n, m;
  int a[1000];
  while (cin >> n >> m) {
    int ans = 0;
    a[0] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    sort(a, a + n + 1);
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        if (m >= a[i] + a[j])
          sum.push_back(a[i] + a[j]);
      }
    }
    sort(sum.begin(), sum.end());
    int s = sum.size() - 1;
    for (int i = 0; i <= s; i++) {
      while (sum[i] + sum[s] > m)
        s--;
      if (ans < sum[i] + sum[s])
        ans = sum[i] + sum[s];
    }
    cout << ans << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fori(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
int in() {
  int x;
  cin >> x;
  return x;
}
int stringInt(string a) { return atoi(a.c_str()); }
string intString(int a) {
  stringstream ss;
  ss << a;
  return ss.str();
}
vector<int> SieveOfEratosthenes(int x) {
  vector<int> a;
  vector<int> b(x + 1, 0);
  for (int i = 2; i <= x; i++) {
    if (b[i] == 0) {
      a.push_back(i);
      for (int l = i; l <= x; l += i) {
        b[l] = 1;
      }
    }
  }
  return a;
}

int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<long long> a, b;
    b.push_back(0);
    rep(i, n) { b.push_back(in()); }
    rep(i, n + 1) {
      rep(l, n + 1) { a.push_back(b[i] + b[l]); }
    }
    long long ans = 0;
    sort(a.begin(), a.end());
    rep(i, a.size()) {
      vector<long long>::iterator t = upper_bound(a.begin(), a.end(), m - a[i]);
      if (t == a.begin())
        continue;
      t--;
      ans = max(ans, *t + a[i]);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fori(i, k, n) for (int i = k; i < (int)n; i++)
using namespace std;
int in() {
  int x;
  cin >> x;
  return x;
}
int stringInt(string a) { return atoi(a.c_str()); }
string intString(int a) {
  stringstream ss;
  ss << a;
  return ss.str();
}
vector<int> SieveOfEratosthenes(int x) {
  vector<int> a;
  vector<int> b(x + 1, 0);
  for (int i = 2; i <= x; i++) {
    if (b[i] == 0) {
      a.push_back(i);
      for (int l = i; l <= x; l += i) {
        b[l] = 1;
      }
    }
  }
  return a;
}

int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<int> a, b, c;
    int ans = 0;
    b.push_back(0);
    rep(i, n) { b.push_back(in()); }
    rep(i, n + 1) {
      rep(l, n + 1) { a.push_back(b[i] + b[l]); }
    }
    rep(i, a.size()) {
      rep(l, a.size()) {
        int j = a[i] + a[l];
        if (j <= m && j > ans)
          ans = j;
        // c.push_back(a[i]+a[l]);
      }
    }
    // sort(c.begin(),c.end());
    // cout<<*(upper_bound(c.begin(),c.end(),m)-1)<<endl;
    cout << ans << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define pb(a) push_back(a)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int main() {
  while (1) {
    int n, m, ans = 0, MAX = 0;
    cin >> n >> m;
    vi d(n + 1), w;
    if (n == 0 && m == 0)
      break;
    d[0] = 0;
    rep(i, 1, n + 1) cin >> d[i];
    rep(i, 0, n + 1) {
      rep(j, 0, n + 1) { w.pb(d[i] + d[j]); }
    }
    sort(all(w));
    rep(i, 0, w.size()) {
      // vector<int>::iteratar it=upper_bound(all(w),m-w[i]);
      int p = upper_bound(all(w), m - w[i]) - w.begin();
      if (p == 0)
        continue;
      MAX = max(MAX, w[i] + w[p - 1]);
    }
    o(MAX);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define pb(a) push_back(a)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int n, m, ans, MAX;
vi d(1010);

void solve(int sc, int k) {
  if (sc > m)
    return;
  MAX = max(MAX, sc);
  if (k == 4)
    return;
  rep(i, 0, n + 1) { solve(sc + d[i], k + 1); }
}

int main() {
  while (1) {
    ans = 0, MAX = 0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    d[0] = 0;
    rep(i, 1, n + 1) cin >> d[i];
    sort(all(d));
    reverse(all(d));
    solve(0, 0);
    o(MAX);
  }
}
Problem p00452 (C++) — buggy submission, verdict: 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 = 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;
}

Error: 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 = 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;
}