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

#pragma GCC optimize("O2")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int V[300000], W[300000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  rep1(i, N) { cin >> V[i] >> W[i]; }

  int Q;
  cin >> Q;

  rep(i, Q) {
    int P, L;
    cin >> P >> L;

    int k = 0;
    int v[20], w[20];
    while (P) {
      v[k] = V[P];
      w[k] = W[P];
      P /= 2;
      k++;
    }

    pair<int, int> A[1000] = {}, B[1000] = {};
    int a = 1, b = 1;
    rep(j, k / 2) {
      int tmp = a;
      pair<int, int> A2[1000];
      rep(q, tmp) { A[a++] = mp(A[q].first + w[j], A[q].second + v[j]); }
      int t1 = 0;
      int t2 = tmp;
      int t3 = 0;
      while (t1 < tmp || t2 < a) {
        if (t1 == tmp)
          A2[t3++] = A[t2++];
        else if (t2 == a)
          A2[t3++] = A[t1++];
        else if (A[t1] > A[t2])
          A2[t3++] = A[t2++];
        else
          A2[t3++] = A[t1++];
      }
      rep(q, a) A[q] = A2[q];
    }

    for (int j = k / 2; j < k; j++) {
      int tmp = b;
      pair<int, int> A2[1000];
      rep(q, tmp) { B[b++] = mp(B[q].first + w[j], B[q].second + v[j]); }
      int t1 = 0;
      int t2 = tmp;
      int t3 = 0;
      while (t1 < tmp || t2 < b) {
        if (t1 == tmp)
          A2[t3++] = B[t2++];
        else if (t2 == b)
          A2[t3++] = B[t1++];
        else if (B[t1] > B[t2])
          A2[t3++] = B[t2++];
        else
          A2[t3++] = B[t1++];
      }
      rep(q, b) B[q] = A2[q];
    }

    int saidai = 0;
    rep(j, a) {
      chmax(saidai, A[j].second);
      A[j].second = saidai;
    }
    saidai = 0;
    rep(j, b) {
      chmax(saidai, B[j].second);
      B[j].second = saidai;
    }
    reverse(B, B + b);

    int p2 = 0;
    int kotae = 0;
    rep(j, a) {
      while (p2 < b && A[j].first + B[p2].first > L) {

        p2++;
      }

      if (p2 < b) {
        chmax(kotae, A[j].second + B[p2].second);
      }
    }

    co(kotae);
  }

  Would you please return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O2")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int V[300000], W[300000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  rep1(i, N) { cin >> V[i] >> W[i]; }
  int Q;
  cin >> Q;

  rep(i, Q) {
    int P, L;
    cin >> P >> L;

    int k = 0;
    int v[20], w[20];
    while (P) {
      v[k] = V[P];
      w[k] = W[P];
      P /= 2;
      k++;
    }

    pair<int, int> A[1000] = {}, B[1000] = {};
    int a = 1, b = 1;
    rep(j, k / 2) {
      int tmp = a;
      rep(q, tmp) { A[a++] = mp(A[q].first + w[j], A[q].second + v[j]); }
    }

    for (int j = k / 2; j < k; j++) {
      int tmp = b;
      rep(q, tmp) { B[b++] = mp(B[q].first + w[j], B[q].second + v[j]); }
    }

    sort(A, A + a);
    sort(B, B + b);
    int saidai = 0;
    rep(j, a) {
      chmax(saidai, A[j].second);
      A[j].second = saidai;
    }
    saidai = 0;
    rep(j, b) {
      chmax(saidai, B[j].second);
      B[j].second = saidai;
    }
    reverse(B, B + b);

    int p2 = 0;
    int kotae = 0;
    rep(j, a) {
      while (p2 < b && A[j].first + B[p2].first > L) {

        p2++;
      }

      if (p2 < b) {
        chmax(kotae, A[j].second + B[p2].second);
      }
    }

    co(kotae);
  }

  Would you please return 0;
}