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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int n, m;
int main() {
  while (cin >> n >> m, n) {
    vector<int> v, w;
    REP(i, n) {
      int a;
      cin >> a;
      w.push_back(a);
    }
    w.push_back(0);
    REP(i, w.size()) REP(j, w.size()) {
      int t = w[i] + w[j];
      v.push_back(w[i] + w[j]);
    }
    sort(ALL(v));
    int ans = 0;
    int i = 0;
    int j = v.size() - 1;
    while (i <= j) {
      if (v[i] + v[j] <= m) {
        ans = max(ans, v[i] + v[j]);
        i++;
      } else {
        j--;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int n, m;
int main() {
  while (cin >> n >> m, n) {
    vector<int> v, w;
    REP(i, n) {
      int a;
      cin >> a;
      w.push_back(a);
    }
    w.push_back(0);
    set<int> used;
    REP(i, w.size()) REP(j, w.size()) {
      int t = w[i] + w[j];
      if (used.find(t) == end(used)) {
        used.insert(t);
        v.push_back(w[i] + w[j]);
      }
    }
    sort(ALL(v));
    int ans = 0;
    int i = 0;
    int j = v.size() - 1;
    while (i <= j) {
      if (v[i] + v[j] <= m) {
        ans = max(ans, v[i] + v[j]);
        i++;
      } else {
        j--;
      }
    }
    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 + m) {
    vector<int> v;
    int a;

    for (int i = 0; i < n; i++) {
      cin >> a;
      v.push_back(a);
    }

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

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

    int ans = 0;
    for (int i = 0; i < v.size(); i++) {
      int want = m - v[i];
      int l = 0, r = v.size() - 1;

      if (want < 0)
        continue;

      while (r - l > 1) {
        int mid = (l + r) / 2;
        if (v[mid] >= want) {
          r = mid;
        } else {
          l = mid;
        }
        if (v[l] + v[i] <= m) {
          ans = max(ans, v[l] + v[i]);
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;

  while (cin >> n >> m, n + m) {
    vector<int> v;
    int a;

    for (int i = 0; i < n; i++) {
      cin >> a;
      v.push_back(a);
    }

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

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

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

    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
long int n, m, a, ans;
vector<long int> ss1, aa;

int main() {

  while (1) {

    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    ans = 0;
    ss1.clear();
    aa.clear();

    aa.push_back(0);
    for (int i = 0; i < n; i++)
      cin >> a, aa.push_back(a);
    sort(aa.begin(), aa.end());

    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        if (aa[i] + aa[j] <= m)
          ss1.push_back(aa[i] + aa[j]);
      }
    }
    sort(ss1.begin(), ss1.end());

    int cnt = ss1.size() - 1;
    for (int i = 0; i < ss1.size(); i++) {
      while (ss1[i] + ss1[cnt] > m)
        cnt--;
      if (ss1[cnt] + ss1[i] <= m)
        ans = max(ans, ss1[cnt] + ss1[i]);
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long int n, m, a, ans;
vector<long int> pp, tt, ss2, ss1;

int main() {

  while (1) {

    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    pp.clear();
    tt.clear();
    ss1.clear();
    ss2.clear();
    ans = 0;

    for (int i = 0; i < n; i++) {
      cin >> a;
      if (i < n / 2)
        pp.push_back(a);
      else
        tt.push_back(a);
    }

    // for(int i=0;i<pp.size();i++)cout << pp[i] << " ";
    // for(int i=n/2;i<n;i++){
    //     cout << tt[a] << " ";
    //     a++;
    // }
    // cout << endl;

    for (int i = 0; i < pp.size(); i++) {
      for (int j = 0; j < pp.size(); j++) {
        ss1.push_back(pp[i] + pp[j]);
      }
    }
    for (int i = 0; i < tt.size(); i++) {
      for (int j = 0; j < tt.size(); j++) {
        ss2.push_back(tt[i] + tt[j]);
      }
    }
    for (int i = 0; i < pp.size(); i++)
      ss1.push_back(pp[i]);
    for (int i = 0; i < tt.size(); i++)
      ss2.push_back(tt[i]);

    cout << "#####" << endl;
    for (int i = 0; i < ss1.size(); i++)
      cout << ss1[i] << " ";
    cout << endl;
    for (int i = 0; i < ss2.size(); i++)
      cout << ss2[i] << " ";
    cout << endl;
    cout << "#####" << endl;

    for (int i = 0; i < ss1.size(); i++) {
      for (int j = 0; j < ss2.size(); j++) {
        if (ss1[i] + ss2[j] < m)
          ans = max(ans, ss1[i] + ss2[j]);
      }
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <vector>
#define REP(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
int main(void) {
  int n, m;
  while (scanf("%d%d", &n, &m), n + m) {
    vector<int> _point(0);
    vector<int> point(0);
    point.push_back(0);
    _point.push_back(0);
    rep(i, n) {
      int x;
      scanf("%d", &x);
      _point.push_back(x);
      for (int j = _point.size() - 1; j >= 0; j--) {
        point.push_back(x + _point[j]);
      }
    }

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

    int ma = 0;

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

      vector<int>::iterator idx =
          upper_bound(point.begin(), point.end(), m - point[i]);
      if (*idx == 0)
        continue;
      idx -= 1;
      int num = *idx;
      ma = max(ma, point[i] + num);
    }
    printf("%d\\n", ma);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
#define REP(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
int main(void) {
  int n, m;
  while (scanf("%d%d", &n, &m), n + m) {
    vector<int> _point(0);
    vector<int> point(0);
    point.push_back(0);
    _point.push_back(0);
    rep(i, n) {
      int x;
      scanf("%d", &x);
      _point.push_back(x);
      for (int j = _point.size() - 1; j >= 0; j--) {
        point.push_back(x + _point[j]);
      }
    }

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

    int ma = 0;

    for (int i = 0; point[i] <= m; i++) {
      vector<int>::iterator idx =
          upper_bound(point.begin(), point.end(), m - point[i]);

      idx -= 1;
      int num = *idx;
      ma = max(ma, point[i] + num);
    }
    printf("%d\\n", ma);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

// JOI 2008??´???3??? ?????????

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

using namespace std;

#define MAX -210000000
#define MIN 210000000

int a, b;
int d[1002], c[1001 * 1001], hk = 0, mcunt, low, high;
int x, pr = 0 /*,flg[200000000]*/, m, ff = 0;

int main() {

  scanf("%d %d", &a, &b);
  while (a != 0 && b != 0) {
    d[0] = 0;
    hk = 0;
    ff = 0;
    pr = 0;
    for (int i = 1; i <= a; i++)
      scanf("%d", &d[i]);

    for (int i = 0; i <= a; i++) {
      for (int j = 0; j <= a; j++) {
        c[hk] = d[i] + d[j];
        hk++;
      }
    }
    sort(c, c + hk);
    for (int i = 0; i < hk; i++) {
      x = b - c[i];
      low = 0;
      high = hk - 1;
      while (low <= high) {
        m = (low + high) / 2;
        if (x == c[m]) {
          ff = 1;
          break;
        }
        if (x > c[m])
          low = m + 1;
        else if (x < c[m])
          high = m - 1;
      }
      if (ff == 1) {
        pr = c[i] + c[m];
        break;
      }
      if (b > c[i] + c[high] && pr < c[i] + c[high]) {
        pr = c[i] + c[high];
      }
    }
    printf("%d\\n", pr);
    scanf("%d %d", &a, &b);
  }
  return 0;
}

Error: 0

Fixed submission:

// JOI 2008??´???3??? ?????????

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

using namespace std;

#define MAX -210000000
#define MIN 210000000

int a, b;
int d[1000], c[1000 * 1000 + 2], hk = 0, mcunt, low, high;
int x, pr = 0 /*,flg[200000000]*/, m, ff = 0;

int main() {

  scanf("%d %d", &a, &b);
  while (a != 0 && b != 0) {
    d[0] = 0;
    hk = 0;
    ff = 0;
    pr = 0;
    for (int i = 1; i <= a; i++)
      scanf("%d", &d[i]);

    for (int i = 0; i <= a; i++) {
      for (int j = 0; j <= a; j++) {
        // if(flg[d[i]+d[j]]!=1){
        c[hk] = d[i] + d[j];
        // printf("%lld\\n",c[cunt]);
        hk++;
        /*	flg[d[i]+d[j]]=1;

        }*/
      }
    }
    // printf("Sort???\\n");
    sort(c, c + hk);
    /*printf("\\n");
    for(int i=0;i<hk;i++) printf("%d\\n",c[i]);
    printf("\\n");

    printf("hk=%d\\n",hk);
    printf("c[low]=%d c[high]=%d\\n",c[hk],c[hk-1]);*/

    for (int i = 0; i < hk; i++) {
      x = b - c[i];
      // printf("x=%d\\n",x);
      low = 0;
      high = hk - 1;
      while (low <= high) {
        m = (low + high) / 2;
        if (x == c[m]) {
          ff = 1;
          break;
        }
        if (x > c[m])
          low = m + 1;
        else if (x < c[m])
          high = m - 1;
      }
      // printf("\\n%d\\n",low);
      if (ff == 1) {
        pr = c[i] + c[m];
        break;
      }
      if (b > c[i] + c[high] && pr < c[i] + c[high]) {
        pr = c[i] + c[high];
        //	printf("%d %d\\n",c[i],c[high]);
      }
    }
    printf("%d\\n", pr);
    scanf("%d %d", &a, &b);
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1002002];
int main(void) {
  while (1) {
    ll n, m, i, j, p[1001], hi, lo, mi, pp, x, mx;
    cin >> n >> m;
    if (n == 0)
      break;
    p[0] = 0;
    for (i = 1; i <= n; i++)
      cin >> p[i];
    pp = pow(n + 1, 2);
    for (i = 0; i <= pp + 1; i++)
      a[i] = 0;
    for (i = 0; i <= n; i++)
      for (j = 0; j <= n; j++)
        a[i * n + j] = p[i] + p[j];
    sort(a, a + (pp + 1));
    //		for(i=0;i<=pp+1;i++) cout<<a[i]<<endl;
    mx = 0;
    for (i = 0; i <= pp + 1; i++) {
      hi = pp + 1;
      lo = 0;
      while (hi >= lo) {
        mi = (hi + lo) / 2;
        x = a[i] + a[mi];
        if (x > m)
          hi = mi - 1;
        else if (x < m)
          lo = mi + 1;
      }
      x = a[i] + a[hi];
      if (mx < x && m >= x)
        mx = x;
    }
    cout << mx << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[1002002];
int main(void) {
  while (1) {
    ll n, m, i, j, p[1001], hi, lo, mi, pp, x, mx;
    cin >> n >> m;
    if (n == 0)
      break;
    p[0] = 0;
    for (i = 1; i <= n; i++)
      cin >> p[i];
    for (i = 0; i <= n; i++)
      for (j = 0; j <= n; j++)
        a[i * n + j] = p[i] + p[j];
    pp = pow(n + 1, 2);
    sort(a, a + (pp + 2));
    //		for(i=0;i<=pp+1;i++) cout<<a[i]<<endl;
    mx = 0;
    for (i = 0; i <= pp + 1; i++) {
      hi = pp + 1;
      lo = 0;
      while (hi >= lo) {
        mi = (hi + lo) / 2;
        x = a[i] + a[mi];
        if (x > m)
          hi = mi - 1;
        else if (x < m)
          lo = mi + 1;
      }
      x = a[i] + a[hi];
      if (mx < x && m >= x)
        mx = x;
    }
    cout << mx << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m;
  int p[1001];
  int s[1001000];
  for (;;) {
    cin >> n >> m;
    if (!n && !m)
      break;

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

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

    int N = (n + 1) * (n + 1);
    sort(s, s + N);

    int ans = 0;
    for (int i = 0; i < N; i++) {
      int hoge = upper_bound(s, s + N, m - s[i]) - s;
      // cout << s[i] << " " << s[hoge-1] << endl;
      if (hoge == 0)
        break;
      else if (hoge == N)
        ans = max(ans, s[i]);
      else
        ans = max(ans, s[i] + s[hoge - 1]);
    }

    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  int p[1001];
  for (;;) {
    cin >> n >> m;
    if (!n && !m)
      break;

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

    set<int> s;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++)
        s.insert(p[i] + p[j]);

    set<int>::iterator ite, hoge;

    int ans = 0;
    for (ite = s.begin(); ite != s.end(); ite++) {
      int tmp = *ite;
      hoge = upper_bound(s.begin(), s.end(), m - tmp);
      if (hoge == s.begin())
        break;
      else if (hoge == s.end())
        ans = max(ans, tmp);
      else
        ans = max(ans, tmp + (*(--hoge)));
    }

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

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

int N = 0, M = 0;
int points[1001];
int scores[1002001];

bool check(int a, int c) {
  if (a >= (N + 2) * (N + 1) / 2 - 1) {
    return true;
  }
  return (scores[a] <= c && scores[a + 1] > c);
}

int search2(int a, int b, int c) {
  int pib = a + b - b / 2;
  if (check(pib, c)) {
    return pib;
  } else if (scores[pib] < c) {
    return search2(pib, b / 2, c);
  } else {
    return search2(a, b / 2, c);
  }
}

int main() {
  for (;;) {
    for (int i = 0; i < 1001; i++) {
      points[i] = 0;
    }
    for (int i = 0; i < 1002001; i++) {
      scores[i] = 0;
    }
    if (cin >> N) {
      if (N == 0) {
        break;
      }
      cin >> M;
      for (int i = 1; i <= N; i++) {
        cin >> points[i];
      }
      for (int i = 0; i <= N; i++) {
        for (int j = i; j <= N; j++) {
          scores[(2 * N - i + 1) * i / 2 + j] = points[i] + points[j];
        }
      }
      int mxsco = 0;
      sort(scores, scores + ((N + 2) * (N + 1)) / 2);
      for (int i = 0; i < (N + 2) * (N + 1) / 2; i++) {
        if (scores[i] > M) {
          break;
        }
        int nosco = scores[i] +
                    scores[search2(0, (N + 2) * (N + 1) / 2, M - scores[i])];
        if (nosco >= mxsco && nosco <= M) {
          mxsco = nosco;
        }
      }
      cout << mxsco << endl;
    } else {
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int N = 0, M = 0;
int points[1001];
int scores[1002001];

bool check(int a, int c) {
  if (a >= (N + 2) * (N + 1) / 2 - 1) {
    return true;
  }
  return (scores[a] <= c && scores[a + 1] > c);
}

int search2(int a, int b, int c) {
  int pib = a + b - b / 2;
  if (check(pib, c)) {
    return pib;
  } else if (scores[pib] < c) {
    return search2(pib, b / 2, c);
  } else {
    return search2(a, b / 2, c);
  }
}

int main() {
  for (;;) {
    for (int i = 0; i < 1001; i++) {
      points[i] = 0;
    }
    for (int i = 0; i < 1002001; i++) {
      scores[i] = 0;
    }
    if (cin >> N) {
      if (N == 0) {
        break;
      }
      cin >> M;
      for (int i = 1; i <= N; i++) {
        cin >> points[i];
      }
      for (int i = 0; i <= N; i++) {
        for (int j = i; j <= N; j++) {
          scores[(2 * N - i + 1) * i / 2 + j] = points[i] + points[j];
        }
      }
      int mxsco = 0;
      sort(scores, scores + ((N + 2) * (N + 1)) / 2);
      for (int i = 0; i < (N + 2) * (N + 1) / 2; i++) {
        if (scores[i] > M) {
          break;
        }
        cout << scores[i] << " ";
        int ll = search2(0, (N + 2) * (N + 1) / 2, M - scores[i]);
        cout << ll << " ";
        cout << scores[ll] << " ";
        int nosco = scores[i] +
                    scores[search2(0, (N + 2) * (N + 1) / 2, M - scores[i])];
        cout << "now score is.." << nosco << endl;
        if (nosco >= mxsco && nosco <= M) {
          mxsco = nosco;
        }
      }
      cout << mxsco;
    } else {
      break;
    }
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> a;
vector<int> all;
int n, m;

int bainary_search(int i) {
  int L = 0;
  int R = all.size() - 1, M;

  while (L < R) {
    M = (L + R) / 2;
    if (all[i] + all[M] <= m)
      L = M + 1;
    else
      R = M;
  }

  if (L != 0)
    L--;
  return L;
}

int main() {

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    a.resize(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    a.push_back(0);
    all.clear();
    for (int i = 0; i <= n; i++)
      for (int j = i; j <= n; j++)
        all.push_back(a[i] + a[j]);

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

    int allsize = all.size();
    int ans = 0;
    for (int i = 0; i < allsize; i++) {
      int res = all[i] + all[bainary_search(i)];
      if (ans < res && res <= m)
        ans = res;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> a;
vector<int> all;
int n, m;

int bainary_search(int i) {
  int L = 0;
  int R = all.size() - 1, M;

  while (L < R) {
    M = (L + R) / 2;
    if (all[i] + all[M] > m)
      R = M;
    else
      L = M + 1;
  }
  L--;
  return L;
}

int main() {

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    a.resize(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    a.push_back(0);

    for (int i = 0; i < a.size(); i++)
      for (int j = i; j < a.size(); j++)
        all.push_back(a[i] + a[j]);

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

    int ans = 0;
    for (int i = 0; i < all.size(); i++) {
      // cout << all[i] <<endl;
      int res = all[i] + all[bainary_search(i)];
      // cout << res <<endl;
      if (ans < res && res <= m)
        ans = res;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> a;
vector<int> all;
int n, m;

int main() {

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    a.resize(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    a.push_back(0);
    all.clear();
    for (int i = 0; i < a.size(); i++)
      for (int j = i; j < a.size(); j++)
        all.push_back(a[i] + a[j]);

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

    int ans = 0;
    int allsize = all.size();
    int b = 0;

    for (int i = allsize - 1; i >= 0; i--) {
      for (int j = b; j < allsize; j++) {
        int sum = all[i] + all[j];
        if (sum > m)
          break;
        b++;
        ans = max(ans, sum);
      }
      if (ans == m)
        break;
    }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> a;
vector<int> all;
int n, m;

int main() {

  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    a.resize(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    a.push_back(0);
    all.clear();
    for (int i = 0; i < a.size(); i++)
      for (int j = i; j < a.size(); j++)
        all.push_back(a[i] + a[j]);

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

    int ans = 0;
    int allsize = all.size();
    int b = 0;

    for (int i = allsize - 1; i >= 0; i--) {
      for (int j = b; j < allsize; j++) {
        int sum = all[i] + all[j];
        if (sum > m)
          break;
        ans = max(ans, sum);
      }
      if (ans == m)
        break;
    }

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

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

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long int ll;

int N, M;
ll P[1111];
ll PP[11111111];
int p;

int main(int argc, char *argv[]) {
  while (cin >> N >> M, N) {
    int NN = N * N;
    rep(i, N) cin >> P[i];
    rep(i, N) rep(j, N) PP[i + j * N] = P[i] + P[j];
    sort(P, P + N);
    sort(PP, PP + NN);
    ll res = 0;
    res = max(res, P[upper_bound(P, P + N, M) - P - 1]);
    res = max(res, PP[upper_bound(PP, PP + NN, M) - PP - 1]);
    rep(i, N) res =
        max(res, PP[upper_bound(PP, PP + NN, M - P[i]) - PP - 1] + P[i]);
    rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
        res = max(res, PP[p] + PP[i]);
    rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
        res = max(res, PP[p] + PP[i]);
    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long int ll;

int N, M;
ll P[1111];
ll PP[1111];
int p;

int main(int argc, char *argv[]) {
  while (cin >> N >> M, N) {
    int NN = N * N;
    rep(i, N) cin >> P[i];
    rep(i, N) rep(j, N) PP[i + j * N] = P[i] + P[j];
    sort(P, P + N);
    sort(PP, PP + NN);
    ll res = 0;
    res = max(res, P[upper_bound(P, P + N, M) - P - 1]);
    res = max(res, PP[upper_bound(PP, PP + NN, M) - PP - 1]);
    rep(i, N) res =
        max(res, PP[upper_bound(PP, PP + NN, M - P[i]) - PP - 1] + P[i]);
    rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
        res = max(res, PP[p] + PP[i]);
    rep(i, NN) if ((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0)
        res = max(res, PP[p] + PP[i]);
    cout << res << endl;
  }

  return 0;
}