Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.dart";
string NAME;
string itos(int n) {
  stringstream ss;
  ss << n;

  return ss.str();
}

/*
        解法３ O(n^2log(n))

        3本の矢を投げて、残りの矢を２分探索する
*/
int n, m, k;
int p[1005];
int s[1005];

bool query(int c, int sum) { return (sum + s[c] <= m); }

int solve(int sum) {
  int L = 0;
  int R = k;
  while (R - L > 1) {
    int C = (L + R) / 2;
    (query(C, sum) ? L : R) = C;
  } // end while

  return sum + s[L];
}
int main() {
//	cut here before submit
#if DEBUG
  NAME = FILE_NAME;
  int CNT = 1;
  NAME += itos(CNT);
  while (freopen(NAME.c_str(), "r", stdin) != NULL) {
#endif
    while (scanf("%d %d", &n, &m), n, m) {
      memset(p, 0, sizeof(p));
      memset(s, 0, sizeof(s));
      for (int i = 1; i <= n; i++)
        scanf("%d", &p[i]);
      sort(p, p + n + 1);
      k = 0;
      for (int a = 0; a <= n; a++) {
        for (int b = 0; b <= n; b++) {
          if (p[a] + p[b] > m)
            continue;
          s[k++] = p[a] + p[b];
        } // end for
      }   // end for

      sort(s, s + k);

      int res = 0;
      rep(i, k) {
        int curr = solve(s[i]);
        res = max(res, curr);
      } // end rep

      printf("%d\n", res);
    } // end while
#if DEBUG
    CNT++; // cut here before submit
    NAME = FILE_NAME;
    NAME += itos(CNT);
  } // end loop; cut here before submit
#endif
  return 0;
}

Fixed submission:
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.dart";
string NAME;
string itos(int n) {
  stringstream ss;
  ss << n;

  return ss.str();
}

/*
        解法３ O(n^2log(n))

        3本の矢を投げて、残りの矢を２分探索する
*/
int n, m, k;
int p[1005];
int s[1005 * 1005];

bool query(int c, int sum) { return (sum + s[c] <= m); }

int solve(int sum) {
  int L = 0;
  int R = k;
  while (R - L > 1) {
    int C = (L + R) / 2;
    (query(C, sum) ? L : R) = C;
  } // end while

  return sum + s[L];
}
int main() {
//	cut here before submit
#if DEBUG
  NAME = FILE_NAME;
  int CNT = 1;
  NAME += itos(CNT);
  while (freopen(NAME.c_str(), "r", stdin) != NULL) {
#endif
    while (scanf("%d %d", &n, &m), n, m) {
      memset(p, 0, sizeof(p));
      memset(s, 0, sizeof(s));
      for (int i = 1; i <= n; i++)
        scanf("%d", &p[i]);
      sort(p, p + n + 1);
      k = 0;
      for (int a = 0; a <= n; a++) {
        for (int b = 0; b <= n; b++) {
          if (p[a] + p[b] > m)
            continue;
          s[k++] = p[a] + p[b];
        } // end for
      }   // end for

      sort(s, s + k);

      int res = 0;
      rep(i, k) {
        int curr = solve(s[i]);
        res = max(res, curr);
      } // end rep

      printf("%d\n", res);
    } // end while
#if DEBUG
    CNT++; // cut here before submit
    NAME = FILE_NAME;
    NAME += itos(CNT);
  } // end loop; cut here before submit
#endif
  return 0;
}