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

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define int long long
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
static const int INF = 1 << 24;

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

// v.erase(unique(v.begin(),v.end()),v.end());

signed main() {
  int n;
  int m;
  while (cin >> n >> m, n || m) {
    // int n;
    // cin>>n;
    // int m;
    // cin>>m;
    vint v(n);
    rep(i, n) { cin >> v[i]; }
    v.PB(0);
    vint tmp;
    rep(i, n + 1) {
      rep(j, n + 1) { tmp.PB(v[i] + v[j]); }
    }
    sort(ALL(tmp));
    int ans = 0;
    tmp.erase(unique(ALL(tmp)), tmp.end());
    rep(i, tmp.size()) {
      vint::iterator it = upper_bound(ALL(tmp), m - tmp[i]);
      it--;
      if (*it + tmp[i] <= m) {
        ans = max(ans, *it + tmp[i]);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define int long long
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
static const int INF = 1 << 24;

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

// v.erase(unique(v.begin(),v.end()),v.end());

signed main() {
  int n;
  int m;
  while (cin >> n >> m, n || m) {
    // int n;
    // cin>>n;
    // int m;
    // cin>>m;
    vint v(n);
    rep(i, n) { cin >> v[i]; }
    v.PB(0);
    vint tmp;
    rep(i, n + 1) {
      rep(j, n + 1) { tmp.PB(v[i] + v[j]); }
    }
    sort(ALL(tmp));
    int ans = 0;
    tmp.erase(unique(ALL(tmp)), tmp.end());
    rep(i, tmp.size()) {
      rep(j, tmp.size()) {
        if (tmp[i] + tmp[j] <= m) {
          ans = max(ans, tmp[i] + tmp[j]);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m;
int p[1000];
int ans;
vector<int> v;

void solve() {
  ans = 0;
  v.clear();
  v.push_back(0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i]);
    if (p[i] <= m)
      v.push_back(p[i]);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (p[i] + p[j] <= m)
        v.push_back(p[i] + p[j]);
    }
  }
  sort(v.begin(), v.end());
  int j = v.size() - 1;
  for (int i = 0; i < v.size(); i++) {
    while (true) {
      if (v[i] + v[j] <= m) {
        ans = max(ans, v[i] + v[j]);
        break;
      }
      j--;
    }
  }
  printf("%d\\n", ans);
}

int main() {
  while (scanf("%d%d", &n, &m), n || m)
    solve();
}

Error: TLE

Fixed submission:

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

int n, m;
int p[1000];
int ans;
set<int> s;
set<int>::iterator it;
vector<int> v;

void solve() {
  ans = 0;
  v.clear();
  s.clear();
  s.insert(0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i]);
    if (p[i] <= m)
      s.insert(p[i]);
  }
  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]);
    }
  }
  for (it = s.begin(); it != s.end(); it++)
    v.push_back(*it);
  int j = v.size() - 1;
  for (int i = 0; i < v.size(); i++) {
    while (true) {
      if (v[i] + v[j] <= m) {
        ans = max(ans, v[i] + v[j]);
        break;
      }
      j--;
    }
  }
  printf("%d\\n", ans);
}

int main() {
  while (scanf("%d%d", &n, &m), n || m)
    solve();
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n;
  int M;
  vector<int> p;
  vector<int> q;
  while (true) {
    cin >> n >> M;
    if (n == 0)
      break;
    p.clear();
    int k;
    for (int i = 0; i < n; i++) {
      cin >> k;
      p.push_back(k);
    }
    p.push_back(0);
    n++;
    sort(p.begin(), p.end());
    q.clear();
    int u = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        q.push_back(p[i] + p[j]);
        u++;
      }
    }
    sort(q.begin(), q.end());
    int s = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        int c = p[i] + p[j];
        if (c > M)
          continue;
        if (c == M) {
          s = M;
          break;
        }
        if (M - c < q[0]) {
          s = max(s, c);
          continue;
        }
        if (q[u - 1] <= M - c) {
          s = max(s, c + q[u - 1]);
          continue;
        }
        s = max(s, c);
        int x = 0;
        int y = u - 1;
        while (true) {
          int k = (x + y) / 2;
          if (q[k] < M - c) {
            s = max(s, c + q[k]);
            if (k == y)
              break;
            x = k + 1;
            continue;
          } else if (q[k] == M - c) {
            s = M;
            break;
          } else {
            if (x == k)
              break;
            y = k - 1;
            continue;
          }
        }
      }
    }
    cout << s << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int s = 0;
int M = 0;
vector<int> p;

int kansu(
    int b, int e, int c,
    int d) { // b〜eの範囲を調べる cが残りの得点 dが残りのダーツ数(自身含む)
  int x = b;
  int y = e;
  while (true) {
    int k = (x + y + 1) / 2;
    if (k == y) {
      break;
    }
    double l = c / d;
    if (p[k] < l) {
      s = max(s, (M - c) + p[k] * d);
      x = k;
      continue;
    } else if (p[k] == l) {
      s = M;
      return 1;
    } else {
      y = k;
      continue;
    }
  }
  for (int i = x; i <= e; i++) {
    if (p[i] > c) {
      s = max(s, M - c);
      return 0;
    } else if (p[i] == c) {
      s = M;
      return 1;
    } else {
      if (d == 1) {
        s = max(s, (M - c) + p[i]);
      } else {
        if (kansu(0, i, c - p[i], d - 1) == 1)
          return 1;
      }
    }
  }
  return 0;
}

int main() {
  int n;
  while (true) {
    cin >> n >> M;
    if (n == 0)
      break;
    p.clear();
    int k;
    for (int i = 0; i < n; i++) {
      cin >> k;
      p.push_back(k);
    }
    sort(p.begin(), p.end());
    s = 0;
    kansu(0, n - 1, M, 4);
    cout << s << endl;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

int N, M;

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  while (N) {
    int num[1001];
    int ans = 0;
    set<int> F;
    set<int> S;
    for (int i = 0; i < N; i++)
      cin >> num[i];
    F.insert(0);
    for (int i = 0; i < N / 2; i++) {
      F.insert(num[i]);
      for (int j = 0; j < N / 2; j++) {
        F.insert(num[i] + num[j]);
      }
    }
    S.insert(0);
    for (int i = N / 2; i < N; i++) {
      S.insert(num[i]);
      for (int j = N / 2; j < N; j++) {
        S.insert(num[i] + num[j]);
      }
    }
    set<int>::reverse_iterator ritr = S.rbegin();
    for (auto i : F) {
      if (ritr == S.rend())
        break;
      while ((i + *ritr) > M) {
        ++ritr;
        if (ritr == S.rend())
          break;
      }
      if (ritr != S.rend())
        ans = max(ans, i + *ritr);
    }
    cout << ans << endl;
    cin >> N >> M;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

int N, M;

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  while (N) {
    int num[1001];
    int ans = 0;
    set<int> F;
    set<int> S;
    for (int i = 0; i < N; i++)
      cin >> num[i];
    num[N] = 0;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        F.insert(num[i] + num[j]);
      }
    }
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        S.insert(num[i] + num[j]);
      }
    }
    set<int>::reverse_iterator ritr = S.rbegin();
    for (auto i : F) {
      if (ritr == S.rend())
        break;
      while ((i + *ritr) > M) {
        ++ritr;
        if (ritr == S.rend())
          break;
      }
      if (ritr != S.rend())
        ans = max(ans, i + *ritr);
    }
    cout << ans << endl;
    cin >> N >> M;
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, p[1001], ans, yoso;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vector<int> vec;
    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++) {
        vec.push_back(p[i] + p[j]);
      }
    }
    sort(vec.begin(), vec.end());
    ans = 0;
    for (int i = 0; i <= vec.size(); i++) {
      yoso = upper_bound(vec.begin(), vec.end(), m - vec[i]) - vec.begin() - 1;
      if (yoso >= 0)
        ans = max(ans, vec[i] + vec[yoso]);
    }
    cout << ans << endl;
  }
  return (0);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, p[1001], ans;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vector<int> vec1, vec2;
    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++) {
        vec1.push_back(p[i] + p[j]);
        vec2.push_back(p[i] + p[j]);
      }
    }
    ans = 0;
    for (int i = 0; i < vec1.size(); i++) {
      for (int j = 0; j < vec2.size(); j++) {
        if (m >= vec1[i] + vec2[j])
          ans = max(ans, vec1[i] + vec2[j]);
      }
    }
    cout << ans << endl;
  }
  return (0);
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

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

int n, m, point1[1001], sum, ans;
vector<int> point2;

int solve(int start, int end, int border) {
  int mid;
  while (1) {
    if ((end + start) % 2) {
      mid = (end + start) / 2 + 1;
    } else {
      mid = (end + start) / 2;
    }

    if (point2[mid] > border) {
      end = mid;
    } else {
      start = mid;
    }

    if (end - start == 1) {
      if (point2[end] < border) {
        return point2[end];
      } else {
        return point2[start];
      }
    }
  }
}

int main() {
  while (cin >> n >> m && n && m) {
    for (int i = 0; i < n; i++) {
      cin >> point1[i];
    }
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n + 1; j++) {
        point2.push_back(point1[i] + point1[j]);
      }
    }

    sort(point1, point1 + n + 1);
    sort(point2.begin(), point2.end());

    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < n + 1; j++) {
        sum = point1[i] + point1[j];
        if (sum > m) {
          continue;
        }
        ans = max(ans, sum + solve(0, point2.size() - 1, m - sum));
      }
    }

    cout << ans << endl;

    memset(point1, 0, sizeof(point1));
    point2.clear();
    ans = 0;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int n, m, point1[1001], sum, ans;
vector<int> point2;

int solve(int start, int end, int border) {
  int mid;
  while (1) {
    if ((end + start) % 2) {
      mid = (end + start) / 2 + 1;
    } else {
      mid = (end + start) / 2;
    }

    if (point2[mid] > border) {
      end = mid;
    } else {
      start = mid;
    }

    if (end - start == 1) {
      if (point2[end] < border) {
        return point2[end];
      } else {
        return point2[start];
      }
    }
  }
}

int main() {
  while (cin >> n >> m && n && m) {
    for (int i = 0; i < n; i++) {
      cin >> point1[i];
    }
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < m + 1; j++) {
        point2.push_back(point1[i] + point1[j]);
      }
    }

    sort(point1, point1 + n + 1);
    sort(point2.begin(), point2.end());

    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < m + 1; j++) {
        sum = point1[i] + point1[j];
        if (sum > m) {
          continue;
        }
        ans = max(ans, sum + solve(0, point2.size() - 1, m - sum));
      }
    }

    cout << ans << endl;

    memset(point1, 0, sizeof(point1));
    point2.clear();
    ans = 0;
  }

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int n, m;
int p[1001];

int solve() {
  int pp[(n + 1) * (n + 1)];

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      pp[n * i + j] = p[i] + p[j];
      // cout<<"pp:"<<pp[n*i+j]<<endl;
    }
  }
  /*
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      cout<<"ij:"<<n*i+j<<"\\t"<<pp[i*n+j]<<endl;
    }
  }
  */
  int ans = 0;

  sort(pp, pp + n * n + n + 1);
  /*
  for(int i = 0;i<=n;i++){
    for(int j = 0;j<=n;j++){
      cout<<"ij:"<<n*i+j<<"\\t"<<pp[i*n+j]<<endl;
    }
  }
  */
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      int *it = upper_bound(pp, pp + n * n + n + 1, m - p[i] - p[j]);
      --it;
      // cout<<"it"<<*it<<endl;
      // cout<<"ij:"<<p[i]<<"\\t"<<p[j]<<endl;
      // cout<<"ans:"<<p[i]+p[j]+(*it)<<endl;
      if (p[i] + p[j] > m)
        continue;
      ans = max(ans, p[i] + p[j] + (*it));
    }
  }
  return ans;
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    p[0] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> p[i];
    }

    cout << solve() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int n, m;
int p[1000];

int solve(int num, int sum) {
  if (sum > m)
    return 0;
  if (num == 5)
    return 0;

  int ret = sum;
  for (int i = 0; i < n; i++) {
    ret = max(ret, solve(num + 1, sum + p[i]));
  }
  return ret;
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }

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

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

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    int latte[1001], ans = 0;
    vector<int> comb;
    latte[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> latte[i];
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        comb.push_back(latte[i] + latte[j]);
      }
    }
    sort(comb.begin(), comb.end());
    // for(int i=0;i<comb.size();i++) printf("%d ",comb[i]); puts("");
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        vector<int>::iterator itr;
        itr = upper_bound(comb.begin(), comb.end(), M - latte[i] - latte[j]);
        if (itr != comb.begin())
          ans = max(ans, latte[i] + latte[j] + *(itr - 1));
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int dp[100000000];

int main() {
  memset(dp, -1, sizeof(dp));
  /*int N,M;
  while(cin>>N>>M,N||M){
          int dp[200000001]={},P,ans=0;
          dp[0]=1;
          for(int i=0;i<N;i++){
                  cin>>P;
                  for(int j=M;j>=0;j--){

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long n, m;
long long a, x;
long long i, j;
vector<long long> p, t;
int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    p.clear();
    t.clear();
    p.push_back(0);
    for (i = 0; i < n; i++) {
      cin >> a;
      p.push_back(a);
    }
    sort(p.begin(), p.end());
    for (i = 0; i <= n; i++) {
      j = i;
      while (p[i] + p[j] <= m) {
        t.push_back(p[i] + p[j]);
        if (j == n)
          break;
        j++;
      }
    }
    sort(t.begin(), t.end());
    x = 0;
    j = t.size() - 1;
    // for(i=0;i<p.size();i++) cout << "PP" << p[i];
    // for(i=0;i<t.size();i++) cout << "TT" << t[i];
    for (i = 0; i <= j; i++) {
      while (t[i] + t[j] > m)
        j--;
      if (x < t[i] + t[j]) {
        x = t[i] + t[j];
        // cout <<"AA"<< t[i] << " " << t[j];
      }
    }
    cout << x << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream> //JOI 2007 ??¬??? 3??????
using namespace std;
int max_p[200000001];
int p[1001];
int main() {
  long n, m;
  long i, j;
  int r;
  while (cin >> n >> m, n) {
    for (i = 0; i <= m; i++) {
      max_p[i] = 0;
    }
    for (i = 0; i < n; i++) {
      cin >> p[i];
    }
    max_p[0] = 1;
    for (i = 0; i < m; i++) {
      if (max_p[i] >= 1 && max_p[i] < 5) {
        r = max_p[i];
        for (j = 0; j < n; j++) {
          if (i + p[j] <= m) {
            if (max_p[i + p[j]] == 0 || r + 1 < max_p[i + p[j]]) {
              max_p[i + p[j]] = r + 1;
            }
          }
        }
      }
    }
    for (i = m; i >= 0; i--) {
      if (max_p[i] != 0) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Runtime Error

// Solution for AOJ 0529: Darts
#include <algorithm>
#include <iostream>
using namespace std;
const int MAX_N = 1000, MAX_M = 200000000;
int n, m;
int p[MAX_N + 1], p2[MAX_N * MAX_N], ans, roop, s, mid;
void search(int end, int start) {
  if ((end + start) % 2)
    mid = (end + start) / 2 + 1;
  else
    mid = (end + start) / 2;
  if (p2[mid] == s)
    return;
  if (p2[mid] < s) {
    if (end - mid == 1) {
      if (p2[end] < s)
        mid++;
      return;
    }
    search(end, mid);
  } else {
    if (mid - start == 1) {
      mid--;
      return;
    }
    search(mid, start);
  }
}
int main() {
  while (cin >> n >> m) {
    ans = 0;
    if (!n && !m)
      return 0;
    for (int i = 1; i <= n; i++) {
      cin >> p[i];
    }
    roop = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        if (p[i] + p[j] <= m) {
          p2[roop] = p[i] + p[j];
          roop++;
        }
      }
    }
    sort(p2, p2 + roop);
    for (int i = 0; i < roop; i++) {
      s = m - p2[i];
      search(roop - 1, 0);
      ans = max(ans, p2[i] + p2[mid]);
      if (ans == m)
        break;
    }
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

// Solution for AOJ 0529: Darts
#include <iostream>
#include <queue>
using namespace std;
const int MAX_M = 200000000;
int n, m, p[1000], save;
bool dp[MAX_M + 1];
queue<int> que;
int main() {
  while (cin >> n >> m) {
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }
    for (int i = 0; i < m; i++) {
      dp[i] = 0;
    }
    dp[0] = 1;
    que.push(0);
    for (int i = 0; i < n; i++) {
      dp[p[i]] = 1;
      que.push(p[i]);
    }
    for (int i = 1; i < 4; i++) {
      save = que.size();
      for (int k = 0; k < save; k++) {
        for (int j = 0; j < n; j++) {
          if (que.front() + p[j] < m) {
            dp[que.front() + p[j]] = 1;
            que.push(que.front() + p[j]);
          }
        }
        que.pop();
      }
    }
    for (int i = m - 1; i >= 0; i--) {
      if (dp[i] != 0) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (1) {
    int n, m, a, ans = 0;
    vector<int> p, pp;
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    p.push_back(0);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a);
      p.push_back(a);
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        pp.push_back(p[i] + p[j]);
      }
    }
    sort(pp.begin(), pp.end());
    int s = pp.size() - 1;
    for (int i = 0; i <= s; i++) {
      while (pp[i] + pp[s] > m)
        s--;
      if (ans < pp[i] + pp[s])
        ans = pp[i] + pp[s];
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n, m, a, ans = 0;
    vector<int> p, pp;
    scanf("%d%d", &n, &m);
    p.push_back(0);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a);
      p.push_back(a);
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        pp.push_back(p[i] + p[j]);
      }
    }
    sort(pp.begin(), pp.end());
    int s = pp.size() - 1;
    for (int i = 0; i <= s; i++) {
      while (pp[i] + pp[s] > m)
        s--;
      if (ans < pp[i] + pp[s])
        ans = pp[i] + pp[s];
    }
    printf("%d\\n", ans);
  }
}