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

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()
#define fcout cout << setprecision(10)

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

template <typename T> void debug(vector<T> &vec) {
  for (auto x : vec) {
    cout << x << " ";
  }
  cout << endl;
  return;
}

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> int getFormer(const vector<T> &vec, T x) {
  return upper_bound(vec.begin(), vec.end(), x) - vec.begin() - 1;
}

template <typename T> int getLatter(const vector<T> &vec, T x) {
  return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
}

vector<long long> era(int N) {
  vector<bool> isPrime(N, true);
  vector<long long> res;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < sqrt(N) + 1; i++) {
    if (isPrime[i] == true) {
      for (int j = i + i; j < N + 1; j += i) {
        isPrime[j] = false;
      }
    }
  }

  for (int i = 2; i < N + 1; i++) {
    if (isPrime[i] == true)
      res.push_back(i);
  }

  return res;
}

vector<long long> primes;

int solve(int N, int P) {
  auto s = upper_bound(ALL(primes), N) - primes.begin();
  vector<long long> sums;
  for (int i = s; i < s + P; i++) {
    for (int j = i; j < s + P; j++) {
      sums.push_back(primes[i] + primes[j]);
    }
  }

  sort(ALL(sums));
  cout << sums[P - 1] << endl;
}

int main() {
  int N, P;
  primes = era(3 * 1e5);
  while (cin >> N >> P && N != -1) {
    solve(N, P);
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()
#define fcout cout << setprecision(10)

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

template <typename T> void debug(vector<T> &vec) {
  for (auto x : vec) {
    cout << x << " ";
  }
  cout << endl;
  return;
}

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> int getFormer(const vector<T> &vec, T x) {
  return upper_bound(vec.begin(), vec.end(), x) - vec.begin() - 1;
}

template <typename T> int getLatter(const vector<T> &vec, T x) {
  return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
}

vector<long long> era(int N) {
  vector<bool> isPrime(N, true);
  vector<long long> res;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < sqrt(N) + 1; i++) {
    if (isPrime[i] == true) {
      for (int j = i + i; j < N + 1; j += i) {
        isPrime[j] = false;
      }
    }
  }

  for (int i = 2; i < N + 1; i++) {
    if (isPrime[i] == true)
      res.push_back(i);
  }

  return res;
}

vector<long long> primes;

int solve(int N, int P) {
  auto s = lower_bound(ALL(primes), N) - primes.begin();
  vector<long long> sums;
  for (int i = s; i < s + P; i++) {
    for (int j = i + 1; j < s + P; j++) {
      sums.push_back(primes[i] + primes[j]);
    }
  }

  sort(ALL(sums));
  cout << sums[P - 1] << endl;
}

int main() {
  int N, P;
  primes = era(3 * 1e5);
  while (cin >> N >> P && N != -1) {
    solve(N, P);
  }
  return 0;
}