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

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

using namespace std;

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main() {
  int N;
  long long M;

  cin >> N >> M;

  while (N != 0 && M != 0) {
    vector<long long> p;

    long long ans = 0;

    for (int i = 0; i < N; ++i) {
      long long temp;
      cin >> temp;
      p.push_back(temp);
      ans = MAX(ans, temp);
    }

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

    for (int i = 0; i < N; ++i) {
      for (int j = i; j < N; ++j) {
        if (p[i] + p[j] > M)
          break;
        p.push_back(p[i] + p[j]);
        ans = MAX(ans, p[i] + p[j]);
      }
    }

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

    for (int i = 0; i < (int)p.size(); ++i) {
      auto itr = upper_bound(p.begin(), p.end(), M - p[i]);
      if (itr == p.begin())
        break;
      else
        --itr;
      ans = MAX(ans, p[i] + *itr);
    }

    cout << ans << endl;
    cin >> N >> M;
    p.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main() {
  int N;
  long long M;

  cin >> N >> M;

  while (N != 0 && M != 0) {
    vector<long long> p(N);
    set<long long> st[2];

    long long ans = 0;

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

    sort(p.begin(), p.begin() + N);

    for (int i = 0; i < N; ++i) {
      if (p[i] <= M)
        st[0].insert(p[i]);
      ans = MAX(ans, p[i]);
    }

    for (int i = 0; i < N; ++i) {
      for (auto itr = st[0].begin(); itr != st[0].end(); ++itr) {
        if (p[i] + *itr <= M) {
          st[1].insert(p[i] + *itr);
          ans = MAX(ans, p[i] + *itr);
        }
      }
    }

    for (auto itr0 = st[0].begin(); itr0 != st[0].end(); ++itr0) {
      auto itr1 = upper_bound(st[1].begin(), st[1].end(), M - *itr0);
      if (itr1 == st[1].begin())
        break;
      --itr1;
      ans = MAX(ans, *itr0 + *itr1);
    }

    for (auto itr0 = st[1].begin(); itr0 != st[1].end(); ++itr0) {
      auto itr1 = upper_bound(st[1].begin(), st[1].end(), M - *itr0);
      if (itr1 == st[1].begin())
        break;
      --itr1;
      ans = MAX(ans, *itr0 + *itr1);
    }

    cout << ans << endl;

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

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

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() { return val; }
    bool operator!=(Iterator &itr) { return val < itr.val; }
    void operator++() { ++val; }
  };
  Iterator i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

template <class T> inline T at(const vector<T> &v, int i) {
  return v[(i % (int)v.size() + v.size()) % v.size()];
}

template <class T> inline bool is_max(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
template <class T> inline bool is_min(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> p;
    for (int _ : range(n)) {
      int x;
      cin >> x;
      p.emplace_back(x);
    }
    p.emplace_back(0);
    vector<int> s;
    for (int i : p)
      for (int j : p)
        s.emplace_back(i + j);
    int res = 0;
    int i = 0, j = s.size() - 1;
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    while (i <= j) {
      if (s[i] + s[j] > m) {
        --j;
        continue;
      }
      is_max(res, s[i] + s[j]);
      ++i;
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() { return val; }
    bool operator!=(Iterator &itr) { return val < itr.val; }
    void operator++() { ++val; }
  };
  Iterator i, n;

public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator &begin() { return i; }
  Iterator &end() { return n; }
};

template <class T> inline T at(const vector<T> &v, int i) {
  return v[(i % (int)v.size() + v.size()) % v.size()];
}

template <class T> inline bool is_max(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
template <class T> inline bool is_min(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> p;
    for (int _ : range(n)) {
      int x;
      cin >> x;
      p.emplace_back(x);
    }
    p.emplace_back(0);
    set<int> s;
    for (int i : p)
      for (int j : p)
        s.insert(i + j);
    int res = 0;
    for (int i : s) {
      auto k = s.upper_bound(m - i);
      if (k == s.begin())
        continue;
      --k;
      is_max(res, i + *k);
    }
    cout << res << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    vector<int> p(n);
    for (int i = 0; i < n; i++)
      cin >> p[i];
    vector<int> a;
    a.push_back(0);
    a.insert(a.end(), p.begin(), p.end());
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        a.push_back(p[i] + p[j]);
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());

    int res = 0;
    for (int i = 0; i < a.size(); i++) {
      int j = upper_bound(a.begin(), a.end(), m - a[i]) - a.begin();
      if (j)
        res = max(res, a[i] + a[j - 1]);
    }
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    vector<int> p(n);
    for (int i = 0; i < n; i++)
      cin >> p[i];
    set<int> a;
    a.insert(0);
    for (int i = 0; i < n; i++) {
      a.insert(p[i]);
      for (int j = 0; j < n; j++)
        a.insert(p[i] + p[j]);
    }
    int res = 0;
    for (set<int>::iterator i = a.begin(), j; i != a.end(); ++i) {
      j = a.upper_bound(m - *i);
      if (j != a.begin()) {
        --j;
        res = max(res, *i + *j);
      }
    }
    cout << res << endl;
  }

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

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

using namespace std;

int N;
int M;
long long ans = 0;

/*
int binarySearch(int t){
    int lo=0,hi=N-1;
    int te;
    while(true){
        int m=(lo+hi)/2;
        if(P[m]<t){
            hi=(m-1)<=1?0:m-1;
        }else if(P[m]>t){
            lo=m+1;
        }
        if(lo>=hi){
            te=hi;
        }
    }
    return te;
}*/
/*
void func(int dart,long long sum,long long index){
    ans=max(ans,sum);
    if(dart==4)return ;
    long long s=M-sum;
    auto itF = (P.begin()+index);
//    auto itF = P.begin();
//    advance(itF,index);

    auto it = upper_bound(P.begin(),itF,s);
    it = prev(it);


    for(auto i=it;i!=P.begin();i--){
        func(dart+1,sum+*i,distance(P.begin(),i));
    }


    if(sum+P[n]<=M){
        func(n-1,dart+1,sum+P[n]);
        func(n,1+dart,sum+P[n]);
        func(n-1,dart,sum);
    }
    else {
        func(n-1,dart,sum);
    }


    return ;
}
*/

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

    vector<long long> v;
    for (int i = 0; i < N + 1; i++) {
      for (int j = 0; j < N + 1; j++) {
        if (P[i] + P[j] <= M)
          v.push_back(P[i] + P[j]);
      }
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    for (int i = 0; i < v.size(); i++) {
      long long ind = upper_bound(v.begin(), v.end(), M - v[i]) - v.begin();
      ans = max(ans, v[i] + v[ind - 1]);
    }
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}
// 574 999424


Error: TLE

Fixed submission:

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

using namespace std;

int N;
int M;
vector<int> P;
int ans = 0;

/*
int binarySearch(int t){
    int lo=0,hi=N-1;
    int te;
    while(true){
        int m=(lo+hi)/2;
        if(P[m]<t){
            hi=(m-1)<=1?0:m-1;
        }else if(P[m]>t){
            lo=m+1;
        }
        if(lo>=hi){
            te=hi;
        }
    }
    return te;
}*/

void func(int dart, int sum) {
  ans = max(ans, sum);
  if (dart == 4)
    return;
  int s = M - sum;
  auto it = upper_bound(P.begin(), P.end(), s);
  it = prev(it);
  for (auto i = it; i != P.begin(); i--) {
    func(dart + 1, sum + *it);
  }

  /*
  if(sum+P[n]<=M){
      func(n-1,dart+1,sum+P[n]);
      func(n,1+dart,sum+P[n]);
      func(n-1,dart,sum);
  }
  else {
      func(n-1,dart,sum);
  }
   */

  return;
}
int main() {
  while (cin >> N >> M, N, M) {
    P = vector<int>(N + 1, 0);
    for (int i = 0; i < N; i++) {
      cin >> P[i];
    }
    sort(P.begin(), P.end());
    P.erase(unique(P.begin(), P.end()), P.end());
    func(0, 0);
    cout << ans << endl;
    ans = 0;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  while (1) {
    int i, j, n, m, o = 1, s = 0;
    static int p[1001] = {0}, up[1001 * 1001] = {0};
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    for (i = 1; i <= n; i++) {
      scanf("%d", &p[i]);
      for (j = i; j >= 0; j--) {
        up[o] = p[i] + p[j];
        o++;
      }
    }
    sort(up, up + o);
    for (i = 0; i < o; i++) {
      if (up[i] + *(upper_bound(up, up + o, m - up[i]) - 1) <= m)
        s = max(s, up[i] + *(upper_bound(up, up + o, m - up[i]) - 1));
    }
    printf("%d\\n", s);
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  while (1) {
    int i, j, n, m, o = 1, s = 0;
    static int p[1001] = {0}, up[1001 * 1001] = {0};
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;
    for (i = 1; i <= n; i++) {
      scanf("%d", &p[i]);
      for (j = i; j > 0; j--) {
        up[o] = p[i] + p[j];
        o++;
      }
    }
    sort(up, up + o);
    for (i = 0; i < o; i++)
      s = max(s, up[i] + *(upper_bound(up, up + o, m - p[i]) - 1));
    printf("%d\\n", s);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
long long a[1001];
long long b[1000000];
int main() {
  int n, m;
  while (cin >> n >> m && n != 0 && m != 0) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    a[n] = 0;
    int f = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        b[f] = a[i] + a[j];
        f++;
      }
    }
    long long sum = 0;
    sort(b, b + f);
    for (int i = 0; i <= f; i++) {
      int s = 0, e = f, t;
      long long g = m - b[i];
      while (e - s > 1) {
        t = (e + s) / 2;
        if (b[t] > g)
          e = t;
        if (b[t] <= g)
          s = t;
      }
      if (b[i] + b[s] <= m)
        sum = max(sum, b[i] + b[s]);
      if (b[i] + b[e] <= m)
        sum = max(sum, b[i] + b[e]);
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
long long a[1001];
long long b[1000000];
int main() {
  int n, m;
  while (cin >> n >> m && n != 0 && m != 0) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    a[n] = 0;
    int t = 0;
    long long sum = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        b[i * n + j] = a[i] + a[j];
        t++;
      }
    }
    for (int i = 0; i < t; i++) {
      for (int j = 0; j < t; j++) {
        if (b[i] + b[j] <= m)
          sum = max(sum, b[i] + b[j]);
      }
    }
    cout << sum << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int, int> P;

int p[1001];
int h[1000 * 1000 + 1001];

int main() {
  int n, m, ans;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    p[0] = 0;
    ans = 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) {
        h[i * n + j] = p[i] + p[j];
      }
    }
    sort(h, h + n * n + 1);
    /*
    for(int i=0; i<=n*n; ++i){
            cout << h[i] << "\\n";
    }
    */
    for (int i = 0; i <= n; ++i) {
      for (int j = 0; j <= n; ++j) {
        int tmp = p[i] + p[j];
        int l = 0, r = n * n;
        int cnt = 0;
        while (1) {
          int mid = (r + l) / 2;
          if (cnt >= 30) {
            if (h[mid] + tmp <= m)
              ans = max(ans, h[mid] + tmp);
            break;
          }
          if (h[mid] + tmp > m) {
            r = mid;
          } else if (h[mid] + tmp < m) {
            l = mid;
          } else if (h[mid] + tmp == m) {
            ans = m;
            break;
          }
          ++cnt;
        }
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int, int> P;

int p[1001];
int h[1000 * 1000 + 1];

int main() {
  int n, m, ans;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    p[0] = 0;
    ans = 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) {
        h[i * n + j] = p[i] + p[j];
      }
    }
    sort(h, h + n * n + 1);
    /*
    for(int i=0; i<=n*n; ++i){
            cout << h[i] << "\\n";
    }
    */
    for (int i = 0; i <= n; ++i) {
      for (int j = 0; j <= n; ++j) {
        int tmp = p[i] + p[j];
        int l = 0, r = n * n;
        int cnt = 0;
        while (1) {
          int mid = (r + l) / 2;
          if (cnt >= 50) {
            if (h[mid] + tmp <= m)
              ans = max(ans, h[mid] + tmp);
            break;
          }
          if (h[mid] + tmp > m) {
            r = mid;
          } else if (h[mid] + tmp < m) {
            l = mid;
          } else if (h[mid] + tmp == m) {
            ans = m;
            break;
          }
          ++cnt;
        }
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

Error: 0

Fixed submission:

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

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

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

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int N, M, P[1001];
vector<int> PP;
int main() {
  while (scanf("%d %d", &N, &M), N | M) {
    PP.clear();
    memset(P, 0, sizeof(P));
    for (int i = 1; i <= N; i++)
      scanf("%d", &P[i]);
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= N; j++) {
        PP.push_back((P[i] + P[j]) % M);
      }
    sort(PP.begin(), PP.end());
    PP.erase(unique(PP.begin(), PP.end()), PP.end());
    int ans = 0;
    for (int Qi = 0; Qi < PP.size(); Qi++) {
      int lb = 0, rb = PP.size() - 1;
      while ((rb - lb) > 1) {
        int mid = (lb + rb) / 2;
        if (PP[mid] <= (M - PP[Qi]))
          lb = mid;
        else
          rb = mid - 1;
      }
      int Qj = PP[lb];
      if (PP[rb] <= (M - PP[Qi]))
        Qj = max(Qj, PP[rb]);
      ans = max(ans, PP[Qi] + Qj);
    }
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int N, M, P[1000];
int main() {
  while (scanf("%d %d", &N, &M), N | M) {
    for (int i = 0; i < N; i++)
      scanf("%d", &P[i]);
    int ans = 0;
    vector<int> X, Y;
    X.push_back(0);
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < X.size(); j++) {
        for (int k = 0; k < N; k++) {
          int v = (X[j] + P[k]) % M;
          ans = max(ans, v);
          Y.push_back(v);
        }
      }
      Y.erase(unique(Y.begin(), Y.end()), Y.end());
      X = Y;
    }
    printf("%d\\n", ans);
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int a;
  long long b;
  cin >> a >> b;
  while (a != 0 && b != 0) {
    int i = 0;
    long long su[1001];
    static long long su2[1000000];
    for (i = 0; i < a; i++) {
      cin >> su[i];
    }
    int o = 0;
    for (i = 0; i < a; i++) {
      int k = 0;
      for (k = 0; k < a; k++) {
        su2[o] = su[i] + su[k];
        o++;
      }
    }
    sort(su, su + a, greater<long long>());
    sort(su2, su2 + a * a, greater<long long>());
    long long sum = 0;
    long long max = 0;
    int p = lower_bound(su, su + a, b, greater<long long>()) - su;
    if (p != 0 && su[p] == b) {
      max = b;
      goto todo;
    } else {
      int v = 0;
      for (v = 0; v < a; v++) {
        p = lower_bound(su2, su2 + a * a, b - su[v], greater<long long>()) -
            su2;
        if (su2[p] + su[v] > max && p != a * a) {
          max = su2[p] + su[v];
        }
      }
      if (max) {
        sum = max;
      } else {
        p = lower_bound(su, su + a, b, greater<long long>()) - su;
        if (p != a) {
          max = su[p];
          goto todo;
        }
      }
      p = lower_bound(su2, su2 + a * a, b, greater<long long>()) - su2;
      if (su2[p] > max && p != a * a) {
        max = su2[p];
      }
      for (v = 0; v < a * a; v++) {
        p = lower_bound(su2, su2 + a * a, b - su2[v], greater<long long>()) -
            su2;
        if (su2[v] + su2[p] > max && p != a * a) {
          max = su2[v] + su2[p];
        }
      }
    }
  todo:
    cout << max << "\\n";
    cin >> a >> b;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define MAXSIZE 1005
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

int main() {
  set<int, greater<int>> S;
  vector<int> v;
  int a[MAXSIZE];
  int n, m, ans;
  while (scanf("%d%d", &n, &m), n + m) {
    ans = 0;
    for (int i = 1; i <= n; i++)
      scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++)
      v.push_back(a[i]), S.insert(a[i]);
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++)
        v.push_back(a[i] + a[j]), S.insert(a[i] + a[j]);
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < (int)v.size(); i++) {
      if (v[i] >= m)
        break;
      int num = *S.lower_bound(m - v[i]);
      if (num + v[i] < m)
        ans = max(ans, num + v[i]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <stdio.h>
#include <vector>

using namespace std;

int main(void) {
  int n, m;
  int s;

  while (1) {

    vector<int> p = vector<int>();
    vector<int> s2 = vector<int>();

    scanf("%d %d", &n, &m);

    if (n == 0 && m == 0) {
      break;
    }

    for (int i = 0; i < n; i++) {
      int k;
      scanf("%d", &k);
      p.push_back(k);
    }

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

    sort(s2.begin(), s2.end());
    int max = -1;
    for (int i = 0; i < s2.size(); i++) {
      vector<int>::iterator it = upper_bound(s2.begin(), s2.end(), m - s2[i]);
      // printf("%d\\n", *(it - 1));

      if (it != s2.begin() && max < s2[i] + *(it - 1)) {
        max = s2[i] + *(it - 1);
      }
    }

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

  return (0);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>

using namespace std;

int main(void) {
  int n, m;
  int s;
  vector<int> p;
  vector<int> s2;

  while (1) {
    scanf("%d %d", &n, &m);

    if (n == 0 && m == 0) {
      break;
    }

    for (int i = 0; i < n; i++) {
      int k;
      scanf("%d", &k);
      p.push_back(k);
    }

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

    sort(s2.begin(), s2.end());
    int max = -1;
    for (int i = 0; i < s2.size(); i++) {
      vector<int>::iterator it = upper_bound(s2.begin(), s2.end(), m - s2[i]);
      // printf("%d\\n", *(it - 1));

      if (it != s2.begin() && max < s2[i] + *(it - 1)) {
        max = s2[i] + *(it - 1);
      } else if (max < s2[i]) {
        max = s2[i];
      }
    }

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

  return (0);
}