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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 8192

int dp[NUM + 1];

void dfs(int current, int array[15], int depth) {

  if (depth == 13)
    return;

  for (int i = 0; i <= depth; i++) {
    if (current + array[i] > NUM)
      continue;

    if (dp[current + array[i]] >= depth + 1) {
      dp[current + array[i]] = depth + 1;
      int next_array[15];
      for (int k = 0; k <= depth; k++)
        next_array[k] = array[k];
      next_array[depth + 1] = current + array[i];
      dfs(current + array[i], next_array, depth + 1);
    }
  }

  for (int i = 0; i <= depth; i++) {
    if (current - array[i] <= 0)
      continue;

    if (dp[current - array[i]] >= depth + 1) {
      dp[current - array[i]] = depth + 1;
      int next_array[15];
      for (int k = 0; k <= depth; k++)
        next_array[k] = array[k];
      next_array[depth + 1] = current - array[i];
      dfs(current - array[i], next_array, depth + 1);
    }
  }
}

int main() {

  dp[0] = 0;
  dp[1] = 0;
  for (int i = 2; i <= NUM; i++)
    dp[i] = BIG_NUM;

  int first_array[15];
  first_array[0] = 1;

  dfs(1, first_array, 0);

  int N;

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    printf("%d\\n", dp[N]);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 8192

int dp[NUM + 1];

void dfs(int current, bool check[NUM + 1], int depth) {

  if (depth == 13)
    return;

  for (int i = 1; current + i <= NUM; i++) {
    if (check[i]) {
      if (dp[current + i] >= depth + 1) {
        dp[current + i] = depth + 1;
        bool next_check[NUM + 1];
        for (int k = 1; k <= NUM; k++)
          next_check[k] = check[k];
        next_check[current + i] = true;
        dfs(current + i, next_check, depth + 1);
      }
    }
  }

  for (int i = 1; i <= current - 1; i++) {
    if (check[i]) {
      if (dp[current - i] >= depth + 1) {
        dp[current - i] = depth + 1;
        bool next_check[NUM + 1];
        for (int k = 1; k <= NUM; k++)
          next_check[k] = check[k];
        next_check[current - i] = true;
        dfs(current - i, next_check, depth + 1);
      }
    }
  }
}

int main() {

  dp[0] = 0;
  dp[1] = 0;
  for (int i = 2; i <= NUM; i++)
    dp[i] = BIG_NUM;

  bool first_check[NUM + 1];

  first_check[1] = true;
  for (int i = 2; i <= NUM; i++)
    first_check[i] = false;

  dfs(1, first_check, 0);

  int N;

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    printf("%d\\n", dp[N]);
  }

  return 0;
}