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

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

using namespace std;

typedef pair<int, int> P;
typedef long long ll;
const int MAX = 2;
const ll INF = (1LL << 60);

int N, M;
vector<ll> V[MAX];
ll ans;
void init() {
  for (int i = 0; i < MAX; i++)
    V[i].clear();
  ans = 0;
}

void solve() {

  sort(V[0].begin(), V[0].end());

  for (int i = 0; i < V[0].size(); i++)
    for (int j = 0; j < V[0].size(); j++)
      if (V[0][i] + V[0][j] <= M)
        V[1].push_back(V[0][i] + V[0][j]);

  V[1].push_back(INF);
  sort(V[1].begin(), V[1].end());

  vector<ll>::iterator ite1;

  /*
 for(int i = 0; i < V[0].size(); i++)
   for(ite1 = V[1].end(); ite1 != V[1].end(); ite1 =
 upper_bound(V[1].begin(),V[1].end(),*ite1)) if(*ite1 + V[0][i] <= M) ans =
 max(ans,*ite1+V[0][i]); else break;

 for(ite1 = V[1].begin(); ite1 != V[1].end(); ite1 =
 upper_bound(V[1].begin(),V[1].end(),*ite1)) for(ite2 = V[1].begin(); ite2 !=
 V[1].end(); ite2 = upper_bound(V[1].begin(),V[1].end(),*ite2)) if(*ite1 + *ite2
 <= M) ans = max(ans,*ite1+*ite2); else break;
  */

  for (int i = 0; i < V[0].size(); i++) {
    vector<ll>::iterator ite =
        lower_bound(V[1].begin(), V[1].end(), M - V[0][i]);

    while (1) {
      if (V[0][i] + *ite <= M) {
        ans = max(ans, V[0][i] + *ite);
        break;
      } else if (ite == V[1].begin())
        break;
      else
        ite--;
    }
  }

  for (ite1 = V[1].begin(); ite1 != V[1].end();
       ite1 = upper_bound(V[1].begin(), V[1].end(), *ite1)) {
    vector<ll>::iterator ite = lower_bound(V[1].begin(), V[1].end(), M - *ite1);
    while (1) {
      if (*ite1 + *ite <= M) {
        ans = max(ans, *ite1 + *ite);
        break;
      } else if (ite == V[1].begin())
        break;
      else
        ite--;
    }
  }

  for (int i = 0; i < MAX; i++) {
    vector<ll>::iterator ite;
    ite = upper_bound(V[i].begin(), V[i].end(), M);
    /*while(1){
      if(*ite <= M) {
        ans = max(ans,*ite);
        break;
      }else if(ite == V[i].begin()) break;
      else ite--;
      }*/
    ans = max(ans, *(ite - 1));
  }
  cout << ans << endl;
}

int main() {
  while (cin >> N >> M && N + M) {
    init();
    for (int i = 0; i < N; i++) {
      ll in;
      cin >> in;
      V[0].push_back(in);
    }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

typedef pair<int, int> P;
typedef long long ll;
const int MAX = 2;
const ll INF = (1LL << 60);

int N, M;
vector<ll> V[MAX];
ll ans;
void init() {
  for (int i = 0; i < MAX; i++)
    V[i].clear();
  ans = 0;
}

void solve() {

  sort(V[0].begin(), V[0].end());

  for (int i = 0; i < V[0].size(); i++)
    for (int j = 0; j < V[0].size(); j++)
      if (V[0][i] + V[0][j] < M)
        V[1].push_back(V[0][i] + V[0][j]);

  V[1].push_back(INF);
  sort(V[1].begin(), V[1].end());

  vector<ll>::iterator ite1, ite2;

  for (int i = 0; i < V[0].size(); i++)
    for (ite1 = V[1].begin(); ite1 != V[1].end();
         ite1 = upper_bound(V[1].begin(), V[1].end(), *ite1))
      if (*ite1 + V[0][i] <= M)
        ans = max(ans, *ite1 + V[0][i]);
      else
        break;

  for (ite1 = V[1].begin(); ite1 != V[1].end();
       ite1 = upper_bound(V[1].begin(), V[1].end(), *ite1))
    for (ite2 = V[1].begin(); ite2 != V[1].end();
         ite2 = upper_bound(V[1].begin(), V[1].end(), *ite2))
      if (*ite1 + *ite2 <= M)
        ans = max(ans, *ite1 + *ite2);
      else
        break;

  for (int i = 0; i < MAX; i++) {
    vector<ll>::iterator ite;
    ite = upper_bound(V[i].begin(), V[i].end(), M);
    ans = max(ans, *(ite - 1));
  }
  cout << ans << endl;
}

int main() {
  while (cin >> N >> M && N + M) {
    init();
    for (int i = 0; i < N; i++) {
      ll in;
      cin >> in;
      V[0].push_back(in);
    }
    solve();
  }
  return 0;
}