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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int n;
  ll m;
  while (scanf("%d%lld", &n, &m), n | m) {
    ll p[1024];
    for (int i = 0; i < n; ++i)
      scanf("%lld", p + i);
    p[n] = 0;
    vector<ll> two;
    for (int i = 0; i <= n; ++i)
      for (int j = i; j <= n; ++j)
        two.push_back(p[i] + p[j]);
    sort(all(two));

    ll res = 0;
    foreach (it, two) {
      int k = lower_bound(all(two), m - *it) - two.begin();
      --k;
      if (k >= 0)
        max_swap(res, *it + two[k]);
    }
    printf("%lld\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

template <class T> void max_swap(T &a, const T b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int n;
  ll m;
  while (scanf("%d%I64d", &n, &m), n | m) {
    ll p[1024];
    for (int i = 0; i < n; ++i)
      scanf("%I64d", p + i);
    p[n] = 0;
    vector<ll> two;
    for (int i = 0; i <= n; ++i)
      for (int j = i; j <= n; ++j)
        two.push_back(p[i] + p[j]);
    sort(all(two));

    ll res = 0;
    foreach (it, two) {
      int k = lower_bound(all(two), m - *it) - two.begin();
      --k;
      if (k >= 0)
        max_swap(res, *it + two[k]);
    }
    printf("%I64d\\n", res);
  }
}