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

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <set>
#include <vector>

#define MAX 64
#define WMAX 1024
#define CAP_MAX 128

using namespace std;

int water[MAX];
int beaker[MAX];
bool vis[MAX];

bool dfs(int n, int num);
bool pour(int n, int w, int from, int now, int num);

bool pour(int n, int w, int from, int now, int num) {
  if (w == 0) {
    if (dfs(n, num))
      return true;
    return false;
  }
  if (w < 0 || from >= n || now >= n)
    return false;

  for (int i = from + 1; i < n; ++i) {
    if (beaker[i] == water[i])
      break;
    if (!vis[i] && beaker[i] > w)
      return false;
  }

  int pre = -1;
  for (int i = now; i < n; ++i) {
    if (!vis[i] && beaker[i] <= w) {
      if (pre == beaker[i])
        continue;

      water[i] += beaker[i];
      vis[i] = true;
      if (pour(n, w - beaker[i], from, i + 1, num + 1))
        return true;
      vis[i] = false;
      water[i] -= beaker[i];

      pre = beaker[i];
    }
  }
  return false;
}

bool dfs(int n, int num) {
  if (num == n) {
    return true;
  }

  for (int i = 0; i < n; ++i) {
    if (vis[i] && beaker[i] == water[i]) {
      water[i] = 0;
      if (pour(n, beaker[i], i, i + 1, num))
        return true;
      water[i] = beaker[i];
    }
  }
  return false;
}

int main() {
  while (true) {
    int n;
    string res = "NO";

    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < MAX; ++i) {
      beaker[i] = 0;
      vis[i] = false;
      water[i] = 0;
    }

    for (int i = 0; i < n; ++i) {
      cin >> beaker[i];
    }

    sort(beaker, beaker + n, greater<int>());
    water[0] = beaker[0];
    vis[0] = true;

    if (dfs(n, 1)) {
      res = "YES";
    }

    puts(res.c_str());
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

#define MAX 64
#define WMAX 1024
#define CAP_MAX 128

using namespace std;

int cnt;
int depth;
bool dfs(int num, int n, int beaker[], bool vis[], int rem[]) {
  //++cnt;
  // if( cnt > 10000 ) return false;
  if (num == n) {
    for (int i = 0; i < n; ++i)
      if (0 < rem[i] && rem[i] < beaker[i])
        return false;
    return true;
  }

  /*
  for(int i = 0; i < depth; ++i){
    putchar(' ');
  }
  for(int i = 0; i < n; ++i){
    cout << beaker[i] << "(" << rem[i] << ")" << ' ';
  }
  cout << endl;
  */

  bool first = true;
  for (int i = 0; i < n; ++i) {
    if (vis[i] && rem[i] > 0) {
      for (int j = i + 1; j < n; ++j) {
        if (!vis[j] && rem[j] == 0 && beaker[j] <= rem[i]) {

          rem[i] -= beaker[j];
          int t = rem[j];
          rem[j] = beaker[j];

          vis[j] = true;
          //++depth;
          if (dfs(num + 1, n, beaker, vis, rem))
            return true;
          //--depth;
          vis[j] = false;

          rem[i] += beaker[j];
          rem[j] = t;

          if (i == 0)
            return false;
          break;
        }
      }
      first = false;
    } else {
      if (!first)
        return false;
    }
  }

  return false;
}

int main() {
  while (true) {
    int n;
    bool vis[MAX] = {
        false,
    };
    int rem[MAX];
    int trem = 0;
    int beaker[MAX];
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; ++i) {
      cin >> beaker[i];
      rem[i] = 0;
      if (i > 0)
        trem += beaker[i];
    }
    sort(beaker, beaker + n, greater<int>());
    vis[0] = true;
    rem[0] = beaker[0];
    depth = 0;
    string res = "NO";
    if (n == 1 || (n > 1 && trem >= beaker[0])) {
      if (dfs(1, n, beaker, vis, rem)) {
        res = "YES";
      }
    }
    // cout << "COUNT : " << cnt << endl;
    puts(res.c_str());
  }
  return 0;
}