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

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

long long int solve(long long int a, vector<long long int> &side_b,
                    long long int m) {
  int left = 0, right = side_b.size() - 1;
  while (right - left > 1) {
    int middle = (right + left) / 2;
    if (side_b[middle] + a <= m) {
      left = middle;
    } else {
      right = middle;
    }
  }
  return a + side_b[left];
}

int main(void) {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

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

    vector<long long int> side_a;
    for (int i = 0; i < n; i++) {
      side_a.push_back(input[i]);
      for (int j = 0; j < n; j++) {
        side_a.push_back(input[i] + input[j]);
      }
    }
    sort(side_a.begin(), side_a.end());
    side_a.push_back(side_a[side_a.size() - 1]);

    long long int ans = 0;
    for (size_t i = 0; i < side_a.size(); i++) {
      long long int a = side_a[i];
      long long int tmp = solve(a, side_a, m);
      if (tmp <= m) {
        ans = max(ans, tmp);
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

long long int solve(long long int a, vector<long long int> &side_b,
                    long long int m) {
  int left = 0, right = side_b.size();
  while (right - left > 1) {
    int middle = (right + left) / 2;
    if (side_b[middle] + a <= m) {
      left = middle;
    } else {
      right = middle;
    }
  }
  return a + side_b[left];
}

int main(void) {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }

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

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

    long long int ans = 0;
    for (size_t i = 0; i < side_a.size(); i++) {
      long long int a = side_a[i];
      long long int tmp = solve(a, side_b, m);
      if (tmp <= m) {
        ans = max(ans, tmp);
      }
    }

    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
#define N1 1001
using namespace std;
int Search(int);
int N, M, P[N1];
vector<int> V;
int main() {
  int i, j, maxv, r;
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (i = 0; i < N; i++) {
      cin >> P[i];
      V.push_back(P[i]);
    }
    V.push_back(0);
    for (i = 0; i < N; i++) {
      for (j = i; j < N; j++) {
        V.push_back(P[i] + P[j]);
      }
    }
    sort(V.begin(), V.end());
    //    for(i=0;i<V.size();i++) cout << V[i] << endl;
    maxv = 0;
    for (i = 0; i < V.size(); i++) {
      if (V[i] < M) {
        r = Search(V[i]);
        maxv = max(maxv, V[i] + r);
      }
    }
    cout << maxv << endl;
    V.clear();
  }
  return 0;
}
int Search(int a) {
  int left = 0, right = V.size(), mid;
  while (left < right) {
    // x    cout << left << ' ' << right << endl;
    mid = (right + left) / 2;
    if (a + V[mid] <= M)
      left = mid + 1;
    else if (a + V[mid] > M)
      right = mid;
  }
  return V[left - 1];
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define N1 1001
using namespace std;
int Search(int);
int N, M, P[N1];
vector<int> V, cp, cp1;
int main() {
  int i, j, maxv, r;
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (i = 0; i < N; i++) {
      cin >> P[i];
      V.push_back(P[i]);
    }
    V.push_back(0);
    for (i = 0; i < N; i++) {
      for (j = i; j < N; j++) {
        V.push_back(P[i] + P[j]);
      }
    }
    cp = cp1 = V;
    for (i = 0; i < cp.size(); i++) {
      for (j = 0; j < cp1.size(); j++) {
        V.push_back(cp[i] + cp1[j]);
      }
    }
    for (i = 0; i < N; i++) {
      for (j = 0; j < cp.size(); j++) {
        V.push_back(P[i] + cp[j]);
      }
    }
    sort(V.begin(), V.end());
    maxv = 0;
    for (i = 0; i < V.size(); i++) {
      r = Search(V[i]);
      maxv = max(maxv, r);
    }
    cout << maxv << endl;
    V.clear();
    cp.clear();
    cp1.clear();
  }
  return 0;
}
int Search(int a) {
  int left = 0, right = V.size() - 1, mid;
  while (1) {
    // x    cout << left << ' ' << right << endl;
    mid = (right + left) / 2;
    if (a + V[mid] < M)
      left = mid + 1;
    else if (a + V[mid] >= M)
      right = mid;
    if (left == right)
      break;
  }
  return V[left - 1];
}
Problem p00452 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define INF 100000005
#define MAX 1004

int p[MAX], q[MAX * MAX];
int main() {
  int n, m, ans, qq;
  while (cin >> n >> m, n) {
    for (int i = 1; i <= n; i++)
      cin >> p[i];
    ans = 0;
    p[0] = 0;
    qq = 0;

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        q[qq++] = p[i] + p[j];
      }
    }
    sort(q, q + qq);
    int t;
    // for(int i = 0;i < qq;i++)cout << q[i] << endl;
    for (int i = 0; i < qq; i++) {
      t = upper_bound(q, q + qq, (m - q[i])) - q;
      if (t - 1 >= 0)
        ans = max(ans, q[i] + q[t - 1]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 100000005
#define MAX 100000000

int p[MAX], dp[MAX];
int main() {
  int n, m, ans;
  while (cin >> n >> m, n) {
    fill(dp, dp + MAX, 0);
    for (int i = 0; i < n; i++)
      cin >> p[i];
    ans = 0;

    for (int i = 0; i < n; i++) {
      dp[0] = 5;
      for (int j = p[i]; j <= m; j++) {
        dp[j] = max(dp[j], dp[j - p[i]] - 1);
        // if(dp[j] < dp[j-p[i]-1])dp[j] = dp[j-p[i]]-1;
        if (dp[j])
          ans = max(ans, j);
        // if(dp[j] && ans < j)ans = j;
      }
    }
    // for(int i = 1;i <= 50;i++)cout << dp[i] << endl;
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<long long> sep;

long long ary[1010];
int main() {
  int N;
  long long M, res;
  vector<long long>::iterator it, it2;
  while (1) {
    sep.clear();
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> ary[i];
    }
    ary[N] = 0;
    for (int i = 0; i <= N; i++) {
      for (int j = i; j <= N; j++) {
        if (ary[i] + ary[j] <= M) {
          sep.push_back(ary[i] + ary[j]);
        }
      }
    }
    sort(sep.begin(), sep.end());
    res = 0;
    it2 = sep.end();
    it2--;
    for (it = sep.begin(); it != sep.end(); it++) {
      while ((*it) + (*it2) > M)
        it2--;
      if ((*it) > (*it2))
        break;
      res = max(res, (*it) + (*it2));
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

set<long long> sep;
long long ary[1010];
int main() {
  int N;
  long long M, res;
  set<long long>::iterator it, it2;
  while (1) {
    sep.clear();
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < N; i++) {
      cin >> ary[i];
    }
    ary[N] = 0;
    for (int i = 0; i <= N; i++) {
      for (int j = i; j <= N; j++) {
        if (ary[i] + ary[j] <= M) {
          sep.insert(ary[i] + ary[j]);
        }
      }
    }
    res = 0;
    it2 = sep.end();
    it2--;
    for (it = sep.begin(); it != sep.end(); it++) {
      while ((*it) + (*it2) > M)
        it2--;
      if ((*it) > (*it2))
        break;
      res = max(res, (*it) + (*it2));
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ef(e, a, b) ((e) ? (a) : (b))

using namespace std;

int main(void) {
  int n, m;

  vector<int> x;

  while (cin >> n >> m, (n | m)) {
    vector<int> v(n);
    int ans = 0;

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

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

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

    for (int i = 0, sz = v.size(); i < sz; ++i) {
      if (v[i] > m)
        break;

      int t = m - v[i];

      vector<int>::iterator it = lower_bound(v.begin(), v.end(), t);
      if (t >= *it) {
        ans = max(ans, v[i] + *it);
      } else {
        --it;
        ans = max(ans, v[i] + *it);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define ef(e, a, b) ((e) ? (a) : (b))

using namespace std;

int main(void) {
  int n, m;
  int p[1000];

  while (cin >> n >> m, (n | m)) {
    int ans = 0;

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

    for (int i = 0; i < n; ++i) {
      if (p[i] > m) {
        break;
      }
      ans = max(ans, p[i]);
      for (int j = 0; j < n; ++j) {
        if (p[i] + p[j] > m) {
          break;
        }
        ans = max(ans, p[i] + p[j]);
        for (int k = 0; k < n; ++k) {
          if (p[i] + p[j] + p[k] > m) {
            break;
          }
          ans = max(ans, p[i] + p[j] + p[k]);
          for (int l = 0; l < n; ++l) {
            if (p[i] + p[j] + p[k] + p[l] > m) {
              break;
            }
            ans = max(ans, p[i] + p[j] + p[k] + p[l]);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  while (1) {
    int n, m, p, q = 0, r = 0, l = 0, s = 0, e = 0, ans = 0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> a, x;
    a.push_back(0);
    for (int i = 0; i < n; i++) {
      cin >> p;
      a.push_back(p);
    }
    q = n + 1;
    // cout<<q<<endl;
    for (int i = 0; i < q; i++) {
      for (int j = 0; j < q; j++) {
        x.push_back(a[i] + a[j]);
      }
    }
    sort(x.begin(), x.end());
    // for(int i=0;i<q*q;i++)
    // cout<<x[i]<<' ';
    // cout<<endl;
    for (int i = 0; i < x.size(); i++) {
      r = m - x[i];
      // cout<<r<<"を探します"<<endl;
      if (r >= 0) {
        s = 0;
        e = q * q;
        l = (s + e) / 2;
        while (s < e) {
          // cout<<"l="<<l<<endl;
          l = (s + e) / 2;
          if (x[l] == r)
            break;
          if (x[l] > r) {
            e = l - 1;
          } else {
            s = l + 1;
          }
        }
        while (x[l] > r && p > 0)
          l--;
        while (1) {
          if (l + 1 == q * q)
            break;
          if (x[l + 1] <= r)
            l++;
          else
            break;
        }
        ans = max(ans, x[i] + x[l]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  while (1) {
    int n, m, p, q = 0, r = 0, l = 0, s = 0, e = 0, ans = 0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> a, x;
    a.push_back(0);
    for (int i = 0; i < n; i++) {
      cin >> p;
      a.push_back(p);
    }
    q = n + 1;
    // cout<<q<<endl;
    for (int i = 0; i < q; i++) {
      for (int j = 0; j < q; j++) {
        x.push_back(a[i] + a[j]);
      }
    }
    sort(x.begin(), x.end());
    // for(int i=0;i<q*q;i++)
    // cout<<x[i]<<' ';
    // cout<<endl;
    for (int i = 0; i < q * q; i++) {
      r = m - x[i];
      // cout<<r<<"を探します"<<endl;
      s = 0;
      e = q * q;
      while (1) {
        l = (s + e) / 2;
        // cout<<"l="<<l<<endl;
        if (x[l] > r) {
          e = l - 1;
        } else if (x[l] < r) {
          s = l + 1;
        }
        if (s >= e || x[l] == r || l == 0) {
          if (x[i] + x[l] <= m)
            ans = max(ans, x[i] + x[l]);
          break;
        }
      }
      while (1) {
        if (l + 1 > q * q)
          break;
        if (x[l + 1] <= m) {
          l++;
          ans = max(ans, x[i] + x[l]);
        } else
          break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
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 != 0) {
    vector<int> ps(N);
    for (int i = 0; i < N; i++) {
      cin >> ps[i];
    }

    vector<int> v(1, 0);
    for (int i = 0; i < N; i++) {
      v.push_back(ps[i]);
      for (int j = i; j < N; j++) {
        if (ps[i] + ps[j] <= M) {
          v.push_back(ps[i] + ps[j]);
        }
      }
    }
    sort(v.begin(), v.end());

    int ans = 0;
    for (vector<int>::const_iterator it(v.begin()); it != v.end(); ++it) {
      vector<int>::const_iterator jt = lower_bound(v.begin(), v.end(), M - *it);
      if (jt != v.begin()) {
        --jt;
        ans = max(ans, *it + *jt);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, M;
  while (cin >> N >> M && N != 0) {
    vector<int> ps(N);
    for (int i = 0; i < N; i++) {
      cin >> ps[i];
    }

    set<int> s;
    s.insert(0);
    for (int i = 0; i < N; i++) {
      s.insert(ps[i]);
      for (int j = i; j < N; j++) {
        if (ps[i] + ps[j] <= M) {
          s.insert(ps[i] + ps[j]);
        }
      }
    }

    const vector<int> v(s.begin(), s.end());
    int ans = 0;
    for (vector<int>::const_iterator it(v.begin()); it != v.end(); ++it) {
      vector<int>::const_iterator jt = lower_bound(v.begin(), v.end(), M - *it);
      if (jt != v.begin()) {
        --jt;
        ans = max(ans, *it + *jt);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  while (1) {
    llong n, m;
    scanf("%lld%lld", &n, &m);
    if (n == 0)
      return 0;

    vector<llong> v(n);
    REP(i, n) { scanf("%lld", &v[i]); }
    v.push_back(0);
    n++;

    vector<llong> a;
    REP(i, n) {
      REP(j, n) { a.push_back(v[i] + v[j]); }
    }
    VSORT(a);

    llong ans = 0;
    REP(i, n) {
      REP(j, n) {
        llong tmp = v[i] + v[j];
        llong k = upper_bound(a.begin(), a.end(), m - tmp) - a.begin();
        if (k == a.size())
          continue;
        k--;

        if (k >= 0)
          ans = max(ans, tmp + a[k]);
      }
    }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

llong ub(vector<llong> a, llong x) {
  llong l = 0, r = a.size() - 1, mid;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (a[mid] >= x)
      r = mid;
    else
      l = mid;
  }

  if (a[r] == x)
    r++;
  return r;
}

int main() {
  while (1) {
    llong n, m;
    scanf("%lld%lld", &n, &m);
    if (n == 0)
      return 0;

    vector<llong> v(n);
    REP(i, n) { scanf("%lld", &v[i]); }
    v.push_back(0);
    n++;

    vector<llong> a;
    REP(i, n) {
      REP(j, n) { a.push_back(v[i] + v[j]); }
    }
    VSORT(a);

    llong ans = 0;
    REP(i, n) {
      REP(j, n) {
        llong tmp = v[i] + v[j];
        if (tmp > m)
          continue;

        llong k = ub(a, m - tmp);
        if (k == a.size())
          continue;
        k--;

        if (k >= 0)
          ans = max(ans, tmp + a[k]);
      }
    }

    printf("%lld\\n", ans);
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

void solve() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<int> point(N);
    for (int i = 0; i < N; ++i) {
      cin >> point[i];
    }
    point.push_back(0);
    vector<int> sum;
    for (int i = 0; i <= N; ++i) {
      for (int j = 0; j <= N; ++j) {
        sum.push_back(point[i] + point[j]);
      }
    }
    int ans = 0;
    sort(sum.begin(), sum.end());
    for (int i = 0; i < sum.size(); ++i) {
      if (M - sum[i] < 0) {
        continue;
      }
      int left = 0;
      int right = sum.size();
      while (left < right) {
        int middle = (left + right) / 2;
        if (sum[i] + sum[middle] > M) {
          right = middle;
        } else if (sum[i] + sum[middle] < M) {
          left = middle + 1;
        } else {
          break;
        }
      }
      ans = max(ans, sum[i] + sum[right - 1]);
    }
    cout << ans << endl;
  }
}

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

Error: TLE

Fixed submission:

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

using namespace std;
vector<int> point;
int N, M;
int ans = 0;

void DFS(int depth, int use, int sum, vector<int> &p) {
  if (depth == N || use == N) {
    ans = max(ans, sum);
    return;
  }
  if (sum + p[depth] > M) {
    DFS(depth + 1, use, sum, p);
  } else {
    DFS(depth, use + 1, sum + p[depth], p);
    DFS(depth + 1, use, sum, p);
  }
}

void solve() {
  while (cin >> N >> M, N || M) {
    ans = 0;
    point.clear();
    point.resize(N);
    for (int i = 0; i < N; ++i) {
      cin >> point[i];
    }
    DFS(0, 0, 0, point);
    cout << ans << endl;
  }
}

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int n;
llong m;

int main() {
  while (1) {
    scanf("%d%lld", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    n++;
    vector<llong> p(n);
    p[0] = 0;
    FOR(i, 1, n) scanf("%lld", &p[i]);
    VSORT(p);
    int size = 0;
    vector<llong> v;
    REP(i, n) {
      REP(j, n) { v.push_back(p[i] + p[j]); }
    }
    VSORT(v);
    llong ans = 0;
    REP(i, v.size()) {
      int rem = m - v[i];
      if (rem < 0)
        continue;
      int l = 0, r = v.size();

      while (l < r) {
        int mid = (l + r) / 2;
        if (rem > v[mid]) {
          l = mid + 1;
        } else if (rem < v[mid]) {
          r = mid;
        } else {
          break;
        }
      }

      ans = max(ans, v[i] + v[r - 1]);
    }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int n;
llong m;

llong bs(vector<llong> v, llong t) {
  int l = 0, r = v.size() - 1;
  int mid;
  while (l < r) {
    mid = (l + r) / 2;
    if (v[mid] > t)
      r = mid;
    else if (v[mid] < t) {
      if (v[mid + 1] > t)
        return v[mid];
      else
        l = mid + 1;
    } else
      break;
  }

  return v[r - 1];
}

llong solve(vector<llong> p) {
  vector<llong> v;
  set<llong> st;
  int c = 0;
  REP(i, n) {
    REPR(j, n - 1) {
      if (st.find(p[i] + p[j]) == st.end()) {
        v.push_back(p[i] + p[j]);
        st.insert(v[c]);
        c++;
      }
    }
  }
  VSORT(v);

  llong res = 0;
  REP(i, c) {
    if (v[i] > m)
      continue;
    res = max(res, v[i] + bs(v, m - v[i]));
  }
  return res;
}

int main() {
  while (1) {
    scanf("%d%lld", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    n++;
    vector<llong> p(n);
    p[0] = 0;
    FOR(i, 1, n) scanf("%lld", &p[i]);
    VSORT(p);
    llong ans = solve(p);
    printf("%lld\\n", ans);
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int N, M, p[1011];

  while (cin >> N >> M, N) {
    vector<int> sum;
    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++) {
        sum.push_back(p[i] + p[j]);
      }
    }

    sort(sum.begin(), sum.end());
    int res = 0;
    for (int i = 0; i < sum.size() && sum[i] <= M; i++) {
      vector<int>::iterator it =
          lower_bound(sum.begin(), sum.end(), M - sum[i]);
      res = max(res, sum[i] + *(it - 1));
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int main() {

  int n, m, p[1111];
  int sum[1111111];

  while (cin >> n >> m, n || m) {
    int idx = 0, maxi = 0;
    for (int i = 0; i < n; i++)
      cin >> p[i];
    p[n] = 0;

    for (int i = 0; i < n + 1; i++)
      for (int j = 0; j < n + 1; j++)
        sum[idx++] = p[i] + p[j];

    for (int i = 0; i < idx; i++) {
      for (int j = 0; j < idx; j++) {
        if (sum[i] + sum[j] <= m)
          maxi = max(maxi, sum[i] + sum[j]);
      }
    }
    cout << maxi << endl;
  }
}