func_code_string: #include <bits/stdc++.h>   using namespace std;   struct Update {     int cost, profit;     Update(int cost, int profit) : cost(cost), profit(profit) {}   };   struct CommutativeUndoableDS {     const int MAX_COST = 4000;     vector<vector<int> > stk;     CommutativeUndoableDS() { stk.emplace_back(MAX_COST + 1, 0); }     void addUpdate(Update u) {       int sz = stk.size();       stk.push_back(stk.back());       for (int i = MAX_COST; i >= u.cost; i--) {         stk[sz][i] = max(stk[sz][i], stk[sz - 1][i - u.cost] + u.profit);       }     }     void undoLastUpdate() {       stk.pop_back();       assert(!stk.empty());     }     int bestProfitWith(int budget) { return stk.back()[budget]; }   };   struct QueueUndoDS {     vector<pair<Update, int> > qyu;     CommutativeUndoableDS ds;     void pushBack(pair<Update, int> u) {       ds.addUpdate(u.first);       qyu.push_back(u);     }     void pushBack(Update u) { pushBack(pair<Update, int>(u, 0)); }     void popFront() {       assert(!qyu.empty());       vector<pair<Update, int> > popped[2];       while (!qyu.empty()) {         ds.undoLastUpdate();         popped[qyu.back().second].push_back(qyu.back());         qyu.pop_back();         if (popped[1].size() >= popped[0].size()) break;       }       if (popped[1].empty()) {         for (pair<Update, int> &u : popped[0]) {           u.second = 1;           pushBack(u);         }       } else {         for (int i : {0, 1}) {           reverse(popped[i].begin(), popped[i].end());           for (pair<Update, int> &u : popped[i]) pushBack(u);         }       }       ds.undoLastUpdate();       qyu.pop_back();     }   };   const int MAX_TIME = 20001;   vector<Update> startsAt[MAX_TIME];   vector<Update> endsAt[MAX_TIME];   vector<pair<int, int> > askAt[MAX_TIME];   int ans[MAX_TIME];   int main() {     int n, p;     cin >> n >> p;     while (n--) {       int c, h, t;       cin >> c >> h >> t;       startsAt[t].emplace_back(c, h);       endsAt[t + p - 1].emplace_back(c, h);     }     int q;     cin >> q;     for (int i = 1; i <= q; i++) {       int a, b;       cin >> a >> b;       askAt[a].emplace_back(i, b);     }     QueueUndoDS knapsackQueuUndo;     for (int t = 1; t < MAX_TIME; t++) {       for (Update u : startsAt[t]) {         knapsackQueuUndo.pushBack(u);       }       for (pair<int, int> query : askAt[t]) {         ans[query.first] = knapsackQueuUndo.ds.bestProfitWith(query.second);       }       for (Update u : endsAt[t]) {         knapsackQueuUndo.popFront();       }     }     for (int i = 1; i <= q; i++) {       cout << ans[i] <<   n ;     }     return 0;   }   