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

#include <bits/stdc++.h>
using namespace std;
int sum[1001 * 1001] = {0};
int sum1[1001 * 1001] = {0};
int main(void) {
  int n, m;
  int i, j;
  int high, low, base, mid;
  int a[1001];
  int k, l;
  int MIN;
  int ans;
  int sgm;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    a[n] = 0;
    sort(a, a + n + 1);
    // for(i=0;i<=n;i++)	 cout<<a[i]<<endl;cout<<endl;
    int flg;
    MIN = INT_MAX;
    ans = 0;
    l = 0;
    for (i = 0; i <= n; i++) {
      for (j = 0; j <= n; j++) {
        sum[l] = a[i] + a[j];
        l++;
      }
    }
    sort(sum, sum + l);
    // for(i=0;i<l;i++)	cout<<i+1<<" "<<sum[i]<<endl;
    k = 0;
    for (i = 0; i < l; i++) {
      if (sum[i] != sum[i + 1]) {
        sum1[k] = sum[i];
        k++;
      }
    }
    // for(i=0;i<k;i++)	cout<<i+1<<" "<<sum1[i]<<endl;
    for (i = 0; i < k; i++) {
      sgm = sum1[i];
      if (sgm <= m) {
        base = m - sgm;
        high = k - 1;
        low = 0;
        flg = 0;
        while (low <= high) {
          mid = (high + low) / 2;
          if (base < sum1[mid]) {
            high = mid - 1;
          } else if (base > sum1[mid]) {
            low = mid + 1;
          } else {
            flg = 1;
            break;
          }
        }
        if (flg == 1) {
          sgm += base;
        } else {
          sgm += sum1[high];
        }
      }
      // cout<<"M="<<MIN<<" "<<"SA="<<m-sgm<<" "<<"SGM="<<sgm<<endl;
      if (MIN > m - sgm && m >= sgm) {
        MIN = m - sgm;
        ans = sgm;
        // cout<<"ans="<<ans<<endl;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  int i, j, k;
  int high, low, base, mid;
  int a[1001];
  int MIN;
  int ans;
  int a1, a2;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    a[n] = 0;
    sort(a, a + n + 1);
    int sum;
    int flg;
    MIN = INT_MAX;
    ans = 0;
    // for(i=0;i<=n;i++)	cout<<a[i]<<endl;
    for (i = 0; i <= n; i++) {
      sum = 0;
      sum += a[i];
      a1 = sum;
      for (j = 0; j <= n; j++) {
        sum = a1;
        sum += a[j];
        a2 = sum;
        for (k = 0; k <= n; k++) {
          sum = a2;
          sum += a[k];
          if (sum <= m) {
            base = m - sum;
            high = n;
            low = 0;
            flg = 0;
            while (low <= high) {
              mid = (high + low) / 2;
              if (base < a[mid]) {
                high = mid - 1;
              } else if (base > a[mid]) {
                low = mid + 1;
              } else {
                flg = 1;
                break;
              }
            }
            if (flg == 1) {
              sum += base;
            } else {
              sum += a[high];
            }
          }

          // cout<<"M="<<MIN<<" "<<"SA="<<m-sum<<" "<<"SUM="<<sum<<endl;
          if (MIN > m - sum && m >= sum) {
            MIN = m - sum;
            ans = sum;
            // cout<<"ans="<<ans<<endl;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}