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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define X first
#define Y second
#define pb push_back
#define rep(X, Y) for (int(X) = 0; (X) < int(Y); ++(X))
#define rrep(X, Y) for (int(X) = int(Y - 1); (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll dp[312][312], INF = 1ll << 50, a[312], n, sum[312];

ll Cost(ll l, ll m) { // cout<<"cost:"<<l<<","<<m<<":";
  if (l < m)
    swap(l, m);
  ll re = 0;
  int f = 0;
  while (l) {
    re += (l % 10) * (m % 10);
    re += f = ((l % 10) + (m % 10) + f >= 10);
    l /= 10;
    m /= 10;
  }
  return re;
}

int main() {
  int i, j, k;
  fill(dp[0], dp[0] + 312 * 312, INF);
  scanf("%lld", &n);
  rep(i, n) scanf("%lld", a + i);
  rep(i, n) sum[i + 1] = a[i] + sum[i];
  rep(i, n + 1) rep(j, n - i + 1) {
    ll &tmp = dp[j][i + j];
    if (i < 2)
      tmp = 0;
    else
      for (k = j + 1; k < i + j; k++)
        tmp = min(tmp, dp[j][k] + dp[k][i + j] +
                           Cost(sum[k] - sum[j], sum[i + j] - sum[k]));
  }
  printf("%lld\\n", dp[0][n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define X first
#define Y second
#define pb push_back
#define rep(X, Y) for (int(X) = 0; (X) < int(Y); ++(X))
#define rrep(X, Y) for (int(X) = int(Y - 1); (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll dp[312][312], INF = 1ll << 50, a[312], n, sum[312];

ll Cost(ll l, ll m) { // cout<<"cost:"<<l<<","<<m<<":";
  if (l < m)
    swap(l, m);
  ll re = 0;
  vector<ll> nn, mm;
  while (l) {
    nn.pb(l % 10);
    l /= 10;
    mm.pb(m % 10);
    m /= 10;
  }
  int f = 0;
  rep(i, nn.size()) {
    re += nn[i] * mm[i];
    re += f = (nn[i] + mm[i] + f >= 10);
  }
  // cout<<re<<endl;
  return re;
}

ll solve(ll l, ll r) {
  //  cout<<l<<","<<r<<endl;
  if (dp[l][r] != INF)
    return dp[l][r];
  if (r - l <= 1)
    return 0;
  ll m, re = INF;
  for (m = l + 1; m < r; m++) {
    re = min(re, solve(l, m) + solve(m, r) +
                     Cost(sum[r] - sum[m], sum[m] - sum[l]));
  }
  cout << l << "," << r << ":" << re << endl;
  return dp[l][r] = re;
}

int main() {
  int i, j, k;
  fill(dp[0], dp[0] + 312 * 312, INF);
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) sum[i + 1] = a[i] + sum[i];
  rep(i, n + 1) rep(j, n - i + 1) {
    ll &tmp = dp[j][i + j];
    if (i < 2)
      tmp = 0;
    else
      for (k = j + 1; k < i + j; k++)
        tmp = min(tmp, dp[j][k] + dp[k][i + j] +
                           Cost(sum[k] - sum[j], sum[i + j] - sum[k]));
  }
  cout << dp[0][n] << endl;
  return 0;
}