Problem p01268 (C++) — buggy submission, verdict: Memory Limit Exceeded

// {{{ include and macros
#define debug(x) cout << #x << ": " << x << endl
#define rep(i, a, b) for (int i = a; i < b; i++)
#include <algorithm>
#include <deque>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// }}}

// {{{ io
void newline() { cout << endl; }
template <typename T> void display(T input) { cout << input; }
template <typename T> void put(T input) {
  display(input);
  newline();
}
// }}}

const int n_max = 100000;
const int prime_number_max = 200270;
const int p_max = 100;
vector<int> prime_numbers;
int n, p;

void reserve_prime_numbers() {
  const int sievesize = prime_number_max;
  std::vector<int> sieve(prime_number_max, 1);
  sieve.reserve(prime_number_max);
  for (int i = 2; i * i < sievesize; i++)
    if (sieve[i])
      for (int j = i + i; j < sievesize; j += i)
        sieve[j] = 0;
  for (int j = 2; j < sievesize; j++)
    if (sieve[j])
      prime_numbers.push_back(j);
}

int get_index() {
  int i = 0;
  for (; prime_numbers[i] <= n; i++) {
  }
  return i;
}

int binary_search_index_rec(int s, int e) {
  int i = (s + e) / 2;
  if (s == e - 1) {
    return i + 1;
  }
  if (prime_numbers[i] == n) {
    return i;
  }
  if (prime_numbers[i] > n)
    return binary_search_index_rec(s, e - (e - s) / 2);
  if (prime_numbers[i] < n)
    return binary_search_index_rec(s + (e - s) / 2, e);
}

int binary_search_index() {
  return binary_search_index_rec(0, prime_numbers.size() - 1);
}

int solve() {
  vector<int> prime_numbers_sum;
  int index = get_index();
  // int index = binary_search_index();
  for (int i = 0 + index; i < 25 + index; i++) {
    for (int l = i; l < 25 + index; l++) {
      int sum = prime_numbers[i] + prime_numbers[l];
      prime_numbers_sum.push_back(sum);
    }
  }
  sort(prime_numbers_sum.begin(), prime_numbers_sum.end());
  return prime_numbers_sum[p - 1];
}

int main() {
  ios::sync_with_stdio(false);
  prime_numbers.reserve(prime_number_max);
  reserve_prime_numbers();
  while (cin >> n >> p && n != -1 && p != -1)
    put(solve());
}

Error: MLE

Fixed submission:

// {{{ include and macros
#define debug(x) cout << #x << ": " << x << endl
#define rep(i, a, b) for (int i = a; i < b; i++)
#include <algorithm>
#include <deque>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// }}}

// {{{ io
void newline() { cout << endl; }
template <typename T> void display(T input) { cout << input; }
template <typename T> void put(T input) {
  display(input);
  newline();
}
// }}}

const int n_max = 100000;
const int prime_number_max = 200270;
const int p_max = 100;
vector<int> prime_numbers;
int n, p;

// テ」ツつィテ」ツδゥテ」ツδ暗」ツつケテ」ツδ?」ツδ催」ツつケテ」ツ?ョテァツッツゥ
void reserve_prime_numbers() {
  // テヲツ閉エテヲツ閉ーテ」ツ?ョテ」ツ?オテ」ツつ凝」ツ??」ツつ津、ツスツ愿ヲツ按静」ツ?凖」ツつ凝」ツ??・ツ按敕ヲツ慊淌ィツィツュテ・ツョツ?
  const int sievesize = prime_number_max;
  std::vector<int> sieve(prime_number_max, 1);

  // 1
  // テ」ツδ禿」ツδε」ツδ暗」ツ?ョテ、ツスツ催ァツスツョテ」ツつ津ヲツ、ツ愿ァツエツ「テ」ツ?凖」ツつ?
  for (int i = 2; i * i < sievesize; i++)
    if (sieve[i])
      for (int j = i + i; j < sievesize; j += i)
        sieve[j] = 0;

  // テヲツ慊?・ツセツ古」ツ?ォテ」ツ??ィツィツュテ・ツョツ堙」ツ?陛」ツつ古」ツつ凝・ツ?、テ」ツつ津・ツ?コテ・ツ環崚」ツ?凖」ツつ?
  for (int j = 2; j < sievesize; j++)
    if (sieve[j])
      prime_numbers.push_back(j);
}

int solve() {
  vector<int> prime_numbers_sum;
  for (int i = 0; i < 10000; i++) {
    for (int l = i; l < 10000; l++) {
      int sum = prime_numbers[i] + prime_numbers[l];
      if (sum >= n) {
        prime_numbers_sum.push_back(sum);
      }
    }
  }
  sort(prime_numbers_sum.begin(), prime_numbers_sum.end());
  return prime_numbers_sum[p - 1];
}

int main() {
  prime_numbers.reserve(prime_number_max);
  reserve_prime_numbers();
  cin >> n >> p;
  put(solve());
}