Problem p00452 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp std::make_pair

// Loop
#define FOR(i, a, b) for (auto i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (auto i = (a); i >= (b); --i)

#define REP(i, a) for (long i = 0; i < (a); ++i)
#define RREP(i, a) for (long i = (a); i >= 0; --i)

#define EACH(i, a) for (auto(i) = (a).begin(), _END = (a).end(); i != _END; ++i)
#define REACH(i, a)                                                            \\
  for (auto(i) = (a).rbegin(), _END = (a).rend(); i != _END; ++i)

// Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a, x) ((a).find(x) != (a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a)                                                              \\
  std::sort((a).begin(), a.end()),                                             \\
      a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

// Setting
#define OPT                                                                    \\
  std::cin.tie(0);                                                             \\
  std::ios::sync_with_stdio(false);

// debug message
bool debug = true;
#define MSG(s)                                                                 \\
  if (debug) {                                                                 \\
    std::cout << s << std::endl;                                               \\
  }
#define DEBUG(x)                                                               \\
  if (debug) {                                                                 \\
    std::cout << "debug(" << #x << "): " << x << std::endl;                    \\
  }

// alias
typedef long long LL;
typedef std::vector<char> VC;
typedef std::vector<int> VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;

typedef std::vector<VC> VC2;
typedef std::vector<VI> VI2;
typedef std::vector<VL> VL2;
typedef std::vector<VLL> VLL2;

typedef std::pair<int, int> PII;

int N, M;
VI P(1000);

int main() {
  while (std::cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    REP(i, N) { std::cin >> P[i]; }

    std::vector<int> sum;
    sum.push_back(0);
    sum.insert(sum.end(), P.begin(), P.end());
    REP(i, N) {
      REP(j, N) {
        if (P[i] + P[j] <= M) {
          sum.pb(P[i] + P[j]);
        }
      }
    }

    UNIQUE(sum);

    int res = 0;
    for (auto x : sum) {
      auto py = std::upper_bound(ALL(sum), M - x) - 1;
      res = std::max(res, x + *py);
    }
    std::cout << res << std::endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

// Shrotening
#define fst first
#define snd second
#define pb push_back
#define mp std::make_pair

// Loop
#define FOR(i, a, b) for (auto i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (auto i = (a); i >= (b); --i)

#define REP(i, a) for (long i = 0; i < (a); ++i)
#define RREP(i, a) for (long i = (a); i >= 0; --i)

#define EACH(i, a) for (auto(i) = (a).begin(), _END = (a).end(); i != _END; ++i)
#define REACH(i, a)                                                            \\
  for (auto(i) = (a).rbegin(), _END = (a).rend(); i != _END; ++i)

// Algorithm
#define ALL(a) (a).begin(), a.end()
#define RALL(a) (a).rbegin(), a.rend()
#define EXIST(a, x) ((a).find(x) != (a).end())
#define SORT(a) std::sort((a).begin(), (a).end())
#define UNIQUE(a)                                                              \\
  std::sort((a).begin(), a.end()),                                             \\
      a.erase(std::unique((a).begin(), a.end()), a.end());
#define SUM(a) std::accumulate((a).begin(), (a).end(), 0);

// Setting
#define OPT                                                                    \\
  std::cin.tie(0);                                                             \\
  std::ios::sync_with_stdio(false);

// debug message
bool debug = true;
#define MSG(s)                                                                 \\
  if (debug) {                                                                 \\
    std::cout << s << std::endl;                                               \\
  }
#define DEBUG(x)                                                               \\
  if (debug) {                                                                 \\
    std::cout << "debug(" << #x << "): " << x << std::endl;                    \\
  }

// alias
typedef long long LL;
typedef std::vector<char> VC;
typedef std::vector<int> VI;
typedef std::vector<long> VL;
typedef std::vector<long long> VLL;

typedef std::vector<VC> VC2;
typedef std::vector<VI> VI2;
typedef std::vector<VL> VL2;
typedef std::vector<VLL> VLL2;

typedef std::pair<int, int> PII;

int N, M;
VI P(1000);
VI sum(1000005);

int main() {
  while (std::cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    REP(i, N) { std::cin >> P[i]; }

    int cnt = 0;
    for (int i = 0; i <= N; ++i) {
      for (int j = 0; j <= N; ++j) {
        int s = 0;
        s += (i < N ? P[i] : 0);
        s += (j < N ? P[j] : 0);
        if (s > M)
          continue;
        sum[cnt++] = s;
      }
    }
    std::sort(sum.begin(), sum.begin() + cnt);

    int ans = 0;
    for (int i = 0; i < cnt; ++i) {
      int v2 =
          *(std::upper_bound(sum.begin(), sum.begin() + cnt, M - sum[i]) - 1);
      ans = std::max(ans, sum[i] + v2);
    }
    std::cout << ans << std::endl;
  }
}