Problem p01764 (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;

int N;
int table[301], memo[301][301];

int calc(int left, int right) {

  int left_table[10], right_table[10];
  for (int i = 0; i < 10; i++) {
    left_table[i] = 0;
    right_table[i] = 0;
  }

  int index = 0;

  while (left > 0) {
    left_table[index++] = left % 10;
    left /= 10;
  }

  index = 0;

  while (right > 0) {
    right_table[index++] = right % 10;
    right /= 10;
  }

  int ret = 0, kuriagari = 0;

  for (int i = 0; i < 10; i++) {
    ret += left_table[i] * right_table[i] + kuriagari;
    if (left_table[i] + right_table[i] + kuriagari >= 10) {
      kuriagari = 1;
    } else {
      kuriagari = 0;
    }
  }

  return ret;
}

int recursive(int left, int right) {

  if (left == right)
    return 0;

  if (memo[left][right] != BIG_NUM)
    return memo[left][right];

  for (int mid = left; mid <= right - 1; mid++) {
    memo[left][right] =
        min(memo[left][right],
            recursive(left, mid) + recursive(mid + 1, right) +
                calc(table[mid] - table[left - 1], table[right] - table[mid]));
  }

  return memo[left][right];
}

int main() {

  scanf("%d", &N);

  table[0] = 0;

  for (int i = 1; i <= N; i++) {
    scanf("%d", &table[i]);
    table[i] += table[i - 1];
  }

  for (int left = 0; left <= N; left++) {
    for (int right = 0; right <= N; right++)
      memo[left][right] = BIG_NUM;
  }

  printf("%d\\n", recursive(1, 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;

int N;
int table[301], memo[301][301];

int calc(int left, int right) {

  int left_table[10], right_table[10];
  for (int i = 0; i < 10; i++) {
    left_table[i] = 0;
    right_table[i] = 0;
  }

  queue<int> Q;

  while (left > 0) {
    Q.push(left % 10);
    left /= 10;
  }

  int index = 0;
  while (!Q.empty()) {
    left_table[index++] = Q.front();
    Q.pop();
  }

  while (right > 0) {
    Q.push(right % 10);
    right /= 10;
  }

  index = 0;
  while (!Q.empty()) {
    right_table[index++] = Q.front();
    Q.pop();
  }

  int ret = 0, kuriagari = 0;

  for (int i = 0; i < 10; i++) {
    ret += left_table[i] * right_table[i] + kuriagari;
    if (left_table[i] + right_table[i] + kuriagari >= 10) {
      kuriagari = 1;
    } else {
      kuriagari = 0;
    }
  }

  return ret;
}

int recursive(int left, int right) {

  if (left == right)
    return 0;

  if (memo[left][right] != BIG_NUM)
    return memo[left][right];

  for (int mid = left; mid <= right - 1; mid++) {
    memo[left][right] =
        min(memo[left][right],
            recursive(left, mid) + recursive(mid + 1, right) +
                calc(table[mid] - table[left - 1], table[right] - table[mid]));
  }

  return memo[left][right];
}

int main() {

  scanf("%d", &N);

  table[0] = 0;

  for (int i = 1; i <= N; i++) {
    scanf("%d", &table[i]);
    table[i] += table[i - 1];
  }

  for (int left = 0; left <= N; left++) {
    for (int right = 0; right <= N; right++)
      memo[left][right] = BIG_NUM;
  }

  printf("%d\\n", recursive(1, N));

  return 0;
}