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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define sz size()
#define lgt length()
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define pb push_back
#define SORT(a) sort(ALL(a));
#define RSORT(a) sort(RALL(a));
#define mp make_pair
#define dump(x) cerr << #x << " = " << (x) << endl;
#define fir first
#define sec second

typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<string> vs;

int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
vs form(string a, char s) {
  vs v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(b);
      b = "";
    } else
      b += a[i];
  }
  v.pb(b);
  return v;
}
vi formint(string a, char s) {
  vi v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(toInt(b));
      b = "";
    } else
      b += a[i];
  }
  v.pb(toInt(b));
  return v;
}

const double EPS = 1e-10, PI = acos(-1.0);
const int INF = 2100000000;

int main(void) {
  int n, m, tmp;
  while (cin >> n >> m, n && m) {
    int res = 0;
    vi v, v1, v2;

    v.pb(0);
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      v.pb(tmp);
    }
    for (int i = 0; i < v.sz; i++) {
      for (int j = 0; j < v.sz; j++) {
        v1.pb(v[i] + v[j]);
        v2.pb(v[i] + v[j]);
      }
    }
    SORT(v1);
    RSORT(v2);

    for (int i = 0, j = 0; i < v1.sz && j < v2.sz;) {
      if (m < v1[i] + v2[j])
        j++;
      else {
        res = max(res, v1[i] + v2[j]);
        i++;
      }
    }

    cout << res << endl;
    v.clear();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define sz size()
#define lgt length()
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define pb push_back
#define SORT(a) sort(ALL(a));
#define RSORT(a) sort(RALL(a));
#define mp make_pair
#define dump(x) cerr << #x << " = " << (x) << endl;
#define fir first
#define sec second

typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<string> vs;

int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
vs form(string a, char s) {
  vs v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(b);
      b = "";
    } else
      b += a[i];
  }
  v.pb(b);
  return v;
}
vi formint(string a, char s) {
  vi v;
  string b;
  rep(i, a.lgt) {
    if (a[i] == s) {
      v.pb(toInt(b));
      b = "";
    } else
      b += a[i];
  }
  v.pb(toInt(b));
  return v;
}

const double EPS = 1e-10, PI = acos(-1.0);
const int INF = 2100000000;

vector<int> v, v1;

int main(void) {
  int n, m, tmp;
  while (cin >> n >> m, n && m) {
    int res = 0;

    v.pb(0);
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      v.pb(tmp);
    }
    for (int i = 0; i < v.sz; i++) {
      for (int j = 0; j < v.sz; j++) {
        v1.pb(v[i] + v[j]);
      }
    }
    SORT(v1);

    int i, j = v1.sz - 1;
    for (i = 0; i <= v1.sz; i++) {
      for (j = v1.sz - 1; m < v1[i] + v1[j] && 0 <= j; j--) {
      }
      //			cout << v1[i] << " " << v1[j] << "*" << j <<
      //endl;
      if (0 <= j)
        res = max(res, v1[i] + v1[j]);
    }

    cout << res << endl;
    v.clear();
    v1.clear();
  }

  return 0;
}