Problem p00452 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int v1[1001];
int v2[1000000];

int main() {
  int n, m;
  while (n = getInt(), m = getInt(), n + m) {
    int cnt = 0;
    REP(i, n) v1[i] = getInt();
    v1[n++] = 0;
    REP(i, n) rep(j, i, n) {
      if (v1[i] + v1[j] <= m)
        v2[cnt++] = v1[i] + v1[j];
    }
    sort(v2, v2 + cnt);

    int ans = 0;
    REP(i, cnt) {
      int a = v2[i];
      int b = m - a;
      int *it = lower_bound(v2, v2 + cnt, b);
      if (it != v2)
        --it;
      ans = max(ans, a + *it);
    }

    print(ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int main() {
  int n, m;
  while (n = getInt(), m = getInt(), n + m) {
    set<int> s;
    s.insert(0);
    REP(i, n) s.insert(getInt());
    set<int> s2;
    FOR(it, s) {
      for (set<int>::iterator it2 = it; it2 != s.end(); ++it2) {
        int sum = *it + *it2;
        if (sum <= m)
          s2.insert(sum);
      }
    }
    int ans = 0;
    FOR(it, s2) {
      int a = *it;
      int b = m - a;
      set<int>::iterator it2 = lower_bound(ALL(s2), b);
      if (it2 != s2.begin())
        --it2;
      ans = max(ans, a + *it2);
    }

    print(ans);
  }
  return 0;
}