Problem p00452 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

// #define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \
  if (DEBUG) {                                                                 \
    cerr << #o << " " << o << endl;                                            \
  }
#define dumpc(o)                                                               \
  if (DEBUG) {                                                                 \
    cerr << #o;                                                                \
    for (auto &e : (o))                                                        \
      cerr << " " << e;                                                        \
    cerr << endl;                                                              \
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
const bool DEBUG = false;

signed main() {
  int P[1010] = {};
  static int a[1000010] = {}; // 1000010
  for (int N, M; cin >> N >> M && N;) {
    memset(a, 0x3f, sizeof(a));
    rep(i, 0, N) cin >> P[i];
    P[N] = 0;
    N++;
    rep(i, 0, N) rep(j, 0, N) { a[i * N + j] = P[i] + P[j]; }
    dumpc(a);
    sort(a, a + N * N);
    dumpc(a);
    int ans = 0;
    rep(i, 0, N * N) {
      if (a[i] > M)
        break;
      int add = a[upper_bound(a, a + N * N, M - a[i]) - a - 1];
      ans = max(ans, a[i] + add);
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

// #define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \
  if (DEBUG) {                                                                 \
    cerr << #o << " " << o << endl;                                            \
  }
#define dumpc(o)                                                               \
  if (DEBUG) {                                                                 \
    cerr << #o;                                                                \
    for (auto &e : (o))                                                        \
      cerr << " " << e;                                                        \
    cerr << endl;                                                              \
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;
const bool DEBUG = false;

signed main() {
  int P[1010] = {};
  static int a[2000000] = {}; // 1000010
  for (int N, M; cin >> N >> M && N;) {
    memset(a, 0x3f, sizeof(a));
    rep(i, 0, N) cin >> P[i];
    P[N] = 0;
    N++;
    rep(i, 0, N) rep(j, 0, N) { a[i * N + j] = P[i] + P[j]; }
    dumpc(a);
    sort(a, a + N * N);
    dumpc(a);
    int ans = 0;
    rep(i, 0, N * N) {
      if (a[i] > M)
        break;
      int add = a[upper_bound(a, a + N * N, M - a[i]) - a - 1];
      ans = max(ans, a[i] + add);
    }
    cout << ans << endl;
  }
  return 0;
}