Problem p01764 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

#define INF 131211109876543210LL
#define MAX_N 350
#define MAX_K 10
#define MAX_NUM 10

long long wa[MAX_N][MAX_N];
long long dp[MAX_N][MAX_N];
long long x[MAX_N], y[MAX_N];
long long power[MAX_K];
long long a1[MAX_K], a2[MAX_K], a3[MAX_K];
long long N;

long long keisan(long long v, long long w) {
  for (int i = 0; i < MAX_K; i++) {
    power[i] = 0LL;
    a1[i] = 0LL;
    a2[i] = 0LL;
    a3[i] = 0LL;
  }
  power[0] = 1;
  for (int i = 1; i < MAX_K; i++) {
    power[i] = power[i - 1] * MAX_NUM;
  }
  for (int i = 0; i < MAX_K; i++) {
    a1[i] = (v / power[i]) % MAX_NUM;
  }
  for (int i = 0; i < MAX_K; i++) {
    a2[i] = (w / power[i]) % MAX_NUM;
  }
  long long res1 = 0;
  for (int i = 0; i < MAX_K; i++) {
    res1 += a1[i] * a2[i];
  }
  a3[0] = (a1[0] + a2[0]) / MAX_NUM;
  for (int i = 1; i < MAX_K; i++) {
    long long a4 = a1[i] + a2[i] + a3[i - 1];
    if (a4 >= MAX_NUM) {
      a3[i] = 1;
    } else {
      a3[i] = 0;
    }
  }
  for (int i = 0; i < MAX_K; i++) {
    res1 += a3[i];
  }
  return res1;
}

void WA() {
  y[0] = x[0];
  for (int i = 1; i < N; i++) {
    y[i] = y[i - 1] + x[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      wa[i][j] = y[j] - y[i - 1];
    }
  }
}

int DP() {
  WA();
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 0; i < N; i++) {
    dp[i][i] = 0;
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= N - i; j++) {
      long long a5 = j, a6 = j + i;

      for (int k = a5; k < a6; k++) {
        long long a7 = dp[a5][k];
        long long a8 = dp[k + 1][a6];
        long long a9 = wa[a5][k];
        long long a10 = wa[k + 1][a6];
        long long a11 = keisan(a9, a10);
        long long a12 = a7 + a8 + a11;
        dp[a5][a6] = min(dp[a5][a6], a12);
      }
    }
  }
  return dp[0][N - 1];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  cout << DP() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

#define INF 131211109876543210LL
#define MAX_N 301
#define MAX_K 9
#define MAX_NUM 10

long long wa[MAX_N][MAX_N];
long long dp[MAX_N][MAX_N];
long long x[MAX_N], y[MAX_N];
long long power[MAX_K];
long long a1[MAX_K], a2[MAX_K], a3[MAX_K];
long long N;

long long keisan(long long v, long long w) {
  for (int i = 0; i < MAX_K; i++) {
    power[i] = 0LL;
    a1[i] = 0LL;
    a2[i] = 0LL;
    a3[i] = 0LL;
  }
  power[0] = 1;
  for (int i = 1; i < MAX_K; i++) {
    power[i] = power[i - 1] * MAX_NUM;
  }
  for (int i = 0; i < MAX_K; i++) {
    a1[i] = (v / power[i]) % MAX_NUM;
  }
  for (int i = 0; i < MAX_K; i++) {
    a2[i] = (w / power[i]) % MAX_NUM;
  }
  long long res1 = 0;
  for (int i = 0; i < MAX_K; i++) {
    res1 += a1[i] * a2[i];
  }
  a3[0] = (a1[0] + a2[0]) / MAX_NUM;
  for (int i = 1; i < MAX_K; i++) {
    long long a4 = a1[i] + a2[i] + a3[i - 1];
    if (a4 >= MAX_NUM) {
      a3[i] = 1;
    } else {
      a3[i] = 0;
    }
  }
  for (int i = 0; i < MAX_K; i++) {
    res1 += a3[i];
  }
  return res1;
}

void WA() {
  y[0] = x[0];
  for (int i = 1; i < N; i++) {
    y[i] = y[i - 1] + x[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      wa[i][j] = y[j] - y[i - 1];
    }
  }
}

int DP() {
  WA();
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 0; i < N; i++) {
    dp[i][i] = 0;
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= N - i; j++) {
      long long a5 = j, a6 = j + i;

      for (int k = a5; k < a6; k++) {
        long long a7 = dp[a5][k];
        long long a8 = dp[k + 1][a6];
        long long a9 = wa[a5][k];
        long long a10 = wa[k + 1][a6];
        long long a11 = keisan(a9, a10);
        long long a12 = a7 + a8 + a11;
        dp[a5][a6] = min(dp[a5][a6], a12);
      }
    }
  }
  return dp[0][N - 1];
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  cout << DP() << endl;
  return 0;
}