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

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int cost[1002001];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, d[1001];
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int ans = 0;
    rep(i, 0, n) {
      cin >> d[i];
      if (ans < d[i] && d[i] <= m)
        ans = d[i];
    }
    d[n] = 0;
    int cou = 0;
    rep(i, 0, n + 1) {
      rep(j, 0, n + 1) {
        cost[cou] = d[i] + d[j];
        if (cost[cou] > ans && cost[cou] <= m)
          ans = cost[cou];
        cou++;
      }
    }
    sort(cost, cost + cou);
    int left = 0;
    int right = cou;
    rep(i, 0, cou) {
      while ((cost[i] + cost[right - 1]) > m)
        right--;
      int a = cost[i] + cost[right - 1];
      if (right < i)
        break;
      // cout << right << " " << a << endl;
      if (a <= m)
        ans = max(a, ans);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
vi cost;
int d[1010];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  while (1) {
    cost.clear();
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    rep(i, 0, n) { cin >> d[i]; }
    d[n] = 0;
    int cou = 0;
    rep(i, 0, n + 1) {
      rep(j, 0, n + 1) {
        int u = d[i] + d[j];
        cost.pb(u);
      }
    }
    sort(ALL(cost));
    int ans = 0;
    for (int v : cost) {
      // cout << v << endl;
      int an = k - v;
      if (an < 0)
        break;
      int smal = 0, larg = cost.size() - 1;
      while (larg > smal) {
        int cen = (smal + larg) / 2;
        // cout << cen << endl;
        if (cost[cen] > an) {
          larg = cen;
        } else if (cost[cen] < an) {
          smal = cen + 1;
        } else {
          ans = k;
          break;
        }
        if (larg <= smal) {
          int a = cost[larg - 1];
          if (ans < (v + a))
            ans = v + a;
          break;
        }
      }
      if (ans < (v + cost[larg - 1]))
        ans = (v + cost[larg]);
    }
    cout << ans << endl;
  }
  return 0;
}