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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll>;
using vs = vector<string>;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = (a); i < (n); i++)

#define LINF ((ll)1ll << 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout << setprecision(a) << fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}
void OUT(bool b) { cout << (b ? "YES" : "NO") << endl; }
void Out(bool b) { cout << (b ? "Yes" : "No") << endl; }
void out(bool b) { cout << (b ? "yes" : "no") << endl; }
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int clz(int x) { return 31 - __builtin_clz(x); }
int clz(ll x) { return 63 - __builtin_clzll(x); }
int ctz(int x) { return __builtin_ctz(x); }
int ctz(ll x) { return __builtin_ctzll(x); }

class compare {
public:
  bool operator()(tuple<ll, ll> a, tuple<ll, ll> b) {
    return (get<1>(a) > get<1>(b));
  }
};

void solve(ll n, ll m) {
  ll ans = 0;
  vl v(n);
  rep(i, n) { cin >> v[i]; }
  v.push_back(0);
  vl cop;
  rep(i, n + 1) {
    range(j, i, n + 1) { cop.push_back(v[i] + v[j]); }
  }
  sort(all(cop));
  for (auto x : cop) {
    ll y = m - x;
    if (y < 0)
      continue;
    auto itr = upper_bound(all(cop), y);
    if (*itr != 0)
      itr--;
    ans = max(*itr + x, ans);
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  while (cin >> n >> m, n) {
    solve(n, m);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll>;
using vs = vector<string>;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = (a); i < (n); i++)

#define LINF ((ll)1ll << 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout << setprecision(a) << fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}
void OUT(bool b) { cout << (b ? "YES" : "NO") << endl; }
void Out(bool b) { cout << (b ? "Yes" : "No") << endl; }
void out(bool b) { cout << (b ? "yes" : "no") << endl; }
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int clz(int x) { return 31 - __builtin_clz(x); }
int clz(ll x) { return 63 - __builtin_clzll(x); }
int ctz(int x) { return __builtin_ctz(x); }
int ctz(ll x) { return __builtin_ctzll(x); }

class compare {
public:
  bool operator()(tuple<ll, ll> a, tuple<ll, ll> b) {
    return (get<1>(a) > get<1>(b));
  }
};

void solve(ll n, ll m) {
  ll ans = 0;
  vl v(n);
  rep(i, n) { cin >> v[i]; }
  v.push_back(0);
  set<ll> cop;
  rep(i, n + 1) {
    range(j, i, n + 1) { cop.insert(v[i] + v[j]); }
  }
  for (auto x : cop) {
    for (auto y : cop) {
      if (x + y <= m)
        ans = max(ans, x + y);
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, m;
  while (cin >> n >> m, n) {
    solve(n, m);
  }
}