alexjercan/bugnet huggingface dataset:
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 <string>
#include <vector>

typedef long long ll;

using namespace std;

int mato[1001], n, m, ans;

void nibun() {}

void solve() {
  vector<int> v;
  ans = 0;

  for (int i = 0; i < n; i++)
    scanf("%d", &mato[i]);

  mato[n] = 0;

  sort(mato, mato + n + 1);

  for (int i = 0; i <= n; i++)
    for (int j = i; j <= n; j++)
      if (mato[i] + mato[j] <= m)
        v.push_back(mato[i] + mato[j]);

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

  v.push_back(200000010);

  for (int i = 0; i < v.size(); i++) {

    int l = 0, r = (int)v.size() - 1, s = m - v[i];

    if (s <= 0)
      continue;

    while (1) {
      if (l == r - 1 || v[l] == v[r])
        break;

      if (v[(l + r) / 2] > s)
        r = (l + r) / 2;
      else
        l = (l + r) / 2;
    }

    ans = max(ans, v[l] + v[i]);
  }

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

  return;
}

int main() {

  while (1) {
    scanf("%d%d", &n, &m);
    if (n + m == 0)
      break;
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

using namespace std;

int mato[1001], n, m, ans;

void dfs(int h, int sum, int j) {

  ans = max(ans, sum);

  if (h == 4)
    return;

  for (int i = j; i < n; i++)
    if (sum + mato[i] <= m)
      dfs(h + 1, sum + mato[i], i);

  return;
}

void solve() {
  ans = 0;

  for (int i = 0; i < n; i++)
    scanf("%d", &mato[i]);

  sort(mato, mato + n);

  dfs(0, 0, 0);

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

  return;
}

int main() {

  while (1) {
    scanf("%d%d", &n, &m);
    if (n + m == 0)
      break;
    solve();
  }

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

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

int p[1010];
int sum[1002010];

int main(void) {
  int n, m;

  while (scanf("%d%d", &n, &m) && (n || m)) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &p[i]);
    }
    p[n++] = 0; // 0ツ点ツづ個スツコツアツづーツ催ャツづゥ

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

    int ans = 0;

    for (int i = 0; i < size; i++) {
      int rem = m - sum[i];
      if (rem < 0)
        continue;
      int left = 0, right = size;

      while (left < right) {
        int center = (left + right) / 2;
        if (rem > sum[center]) {
          left = center + 1;
        } else if (rem < sum[center]) {
          right = center;
        } else {
          break;
        }
      }

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

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

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

void solve() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<int> Vec(N);
    for (int i = 0; i < N; ++i) {
      cin >> Vec[i];
    }
    vector<int> Vec1;
    for (int i = 0; i < N; ++i) {
      Vec1.push_back(M - Vec[i]);
      for (int j = 0; j < N; ++j) {
        int label = i * N + j;
        int temp = Vec[i] + Vec[j];
        Vec.push_back(temp);
        Vec1.push_back(M - temp);
      }
    }
    sort(Vec.begin(), Vec.end());
    sort(Vec1.begin(), Vec1.end());
    int size = N * N;
    for (int i = 0; i < M; ++i) {
      for (int j = 0; j < size; ++j) {
        if (binary_search(Vec.begin(), Vec.end(), Vec1[j] - i)) {
          cout << M - i << endl;
          goto NEXT;
        }
      }
    }
  NEXT:;
  }
}

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

#include <algorithm>
#include <iostream>
using namespace std;
int p[1001], s[1002001];
int main() {
  int n, m;
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int cnt = 1;
    p[0] = 0, s[0] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> p[i];
      for (int j = 0; j <= i; j++)
        s[cnt] = p[i] + p[j], cnt++;
    }
    sort(s, s + cnt);
    int ans = 0;
    for (int i = 0; i < cnt; i++) {
      int left = 0, right = cnt, mid;
      while (true) {
        if (left >= right - 1) {
          mid = (left + right) / 2;
          if (s[i] + s[mid] <= m)
            ans = max(ans, s[i] + s[mid]);
          break;
        }
        mid = (left + right) / 2;
        if (s[i] + s[mid] <= m) {
          ans = max(ans, s[i] + s[mid]);
          left = mid;
        } else
          right = mid;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int p[1001];
int main() {
  int n, m;
  while (true) {
    cin >> n >> m;
    if (n + m == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> p[i];
    p[n] = 0;
    sort(p, p + n + 1);
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= i; j++) {
        for (int k = 0; k <= j; k++) {
          int key = m - p[i] - p[j] - p[k];
          if (key >= 0) {
            int left = 0, right = k + 1, mid, cnt = 0;
            while (true) {
              if (left >= right - 1)
                break;
              mid = (left + right) / 2;
              if (p[mid] <= key) {
                cnt = max(cnt, mid);
                left = mid;
              } else
                right = mid;
            }
            ans = max(ans, m - key + p[cnt]);
          }
        }
      }
    }
    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;

int main() {
  int n, m, t;
  while (cin >> n >> m, n) {
    vector<int> data;
    for (int i = 0; i < n; i++) {
      cin >> t;
      data.push_back(t);
    }
    data.push_back(0);

    vector<int> ret;
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n + 1; j++) {
        ret.push_back(data[i] + data[j]);
      }
    }

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

    int r = 0;
    for (int i = 0; i < ret.size(); i++) {
      int t = m - ret[i];
      if (t < 0)
        continue;
      r = max(r, ret[i] + *(lower_bound(ret.begin(), ret.end(), t) - 1));
    }
    cout << r << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  /*cout << "1000 200000000" << endl;
  for(int i=1;i<=1000;i++){
          cout << i << endl;
  }
  cout << "0 0" << endl;
  return 0;*/
  int n, m, t;
  while (cin >> n >> m, n) {
    vector<int> bin;
    vector<int> data;
    for (int i = 0; i < n; i++) {
      cin >> t;
      data.push_back(t);
    }

    set<int> dp[4];

    for (int i = 0; i < n; i++) {
      dp[0].insert(data[i]);
    }

    for (int k = 1; k < 4; k++) {
      for (int i = 0; i < n; i++) {
        for (set<int>::iterator it = dp[k - 1].begin(); it != dp[k - 1].end();
             it++) {
          dp[k].insert((*it) + data[i]);
          bin.push_back((*it) + data[i]);
        }
      }
    }

    for (int k = 0; k < 4; k++) {
      for (set<int>::iterator it = dp[k].begin(); it != dp[k].end(); it++) {
        bin.push_back((*it));
      }
    }
    sort(bin.begin(), bin.end());

    cout << *(lower_bound(bin.begin(), bin.end(), m) - 1) << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (true) {
    cin >> n >> m;
    if (n == 0)
      return 0;
    vector<int> p(n);
    for (int i = 0; i < n; i++)
      cin >> p[i];
    p.push_back(0);
    n++;
    vector<int> s;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        if (p[i] + p[j] < m)
          s.push_back(p[i] + p[j]);
    }
    sort(s.begin(), s.end());
    int ans = 0, fw = 0, en = s.size() - 1;
    while (true) {
      if (s[fw] + s[en] > ans && s[fw] + s[en] <= m)
        ans = s[fw] + s[en];
      if (s[fw] + s[en] <= m)
        fw++;
      else
        en--;
      if (fw > en)
        break;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (true) {
    cin >> n >> m;
    if (n == 0)
      return 0;
    vector<int> p(n);
    for (int i = 0; i < n; i++)
      cin >> p[i];
    p.push_back(0);
    n++;
    set<int> s;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        if (p[i] + p[j] < m)
          s.insert(p[i] + p[j]);
    }
    int ans = 0;
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
      set<int>::iterator it2 = s.lower_bound(m - *it);
      if (it2 != s.begin())
        it2--;
      if (*it + *it2 < m)
        ans = max(ans, *it + *it2);
      else
        break;
    }
    cout << ans << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

int main(void) {
  ll n, m, p[1000010];
  while (cin >> n >> m) {
    if (n + m == 0)
      break;
    p[0] = 0;
    ll maxp = 0;
    for (ll i = 1; i <= n; i++) {
      cin >> p[i];
    }
    ll now = n + 1;
    for (ll i = 0; i <= n; i++) {
      for (ll j = i; j <= n; j++) {
        p[now++] = p[i] + p[j];
      }
    }
    sort(p, p + now);
    ll ans = 0;
    for (int i = 0; i < now; i++) {
      ll maxa = m - p[i];
      if (maxa < 0)
        continue;
      ll under = 0, upper = now;
      while (upper - under > 0) {
        ll mid = (under + upper) / 2;
        if (maxa > p[mid]) {
          under = mid + 1;
        } else if (maxa == p[mid]) {
          break;
        } else {
          upper = mid;
        }
      }
      ans = max(ans, p[i] + p[upper - 1]);
    }
    cout << ans << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;

int main(void) {
  ll n, m, p[2100];
  while (cin >> n >> m) {
    if (n + m == 0)
      break;
    p[0] = 0;
    ll maxp = 0;
    for (int i = 1; i <= n; i++) {
      cin >> p[i];
      if (p[i] <= m)
        maxp = max(maxp, p[i]);
    }
    int now = n + 1;
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        p[now] = p[i] + p[j];
        if (p[now] <= m)
          maxp = max(maxp, p[now]);
        now++;
      }
    }
    sort(p, p + now);
    ll maxa = m - maxp;
    ll under = 0, upper = now;
    while (upper - under > 1LL) {
      ll mid = (under + upper) / 2;
      if (maxa >= p[mid]) {
        under = mid;
      } else {
        upper = mid;
      }
    }
    cout << (maxp + p[under]) << "\\n";
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#define N_MAX 1001

using namespace std;

int N, M;
int P[N_MAX];
int S[1002001];
void solve() {
  int cnt = 0;
  for (int i = 0; i <= N; ++i) {
    for (int j = i; j <= N; ++j) {
      int tmp = P[i] + P[j];
      if (tmp > M)
        continue;
      S[cnt++] = tmp;
    }
  }

  sort(S, S + cnt);
  int ans = 0;
  for (int i = 0; i < cnt; ++i) {
    int *tmp = upper_bound(S, S + cnt, M - S[i]);
    --tmp;
    ans = max(ans, *tmp + S[i]);
  }
  cout << ans << endl;
}

int main() {
  while ((cin >> N >> M) && N && M) {
    P[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> P[i];
    }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#define N_MAX 1001

using namespace std;

int N, M;
int P[N_MAX];

void solve() {
  set<int> S;
  sort(P, P + N + 1);
  for (int i = 0; i <= N; ++i) {
    for (int j = i; j <= N; ++j) {
      for (int k = j; k <= N; ++k) {
        int tmp = P[i] + P[j] + P[k];
        if (tmp > M)
          continue;
        S.insert(tmp);
      }
    }
  }

  for (int eps = 0;; ++eps) {
    for (set<int>::iterator ii = S.begin(); ii != S.end(); ++ii) {
      int tmp = M - *ii - eps;
      if (tmp < 0)
        continue;
      if (binary_search(P, P + N + 1, tmp)) {
        cout << *ii + tmp << endl;
        return;
      }
    }
  }
}

int main() {
  while ((cin >> N >> M) && N && M) {
    P[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> P[i];
    }
    solve();
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: 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)
        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;
}

Error: TLE

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

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

using namespace std;

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;

    vector<int> ps;
    ps.push_back(0);
    for (int i = 0; i < N; i++) {
      int p;
      cin >> p;
      ps.push_back(p);
    }
    vector<int> d;
    for (size_t i = 0; i < ps.size(); i++) {
      for (size_t j = i; j < ps.size(); j++) {
        d.push_back(ps[i] + ps[j]);
      }
    }
    sort(d.begin(), d.end());
    int ret = 0;
    for (size_t i = 0; i < d.size(); i++) {
      if (d[i] > M)
        break;
      ret = max(ret, d[i] + *(lower_bound(d.begin(), d.end(), M - d[i]) - 1));
    }
    cout << ret << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;
    const int C = 4;
    vector<set<int>> ps(C + 1);
    ps[0].insert(0);
    for (int i = 0; i < N; i++) {
      int p;
      cin >> p;
      for (int j = 0; j < C; j++) {
        for (auto k : ps[j]) {
          if (k + p <= M) {
            ps[j + 1].insert(k + p);
          }
        }
      }
    }
    int ret = 0;
    for (int i = 0; i < C + 1; i++) {
      ret = max(ret, *max_element(ps[i].begin(), ps[i].end()));
    }
    cout << ret << 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;
  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;
}


Error: TLE

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());

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 MOD = 1000000007;

int N, M, arr[1005];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    arr[0] = 0;
    ++N;
    for (int j = 1; j < N; ++j) {
      cin >> arr[j];
    }
    vector<int> arr2;
    for (int j = 0; j < N; ++j)
      for (int k = 0; k < N; ++k)
        if (arr[j] + arr[k] <= M) {
          arr2.push_back(arr[j] + arr[k]);
        }
    sort(begin(arr2), end(arr2));
    const int NN = arr2.size();

    int ans = 0, r = NN - 1;
    for (int j = 0; j < NN; ++j) {
      while (arr2[j] + arr2[r] > M)
        --r;
      if (arr2[j] + arr2[r] <= M) {
        ans = max(ans, arr2[j] + arr2[r]);
      }
    }

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

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using int64 = int64_t;

constexpr int64 MOD = 1000000007;

int N, M, arr[1000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    arr[0] = 0;
    ++N;
    for (int j = 1; j < N; ++j) {
      cin >> arr[j];
    }
    vector<int> arr2;
    for (int j = 0; j < N; ++j)
      for (int k = j + 1; k < N; ++k) {
        arr2.push_back(arr[j] + arr[k]);
      }
    sort(begin(arr2), end(arr2));
    const int NN = arr2.size();

    int ans = 0, r = NN - 1;
    for (int j = 0; j < NN; ++j) {
      while (arr2[j] + arr2[r] > M)
        --r;
      ans = max(ans, arr2[j] + arr2[r]);
    }

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

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

int N, M;
vector<int> point;
vector<int> sel_two;

int find_max_under(int n) {
  int ub = sel_two.size() - 1;
  int lb = 0;
  int mid = (lb + ub) / 2;

  while (!(lb == ub - 1)) {
    mid = (lb + ub) / 2;
    if (sel_two[mid] < n) {
      lb = mid;
    } else if (sel_two[mid] > n) {
      ub = mid;
    } else {
      return n;
    }
  }
  return sel_two[lb];
}

int main() {
  while (cin >> N >> M, N || M) {
    point.clear();
    point.resize(N + 1);
    sel_two.clear();
    point[0] = 0;
    for (int i = 1; i <= N; i++)
      cin >> point[i];

    int max_point = 0;
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= N; j++)
        if (point[i] + point[j] <= M)
          sel_two.push_back(point[i] + point[j]);
    sort(sel_two.begin(), sel_two.end());
    for (int i = 0; i < sel_two.size(); i++) {
      int max_under = find_max_under(M - sel_two[i]);
      max_point = max(sel_two[i] + max_under, max_point);
    }
    //		for (int i = 0; i < sel_two.size(); i++) cout << sel_two[i] << '
    //'; 		cout << endl;

    cout << max_point << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int N, M;
vector<int> point;

int main() {
  while (cin >> N >> M, N || M) {
    point.clear();
    point.resize(N);
    for (int i = 0; i < N; i++)
      cin >> point[i];

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

    int max_point = 0;
    for (set<int>::iterator pos_i = pos.begin(); pos_i != pos.end(); pos_i++) {
      for (set<int>::iterator pos_j = pos.begin(); pos_j != pos.end();
           pos_j++) {
        if (*pos_i + *pos_j <= M)
          max_point = max(max_point, *pos_i + *pos_j);
      }
    }

    cout << max_point << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int n, m, ans;

void solve(vector<int> &p) {
  vector<int> q;

  sort(p.begin(), p.end());
  p.erase(unique(p.begin(), p.end()), p.end());
  for (int i = 0; i < p.size(); i++) {
    if (p[i] > m)
      break;

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

      q.push_back(p[i] + p[j]);
      ans = max(ans, p[i] + p[j]);
    }
  }

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

  for (int i = 0; i < q.size(); i++) {
    int idx = lower_bound(q.begin(), q.end(), m - q[i]) - q.begin() - 1;
    if (idx >= 0 && idx < q.size()) {
      ans = max(ans, q[i] + q[idx]);
    }

    idx = lower_bound(p.begin(), p.end(), m - q[i]) - p.begin() - 1;
    if (idx >= 0 && idx < p.size()) {
      ans = max(ans, q[i] + p[idx]);
    }
  }
}

int main() {
  while (cin >> n >> m, n || m) {
    vector<int> p;
    for (int i = 0; i < n; i++) {
      int e;
      cin >> e;
      p.push_back(e);
    }
    ans = 0;
    solve(p);
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

typedef pair<int, int> P;
int n, m, ans;
int p[1001];
map<P, bool> memo;

void solve(int s, int k) {
  if (s > m)
    return;

  ans = max(ans, s);
  if (k == 0 || memo.count(P(s, k)))
    return;
  memo[P(s, k)] = true;

  for (int i = 0; i < n; i++) {
    solve(s + p[i], k - 1);
  }
}

int main() {
  while (cin >> n >> m, n || m) {
    memo.clear();
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }
    ans = 0;
    solve(0, 4);
    cout << ans << endl;
  }
}