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

#include <algorithm>
#include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip> //do setprecision
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define FORQ(i, b, e) for (int i = (b); i <= (e); ++i)
#define FORD(i, b, e) for (int i = (b)-1; i >= (e); --i)
#define REP(x, n) for (int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double
#define pii pair<int, int>
#define pll pair<LL, LL>
#define vi vector<int>
#define vl vector<LL>
#define vii vector<vi>
#define vll vector<vl>

const double pi = 3.14159265358979323846264;
const int mod = 1000000007;
const int nm = (1 << 18) + 1;
const int nm2 = (1 << 10) + 1;
const int lm = 100002;

inline pair<vi, int> f(int n) {
  vi lst;
  int ret;
  while (n >= nm2) {
    lst.PB(n);
    n /= 2;
  }
  return {lst, n};
}

int main() {
  int n, q;
  cin >> n;
  vl v(nm, 0);
  vl w(nm, 0);
  vll dp(nm2, vl(lm, 0));
  FOR(i, 0, n) { cin >> v[i] >> w[i]; }
  dp[1][w[0]] = v[0];
  FOR(j, 1, lm) { dp[1][j] = max(dp[1][j], dp[1][j - 1]); }
  FOR(i, 1, nm2 + 1) {
    if (i * 2 < nm2) {
      int j = i * 2;
      FOR(k, 0, lm) {
        dp[j][k] = max(dp[i][k], dp[j][k]);
        if (k + w[j - 1] < lm) {
          dp[j][k + w[j - 1]] = max(dp[j][k + w[j - 1]], dp[i][k] + v[j - 1]);
        }
      }
      FOR(jj, 1, lm) { dp[j][jj] = max(dp[j][jj], dp[j][jj - 1]); }
    }
    if (i * 2 + 1 < nm2) {
      int j = i * 2 + 1;
      FOR(k, 0, lm) {
        dp[j][k] = max(dp[i][k], dp[j][k]);
        if (k + w[j - 1] < lm) {
          dp[j][k + w[j - 1]] = max(dp[j][k + w[j - 1]], dp[i][k] + v[j - 1]);
        }
      }
      FOR(jj, 1, lm) { dp[j][jj] = max(dp[j][jj], dp[j][jj - 1]); }
    }
  }
  // cerr<<"test"<<endl;
  cin >> q;
  vi qv(q), qw(q);
  FOR(i, 0, q) {
    cin >> qv[i] >> qw[i];
    if (qv[i] < nm2) {
      // cerr<<"N"<<endl;
      cout << dp[qv[i]][qw[i]] << endl;
    } else {
      const auto pr = f(qv[i]);
      const vi lst = pr.ST;
      const int idx = pr.ND;
      const int ln = lst.size();
      // cerr<<ln<<" "<<idx<<endl;
      LL ans = 0;
      FOR(msk, 0, 1 << ln) {
        LL ww = 0, vv = 0;
        FOR(ii, 0, ln) {
          if (msk & (1 << ii)) {
            vv += v[lst[ii] - 1];
            ww += w[lst[ii] - 1];
          }
        }
        if (ww > qw[i])
          continue;
        const LL rest = qw[i] - ww;
        vv += dp[idx][rest];
        ans = max(ans, vv);
      }

      cout << ans << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip> //do setprecision
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define FORQ(i, b, e) for (int i = (b); i <= (e); ++i)
#define FORD(i, b, e) for (int i = (b)-1; i >= (e); --i)
#define REP(x, n) for (int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double
#define pii pair<int, int>
#define pll pair<LL, LL>
#define vi vector<int>
#define vl vector<LL>
#define vii vector<vi>
#define vll vector<vl>

const double pi = 3.14159265358979323846264;
const int mod = 1000000007;
const int nm = (1 << 18) + 1;
const int nm2 = (1 << 9) + 10;
const int lm = 100002;

inline pair<vi, int> f(int n) {
  vi lst;
  int ret;
  while (n >= nm2) {
    lst.PB(n);
    n /= 2;
  }
  return {lst, n};
}

int main() {
  int n, q;
  cin >> n;
  vl v(nm, 0);
  vl w(nm, 0);
  vll dp(nm2, vl(lm, 0));
  FOR(i, 0, n) { cin >> v[i] >> w[i]; }
  dp[1][w[0]] = v[0];
  FOR(j, 1, lm) { dp[1][j] = max(dp[1][j], dp[1][j - 1]); }
  FOR(i, 1, nm2 + 1) {
    if (i * 2 < nm2) {
      int j = i * 2;
      FOR(k, 0, lm) {
        dp[j][k] = max(dp[i][k], dp[j][k]);
        if (k + w[j - 1] < lm) {
          dp[j][k + w[j - 1]] = max(dp[j][k + w[j - 1]], dp[i][k] + v[j - 1]);
        }
      }
      FOR(jj, 1, lm) { dp[j][jj] = max(dp[j][jj], dp[j][jj - 1]); }
    }
    if (i * 2 + 1 < nm2) {
      int j = i * 2 + 1;
      FOR(k, 0, lm) {
        dp[j][k] = max(dp[i][k], dp[j][k]);
        if (k + w[j - 1] < lm) {
          dp[j][k + w[j - 1]] = max(dp[j][k + w[j - 1]], dp[i][k] + v[j - 1]);
        }
      }
      FOR(jj, 1, lm) { dp[j][jj] = max(dp[j][jj], dp[j][jj - 1]); }
    }
  }
  // cerr<<"test"<<endl;
  cin >> q;
  vi qv(q), qw(q);
  FOR(i, 0, q) {
    cin >> qv[i] >> qw[i];
    if (qv[i] < nm2) {
      // cerr<<"N"<<endl;
      cout << dp[qv[i]][qw[i]] << endl;
    } else {
      auto pr = f(qv[i]);
      vi lst = pr.ST;
      int idx = pr.ND;
      int ln = lst.size();
      // cerr<<ln<<" "<<idx<<endl;
      LL ans = 0;
      FOR(msk, 0, 1 << ln) {
        LL ww = 0, vv = 0;
        FOR(ii, 0, ln) {
          if (msk & (1 << ii)) {
            vv += v[lst[ii] - 1];
            ww += w[lst[ii] - 1];
          }
        }
        if (ww > qw[i])
          continue;
        LL rest = qw[i] - ww;
        vv += dp[idx][rest];
        ans = max(ans, vv);
      }

      cout << ans << endl;
    }
  }
}