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

// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define P pair<int, int>
#define ll long long

ll n, m;
ll t[10000];
const int MA = 2000000;
ll s1[MA];
ll s2[MA];

int main() {

  while (cin >> n >> m, n || m) {
    rep(i, MA) s1[i] = s2[i] = 0;
    rep(i, 10000) t[i] = 0;

    for (int i = 1; i <= n; i++)
      cin >> t[i];
    int g = 0;

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        s1[i * (n + 1) + j] = t[i] + t[j];
        g++;
      }
    }

    sort(s1, s1 + g);
    rep(i, g) s2[i] = s1[i];

    ll ans = 0;

    for (int i = 0; i < g; i++) {
      int now = s1[i];

      int l = 0;
      int r = g - 1;
      int mid = (l + r) / 2;
      ll x = s2[mid];

      for (int i = 0; i < 70; i++) {
        if (x + now == m)
          break;
        if (l > r)
          break;
        if (x + now > m) {
          r = mid;
          mid = (r + l) / 2;
          x = s2[mid];
        } else {
          l = mid;
          mid = (r + l) / 2;
          x = s2[mid];
        }
      }

      // cout<<" "<<x<<" "<<now<<" "<<x+now<<endl;
      if (x + now <= m)
        ans = max(ans, x + now);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2676345#1
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define P pair<int, int>
#define ll __int64

int n, m;
int t[10000];
const int MA = 1000000;
int s1[MA], s2[MA];

int ya[4];

int main() {

  while (cin >> n >> m, n || m) {
    rep(i, MA) s1[i] = s2[i] = 0;
    rep(i, 10000) t[i] = 0;
    for (int i = 1; i <= n; i++)
      cin >> t[i];
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        s1[i * (n + 1) + j] = t[i] + t[j];
        s2[i * (n + 1) + j] = t[i] + t[j];
      }
    }
    int g = (n + 1) * (n + 1);

    sort(s1, s1 + g);
    sort(s2, s2 + g);

    // rep(i,g)cout<<s1[i]<<" ";cout<<endl;

    int ans = 0;

    for (int i = 0; i < g; i++) {
      int now = s1[i];

      int l = 0;
      int r = g - 1;
      int mid = (l + r) / 2;
      int x = s2[mid];

      for (int i = 0; i < 60; i++) {
        if (x + now == m)
          break;
        if (x + now > m) {
          r = mid;
          mid = (r + l) / 2;
          x = s2[mid];
        } else {
          l = mid;
          mid = (r + l) / 2;
          x = s2[mid];
        }
      }

      // cout<<" "<<x<<" "<<now<<" "<<x+now<<endl;
      if (x + now <= m)
        ans = max(ans, x + now);
    }
    cout << ans << endl;
  }
  return 0;
}