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

#include <bits/stdc++.h>

using namespace std;

#define whole(xs) (xs).begin(), (xs).end()

namespace {

typedef double real;
typedef long long ll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

int N;
vector<int> A;
void input() {
  cin >> N;
  A.resize(N);
  cin >> A;
}

const int INF = 1 << 28;

int cost(int x, int y) {
  int ret = 0;
  int c = 0;
  for (int i = 0;; i++) {
    if (x == 0 && y == 0 && c == 0)
      break;
    int xi = x % 10;
    x /= 10;
    int yi = y % 10;
    y /= 10;
    ret += xi * yi + c;
    if (xi + yi + c >= 10)
      c = 1;
    else
      c = 0;
  }
  return ret;
}

void solve() {

  vector<int> sum(N + 1, 0);
  for (int i = 0; i < N; i++) {
    sum[i + 1] = sum[i] + A[i];
  }

  vector<vector<int>> dp[2];
  for (int i = 0; i < 2; i++)
    dp[i] = vector<vector<int>>(N, vector<int>(N + 1, INF));

  for (int i = 0; i + 1 <= N; i++) {
    dp[0][i][i + 1] = 0;
  }

  for (int k = 2; k <= N; k++) {
    dp[1] = dp[0];
    for (int i = 0; i + k <= N; i++) {
      for (int j = 1; j < k; j++) {
        // cerr << k << " " << i << " " << j<< endl;
        dp[0][i][i + k] =
            min(dp[0][i][i + k],
                dp[1][i][i + j] + dp[1][i + j][i + k] +
                    cost(sum[i + j] - sum[i], sum[i + k] - sum[i + j]));
      }
    }
  }
  // cout << dp[0] << endl;
  cout << dp[0][0][N] << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define whole(xs) (xs).begin(), (xs).end()

namespace {

typedef double real;
typedef long long ll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

int N;
vector<int> A;
void input() {
  cin >> N;
  A.resize(N);
  cin >> A;
}

const int INF = 1 << 28;

string to_s(int n) {
  ostringstream os;
  os << n;
  return os.str();
}

int cost(int x, int y) {
  int ret = 0;
  int c = 0;
  for (int i = 0;; i++) {
    if (x == 0 && y == 0 && c == 0)
      break;
    int xi = x % 10;
    x /= 10;
    int yi = y % 10;
    y /= 10;
    ret += xi * yi + c;
    if (xi + yi >= 10)
      c = 1;
  }
  return ret;
}

void solve() {

  vector<int> sum(N + 1, 0);
  for (int i = 0; i < N; i++) {
    sum[i + 1] = sum[i] + A[i];
  }

  vector<vector<int>> dp[2];
  for (int i = 0; i < 2; i++)
    dp[i] = vector<vector<int>>(N, vector<int>(N + 1, INF));

  for (int i = 0; i + 1 <= N; i++) {
    dp[0][i][i + 1] = 0;
  }

  for (int k = 2; k <= N; k++) {
    dp[1] = dp[0];
    swap(dp[0], dp[1]);
    for (int i = 0; i + k <= N; i++) {
      for (int j = 1; j < k; j++) {
        dp[0][i][i + k] =
            min(dp[0][i][i + k],
                dp[1][i][i + j] + dp[1][i + j][i + k] +
                    cost(sum[i + j] - sum[i], sum[i + k] - sum[i + j]));
      }
    }
  }
  // cout << dp[0] << endl;
  cout << dp[0][0][N] << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}