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

#include <algorithm> // require sort next_permutation count etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require sqrt ceil floor
#include <cstdio>  // require printf
#include <cstdlib> // require abs
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <stdint.h> // require uint64_t
#include <string>
#include <vector>

using namespace std;

typedef pair<pair<int, int>, string>
    ITEM; // ACeÌd³AACeÌãÉu¯éd³ÌãÀAACe¼

int memo[10][10]; // s: ACe¼ ñ:½iÚ© -1:
                  // Ü¾ãÉÏßé©Ç¤©¢èA0:ÏßÈ¢ 1:
                  // Ïßé
vector<ITEM> list;
int N;
int minG;
vector<int> minorder;
int total;

int getG(vector<int> order) {
  int res = 0;

  for (int i = 0; i < order.size(); ++i) {
    res += (i + 1) * (list[order[i]].first.first);
  } // end for

  return res;
}

void bt(vector<int> &order, int curr) {
  if (curr == N) {
    int currG = getG(order);
    if (currG < minG) {
      minorder = order;
      minG = currG;
    } // end if
    return;
  } // end if

  if (order[curr] != -1 && memo[order[curr]][curr] == 0) {
    return;
  } // end if

  if (order[curr] == -1) {
    vector<bool> used(N, false);
    for (int i = 0; i < curr; ++i) {
      used[order[i]] = true;
    } // end if

    for (int i = 0; i < N; ++i) {
      if (!used[i]) {
        order[curr] = i;
        int curr_w = total;
        for (int j = 0; j <= curr; ++j) {
          curr_w -= list[order[j]].first.first;
        } // end for
        if (curr_w <= list[order[curr]].first.second) {
          memo[order[curr]][curr] = 1;
          bt(order, curr + 1);
        } else {
          memo[order[curr]][curr] = 0;
        } // end if
        order[curr] = -1;
      } // end if
    }   // end for
  }     // end if
}

void init(int n) {
  N = n;
  list.clear();
  list.resize(n);
  minorder.clear();
  minorder.resize(n);
  total = 0;
  minG = INT_MAX;
  memset(memo, -1, sizeof(memo));
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.lunch", "r", stdin );
  int n;

  while (cin >> n && n) {
    init(n);
    for (int i = 0; i < n; ++i) {
      cin >> list[i].second;
      cin >> list[i].first.first;  // w
      cin >> list[i].first.second; // s
      total += list[i].first.first;
    } // end for
    vector<int> order(n, -1);

    bt(order, 0);

    for (int i = 0; i < n; ++i) {
      cout << list[minorder[i]].second << endl;
    } // end for

  } // end loop

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // require sort next_permutation count etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require sqrt ceil floor
#include <cstdio>  // require printf
#include <cstdlib> // require abs
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <stdint.h> // require uint64_t
#include <string>
#include <vector>

using namespace std;

typedef pair<pair<int, int>, string>
    ITEM; // ACeÌd³AACeÌãÉu¯éd³ÌãÀAACe¼

int memo[10][10]; // s: ACe¼ ñ:½iÚ© -1:
                  // Ü¾ãÉÏßé©Ç¤©¢èA0:ÏßÈ¢ 1:
                  // Ïßé
vector<ITEM> list;
int N;

int dfs(vector<int> order, int curr) {
  if (curr == N) {
    return 1;
  } // end if

  if (memo[order[curr]][curr] == 0) {
    return 0;
  } // end if

  int total_wight = 0;

  for (int i = curr + 1; i < N; ++i) {
    total_wight += list[order[i]].first.first;
  } // end if

  if (total_wight <= list[order[curr]].first.second) {
    memo[order[curr]][curr] = 1;
    return dfs(order, curr + 1);
  } else {
    return (memo[order[curr]][curr] = 0);
  } // end if

  return 0;
}

int getG(vector<int> order) {
  int res = 0;

  for (int i = 0; i < order.size(); ++i) {
    res += (i + 1) * (list[order[i]].first.first);
  } // end for

  return res;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.lunch", "r", stdin );
  int n;

  while (cin >> n && n) {
    N = n;
    list.clear();
    list.resize(n);
    memset(memo, -1, sizeof(memo));
    for (int i = 0; i < n; ++i) {
      cin >> list[i].second;
      cin >> list[i].first.first;  // w
      cin >> list[i].first.second; // s
    }                              // end for
    vector<int> order(n, 0);
    for (int i = 0; i < n; ++i) {
      order[i] = i;
    } // end for

    vector<int> minorder;
    int minG = INT_MAX;
    do {
      if (dfs(order, 0) > 0) {
        int currG = getG(order);
        if (currG < minG) {
          minorder = order;
          minG = currG;
        } // end if
      }   // end if
    } while (next_permutation(order.begin(), order.end()));

    for (int i = 0; i < n; ++i) {
      cout << list[minorder[i]].second << endl;
    } // end for

  } // end loop

  return 0;
}