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

#define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double eps = 1e-8;

vector<int> split(string delim, string str) {
  vector<int> res;
  str += delim;
  for (int i = 0; i < str.size(); i++) {
    for (int j = 1; i + j <= str.size(); j++) {
      if (str.substr(i, j).find(delim) != string::npos) {
        int num = atoi(str.substr(i, j - 1).c_str());
        if (str.substr(i, j - 1).size() > 0)
          res.push_back(num);
        i += j - 1;
        break;
      }
    }
  }

  return res;
}

int main() {
  string str;
  while (getline(cin, str)) {
    vector<int> data = split(" ", str);
    if (data.size() == 1 && data[0] == 0)
      break;
    set<int> *dp = new set<int>[data.size() + 1];

    dp[0].insert(0);
    for (int i = 0; i < data.size(); i++) {
      if (data[i] == 1) {
        for (set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++) {
          dp[i + 1].insert(*it + 1);
          dp[i + 1].insert(*it + 11);
        }
      } else if (2 <= data[i] && data[i] <= 9) {
        for (set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++) {
          dp[i + 1].insert(*it + data[i]);
        }
      } else {
        for (set<int>::iterator it = dp[i].begin(); it != dp[i].end(); it++) {
          dp[i + 1].insert(*it + 10);
        }
      }
    }

    int res = MAX_N;
    for (set<int>::iterator it = dp[data.size()].begin();
         it != dp[data.size()].end(); it++) {
      if (*it <= 21 && abs(*it - 21) < abs(res - 21)) {
        res = *it;
      }
    }

    printf("%d\\n", res == MAX_N ? 0 : res);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double eps = 1e-8;

vector<int> split(string delim, string str) {
  vector<int> res;
  str += delim;
  for (int i = 0; i < str.size(); i++) {
    for (int j = 1; i + j <= str.size(); j++) {
      if (str.substr(i, j).find(delim) != string::npos) {
        int num = atoi(str.substr(i, j - 1).c_str());
        if (str.substr(i, j - 1).size() > 0)
          res.push_back(num);
        i += j - 1;
        break;
      }
    }
  }

  return res;
}

void dfs(const vector<int> &data, int i, int sum, int *res) {

  if (i == data.size()) {
    if (sum <= 21 && abs(*res - 21) > abs(sum - 21)) {
      *res = sum;
    }
    return;
  }
  if (data[i] == 1) {
    dfs(data, i + 1, sum + 1, res);
    dfs(data, i + 1, sum + 11, res);
  } else if (2 <= data[i] && data[i] <= 9) {
    dfs(data, i + 1, sum + data[i], res);
  } else {
    dfs(data, i + 1, sum + 10, res);
  }
}

int main() {
  string str;
  while (getline(cin, str)) {
    vector<int> data = split(" ", str);
    if (data.size() == 1 && data[0] == 0)
      break;
    int res = 10000;
    dfs(data, 0, 0, &res);
    printf("%d\\n", res == 10000 ? 0 : res);
  }
}