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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

bool isPrime[200000];
vector<int> primes;

int main() {
  rep(i, 200000) isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  REP(i, 2, 200000) {
    if (!isPrime[i])
      continue;
    for (int j = i + i; j < 200000; j += i)
      isPrime[j] = false;
    primes.pb(i);
  }
  // cout<<primes.size()<<endl;
  while (1) {
    int n = in.nextInt(), p = in.nextInt();
    if (n == -1 && p == -1)
      break;

    vi v;
    int idx = upper_bound(all(primes), n) - primes.begin();
    for (int i = idx; i < primes.size(); i++) {
      for (int j = i; j < primes.size(); j++) {
        if (v.size() < p + 2)
          v.pb(primes[i] + primes[j]);
        else {
          sort(all(v));
          if (v.back() > primes[i] + primes[j]) {
            v.pop_back();
            v.pb(primes[i] + primes[j]);
          } else {
            break;
          }
        }
      }
    }

    sort(all(v));
    printf("%d\\n", v[p - 1]);

    // rep(i, v.size()) cout<<" "<<v[i];
    // cout<<endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

bool isPrime[200000];
vector<int> primes;

int main() {
  rep(i, 200000) isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  REP(i, 2, 200000) {
    if (!isPrime[i])
      continue;
    for (int j = i + i; j < 200000; j += i)
      isPrime[j] = false;
    primes.pb(i);
  }
  // cout<<primes.size()<<endl;
  while (1) {
    int n = in.nextInt(), p = in.nextInt();
    if (n == -1 && p == -1)
      break;

    vi v;
    int idx = upper_bound(all(primes), n) - primes.begin();
    for (int i = idx; i < primes.size(); i++) {
      for (int j = idx; j < primes.size(); j++) {
        if (v.size() < p + 1)
          v.pb(primes[i] + primes[j]);
        else {
          sort(all(v));
          if (v.back() > primes[i] + primes[j]) {
            v.pop_back();
            v.pb(primes[i] + primes[j]);
          }
        }
      }
    }

    sort(all(v));
    printf("%d\\n", v[p]);
  }
}