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

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef char CHAR;
typedef unsigned char UCHAR;
typedef short SHORT;
typedef unsigned short USHORT;
typedef int INT;
typedef unsigned int UINT;
typedef long long LLONG;
typedef unsigned long long ULLONG;

#define LengthOf(x) (sizeof(x) / sizeof(x[0]))

#if 0
//No.4
int main()
{
	return 0;
}
#endif

#if 1
// NO.3
int sub(INT N, INT M, INT X, INT C[12], INT A[12][12], bool read[12],
        INT skill[12], INT cost, INT start) {
  INT ret = 0;

  // skillを満たした
  bool master = true;
  for (int i = 0; i < M; ++i) {
    if (skill[i] < X) {
      master = false;
      break;
    }
  }

  if (master) {
    ret = cost;
  } else {
    // もう一冊読む
    INT minCost = 0x7FFFFFFF;
    for (int i = start; i < N; ++i) {
      if (read[i] == false) {
        read[i] = true;
        INT newSkill[12];
        for (int j = 0; j < M; ++j) {
          newSkill[j] = skill[j] + A[i][j];
        }

        INT tmpCost =
            sub(N, M, X, C, A, read, newSkill, cost + C[i], start + 1);
        if (tmpCost >= 0 && tmpCost < minCost) {
          minCost = tmpCost;
        }

        read[i] = false;
      }
    }

    if (minCost == 0x7FFFFFFF) {
      ret = -1;
    } else {
      ret = minCost;
    }
  }

  return ret;
}

// No.3
int main() {
  INT N, M, X;
  INT C[12];
  INT A[12][12];

  cin >> N >> M >> X;
  for (int i = 0; i < N; ++i) {
    cin >> C[i];
    for (int j = 0; j < M; ++j) {
      cin >> A[i][j];
    }
  }

  bool read[12];
  INT skill[12];
  memset(read, 0, sizeof(read));
  memset(skill, 0, sizeof(skill));

  INT cost = sub(N, M, X, C, A, read, skill, 0, 0);

  cout << cost;
  return 0;
}
#endif


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef char CHAR;
typedef unsigned char UCHAR;
typedef short SHORT;
typedef unsigned short USHORT;
typedef int INT;
typedef unsigned int UINT;
typedef long long LLONG;
typedef unsigned long long ULLONG;

#define LengthOf(x) (sizeof(x) / sizeof(x[0]))

#if 0
//No.4
int main()
{
	return 0;
}
#endif

#if 1
// NO.3
int sub(INT N, INT M, INT X, INT C[12], INT A[12][12], bool read[12],
        INT skill[12], INT cost) {
  INT ret = 0;

  // skillを満たした
  bool master = true;
  for (int i = 0; i < M; ++i) {
    if (skill[i] < X) {
      master = false;
      break;
    }
  }

  if (master) {
    ret = cost;
  } else {
    // もう一冊読む
    INT minCost = 0x7FFFFFFF;
    for (int i = 0; i < N; ++i) {
      if (read[i] == false) {
        read[i] = true;
        INT newSkill[12];
        for (int j = 0; j < M; ++j) {
          newSkill[j] = skill[j] + A[i][j];
        }

        INT tmpCost = sub(N, M, X, C, A, read, newSkill, cost + C[i]);
        if (tmpCost >= 0 && tmpCost < minCost) {
          minCost = tmpCost;
        }

        read[i] = false;
      }
    }

    if (minCost == 0x7FFFFFFF) {
      ret = -1;
    } else {
      ret = minCost;
    }
  }

  return ret;
}

// No.3
int main() {
  INT N, M, X;
  INT C[12];
  INT A[12][12];

  cin >> N >> M >> X;
  for (int i = 0; i < N; ++i) {
    cin >> C[i];
    for (int j = 0; j < M; ++j) {
      cin >> A[i][j];
    }
  }

  bool read[12];
  INT skill[12];
  memset(read, 0, sizeof(read));
  memset(skill, 0, sizeof(skill));

  INT cost = sub(N, M, X, C, A, read, skill, 0);

  cout << cost;
  return 0;
}
#endif