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

#include <bits/stdc++.h>
using namespace std;
int n, m, x;
vector<vector<int>> textList;
vector<bool> isBought;
long totalCost = -1;
vector<int> skillVector;
void calc() {
  // cout << "calc " << "selectList.size():" << selectList.size() << endl;
  long currentCost = 0;
  for (int i = 0; i < isBought.size(); i++) {
    if (isBought[i]) {
      currentCost += textList[i][0];
      for (int j = 1; j <= m; j++) {
        skillVector[j - 1] += textList[i][j];
      }
    }
  }
  bool isValid = true;
  for (int i = 0; i < skillVector.size(); i++) {
    // cout << " skillVector[i]:"<<skillVector[i];
    if (skillVector[i] < x) {
      isValid = false;
    }
  }
  // cout << endl;
  // cout << "currentCost:" << currentCost<<endl;
  if (isValid) {
    if (totalCost == -1) {
      totalCost = currentCost;
    } else {
      if (totalCost > currentCost) {
        totalCost = currentCost;
      }
    }
  }
  for (int i = 0; i < skillVector.size(); i++) {
    skillVector[i] = 0;
  }
#if 0
for(int i = 0; i < selectList.size(); i++){
    cout << selectList[i] << " ";
}
cout << endl;
#endif
}
void dfs(bool buy) {
  // cout << "dfs " << textIndex << endl;
  isBought.push_back(buy);
  if (isBought.size() == n) {
    // 探索終了結果判定
    calc();
    return;
  }
  dfs(true);
  isBought.pop_back();
  dfs(false);
  isBought.pop_back();
}
int main() {
  cin >> n >> m >> x;
  for (int i = 0; i < n; i++) {
    textList.push_back(vector<int>(m + 1));
    for (int j = 0; j <= m; j++) {
      cin >> textList[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    skillVector.push_back(0);
  }
  dfs(true);
  isBought.pop_back();
  dfs(false);
  isBought.pop_back();
  cout << totalCost << endl;
#if 0
    cout << n << " " << m << " " << x << endl;
    for(int i = 0; i < textList.size(); i++){
        for(int j = 0; j<textList[i].size(); j++){
            cout << textList[i][j] << " ";
        }
        cout << endl;
    }
#endif
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, x;
vector<vector<int>> textList;
vector<int> selectList;
vector<int> availableIndexList;
long totalCost = -1;
vector<int> skillVector;
void calc() {
  // cout << "calc " << "selectList.size():" << selectList.size() << endl;
  long currentCost = 0;
  for (int i = 0; i < selectList.size(); i++) {
    currentCost += textList[selectList[i]][0];
    for (int j = 1; j <= m; j++) {
      skillVector[j - 1] += textList[selectList[i]][j];
    }
  }
  bool isValid = true;
  for (int i = 0; i < skillVector.size(); i++) {
    // cout << " skillVector[i]:"<<skillVector[i];
    if (skillVector[i] < x) {
      isValid = false;
    }
  }
  // cout << endl;
  // cout << "currentCost:" << currentCost<<endl;
  if (isValid) {
    if (totalCost == -1) {
      totalCost = currentCost;
    } else {
      if (totalCost > currentCost) {
        totalCost = currentCost;
      }
    }
  }
  for (int i = 0; i < skillVector.size(); i++) {
    skillVector[i] = 0;
  }
#if 0
for(int i = 0; i < selectList.size(); i++){
    cout << selectList[i] << " ";
}
cout << endl;
#endif
}
void dfs(int textIndex) {
  // cout << "dfs " << textIndex << endl;
  selectList.push_back(textIndex);
  availableIndexList[textIndex] = -1;
  if (selectList.size() == n) {
    // 探索終了結果判定
    calc();
    return;
  } else {
    // 全部買わなくてもいい。
    long tmp = totalCost;
    calc();
    if (totalCost != tmp) {
      // cout << "!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
      // 探索終了
      return;
    }
  }
  for (int i = 0; i < availableIndexList.size(); i++) {
    if (availableIndexList[i] != -1) {
      dfs(availableIndexList[i]);
      availableIndexList[i] = i;
      selectList.pop_back();
    }
  }
}
int main() {
  cin >> n >> m >> x;
  for (int i = 0; i < n; i++) {
    textList.push_back(vector<int>(m + 1));
    availableIndexList.push_back(i);
    for (int j = 0; j <= m; j++) {
      cin >> textList[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    skillVector.push_back(0);
  }
  for (int i = 0; i < availableIndexList.size(); i++) {
    if (availableIndexList[i] != -1) {
      dfs(availableIndexList[i]);
      availableIndexList[i] = i;
      selectList.pop_back();
    }
  }
  cout << totalCost << endl;
#if 0
    cout << n << " " << m << " " << x << endl;
    for(int i = 0; i < textList.size(); i++){
        for(int j = 0; j<textList[i].size(); j++){
            cout << textList[i][j] << " ";
        }
        cout << endl;
    }
#endif
  return 0;
}