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

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  scanf("%d", &n);

  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &v[i], &w[i]);
  }

  int q;
  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int vertex, W;
    scanf("%d %d", &vertex, &W);

    int m = 1;
    vector<int> vq, wq;
    vq.push_back(v[vertex - 1]);
    wq.push_back(w[vertex - 1]);
    while (vertex != 1) {
      vertex /= 2;
      // cout << vertex << " ";
      vq.push_back(v[vertex - 1]);
      wq.push_back(w[vertex - 1]);
      m += 1;
    }
    // cout << endl;

    vector<pair<int, int>> a = {{0, 0}}, a1, a2;
    for (int i = 0; i < m / 2; i++) {
      a1 = a;
      a2 = a;
      for (int j = 0; j < a.size(); j++) {
        a2[j].first += wq[i];
        a2[j].second += vq[i];
      }
      a = {};
      int j1 = 0, j2 = 0;
      while (j2 < a2.size()) {
        if (j1 == a1.size()) {
          if (a2[j2].first > W)
            break;
          a.push_back(a2[j2]);
          j2 += 1;
        } else if (a1[j1] < a2[j2]) {
          a.push_back(a1[j1]);
          j1 += 1;
        } else {
          a.push_back(a2[j2]);
          j2 += 1;
        }
      }
    }
    // sort(a.begin(),a.end());

    vector<pair<int, int>> b = {{0, 0}}, b1, b2;
    for (int i = m / 2; i < m; i++) {
      b1 = b;
      b2 = b;
      for (int j = 0; j < b.size(); j++) {
        b2[j].first += wq[i];
        b2[j].second += vq[i];
      }
      b = {};
      int j1 = 0, j2 = 0;
      while (j2 < b2.size()) {
        if (j1 == b1.size()) {
          if (b2[j2].first > W)
            break;
          b.push_back(b2[j2]);
          j2 += 1;
        } else if (b1[j1] < b2[j2]) {
          b.push_back(b1[j1]);
          j1 += 1;
        } else {
          b.push_back(b2[j2]);
          j2 += 1;
        }
      }
    }

    vector<pair<int, int>> c, d;
    int cutoff = 0;
    for (int i = 0; i < a.size(); i++) {
      if (i > 0 && a[i].second <= cutoff)
        continue;
      c.push_back(a[i]);
      cutoff = a[i].second;
    }
    cutoff = 0;
    for (int i = 0; i < b.size(); i++) {
      if (i > 0 && b[i].second <= cutoff)
        continue;
      d.push_back(b[i]);
      cutoff = b[i].second;
    }

    /*
    cout << endl;
    for(auto aa : c) cout << aa.first << " " << aa.second << endl;
    cout << endl;
    for(auto bb : d) cout << bb.first << " " << bb.second << endl;
    cout << endl;
    */

    int ans = 0;
    for (int i = 0; i < c.size(); i++) {
      int test = c[i].second;
      int itr =
          lower_bound(d.begin(), d.end(), make_pair(W - c[i].first + 1, 0)) -
          d.begin();
      if (itr >= 1)
        test += d[itr - 1].second;
      ans = max(ans, test);
      // cout << i << " " << itr << " " << test << endl;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;

  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int vertex, W;
    cin >> vertex >> W;

    int m = 1;
    vector<int> vq, wq;
    vq.push_back(v[vertex - 1]);
    wq.push_back(w[vertex - 1]);
    while (vertex != 1) {
      vertex /= 2;
      // cout << vertex << " ";
      vq.push_back(v[vertex - 1]);
      wq.push_back(w[vertex - 1]);
      m += 1;
    }
    // cout << endl;

    vector<pair<int, int>> a = {{0, 0}}, a1, a2;
    for (int i = 0; i < m / 2; i++) {
      a1 = a;
      a2 = a;
      for (int j = 0; j < a.size(); j++) {
        a2[j].first += wq[i];
        a2[j].second += vq[i];
      }
      a = {};
      int j1 = 0, j2 = 0;
      while (j2 < a2.size()) {
        if (j1 == a1.size()) {
          if (a2[j2].first > W)
            break;
          a.push_back(a2[j2]);
          j2 += 1;
        } else if (a1[j1] < a2[j2]) {
          a.push_back(a1[j1]);
          j1 += 1;
        } else {
          a.push_back(a2[j2]);
          j2 += 1;
        }
      }
    }
    // sort(a.begin(),a.end());

    vector<pair<int, int>> b = {{0, 0}}, b1, b2;
    for (int i = m / 2; i < m; i++) {
      b1 = b;
      b2 = b;
      for (int j = 0; j < b.size(); j++) {
        b2[j].first += wq[i];
        b2[j].second += vq[i];
      }
      b = {};
      int j1 = 0, j2 = 0;
      while (j2 < b2.size()) {
        if (j1 == b1.size()) {
          if (b2[j2].first > W)
            break;
          b.push_back(b2[j2]);
          j2 += 1;
        } else if (b1[j1] < b2[j2]) {
          b.push_back(b1[j1]);
          j1 += 1;
        } else {
          b.push_back(b2[j2]);
          j2 += 1;
        }
      }
    }

    vector<pair<int, int>> c, d;
    int cutoff = 0;
    for (int i = 0; i < a.size(); i++) {
      if (i > 0 && a[i].second <= cutoff)
        continue;
      c.push_back(a[i]);
      cutoff = a[i].second;
    }
    cutoff = 0;
    for (int i = 0; i < b.size(); i++) {
      if (i > 0 && b[i].second <= cutoff)
        continue;
      d.push_back(b[i]);
      cutoff = b[i].second;
    }

    /*
    cout << endl;
    for(auto aa : c) cout << aa.first << " " << aa.second << endl;
    cout << endl;
    for(auto bb : d) cout << bb.first << " " << bb.second << endl;
    cout << endl;
    */

    int ans = 0;
    for (int i = 0; i < c.size(); i++) {
      int test = c[i].second;
      int itr =
          lower_bound(d.begin(), d.end(), make_pair(W - c[i].first + 1, 0)) -
          d.begin();
      if (itr >= 1)
        test += d[itr - 1].second;
      ans = max(ans, test);
      // cout << i << " " << itr << " " << test << endl;
    }
    printf("%d\\n", ans);
  }
  return 0;
}