alexjercan/bugnet huggingface dataset:
Problem p01757 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef pair<int, int> P;
map<int, int> mp;
int N, M, a[10001], b[10000];
int seginc(int l, int r) {
  int x = upper_bound(a, a + M + 1, l) - a - 1;
  if (r <= a[x + 1])
    return x;
  return -1;
}
int query(int l, int r, int k, int L, int j) {
  if (j + L == N && mp.count(k))
    return mp[k];
  int in = seginc(l, r);
  int ret = 0;
  if (in >= 0) {
    if (j == b[in])
      ret = (r - l) - 1;
    else if (b[in] > N - L)
      ret = (r - l) - (1 << (b[in] - (N - L) - 1));
    else
      ret = r - l;
  } else {
    int x = query(l, (l + r) / 2, k * 2, L - 1, j) +
            query((l + r) / 2, r, k * 2 + 1, L - 1, N - L + 1);
    int y = query(l, (l + r) / 2, k * 2, L - 1, N - L + 1) +
            query((l + r) / 2, r, k * 2 + 1, L - 1, j);
    ret = min(x, y);
  }
  if (j + L == N)
    mp[k] = ret;
  return ret;
}
int main() {
  cin >> N >> M;
  rep(i, M + 1) cin >> a[i];
  rep(i, M) cin >> b[i];
  cout << query(0, 1 << N, 1, N, 0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef pair<int, int> P;
map<int, int> mp;
int N, M, a[10001], b[10000];
int seginc(int l, int r) {
  int x = upper_bound(a, a + M + 1, l) - a - 1;
  if (r <= a[x + 1])
    return x;
  return -1;
}
int query(int l, int r, int k, int L, int j) {
  if (j - L == N + 2 && mp.count(k))
    return mp[k];
  int in = seginc(l, r);
  int ret = 0;
  if (in >= 0) {
    //		show(in);
    if (j == b[in])
      ret = (r - l) - 1;
    else if (b[in] > N - L)
      ret = (r - l) - (1 << (b[in] - (N - L) - 1));
    else
      ret = r - l;
  } else {
    int x = query(l, (l + r) / 2, k * 2, L - 1, j) +
            query((l + r) / 2, r, k * 2 + 1, L - 1, N - L + 1);
    int y = query(l, (l + r) / 2, k * 2, L - 1, N - L + 1) +
            query((l + r) / 2, r, k * 2 + 1, L - 1, j);
    ret = min(x, y);
  }
  if (j - L == N + 2)
    mp[k] = ret;
  //	printf("k=%d,  j=%d,   ret=%d\\n",k,j,ret);
  return ret;
}
int main() {
  cin >> N >> M;
  rep(i, M + 1) cin >> a[i];
  rep(i, M) cin >> b[i];
  cout << query(0, 1 << N, 1, N, 0) << endl;
}
Problem p01757 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int n, m;
int a[10010], b[10010];

map<P, int> dp[31];

int dfs(int l, int r, int t, int rank) {
  if (rank == t && dp[t].find(P(l, r)) != dp[t].end())
    return dp[t][P(l, r)];
  int idx = lower_bound(a, a + m + 1, r) - a;
  int res = -1;
  if (idx > 0 && a[idx - 1] <= l && r <= a[idx]) {
    if (b[idx - 1] >= rank + 1) {
      res = r - l - (1 << (b[idx - 1] - rank - 1));
    } else {
      if (b[idx - 1] == t)
        res = r - l - 1;
      else
        res = r - l;
    }
  } else {
    int mid = (l + r) / 2;
    res = min(dfs(l, mid, t, rank + 1) + dfs(mid, r, rank + 1, rank + 1),
              dfs(l, mid, rank + 1, rank + 1) + dfs(mid, r, t, rank + 1));
  }
  if (rank == t)
    return dp[t][P(l, r)] = res;
  else
    return res;
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, m + 1) scanf("%d", a + i);
  rep(i, m) scanf("%d", b + i);
  printf("%d\\n", dfs(0, 1 << n, 0, 0));
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int n, m;
int a[10010], b[10010];

map<P, int> dp[31];

int dfs(int l, int r, int t, int rank) {
  if (dp[t].find(P(l, r)) != dp[t].end())
    return dp[t][P(l, r)];
  int idx = lower_bound(a, a + m + 1, r) - a;
  int res = -1;
  if (idx > 0 && a[idx - 1] <= l && r <= a[idx]) {
    if (b[idx - 1] >= rank + 1) {
      res = r - l - (1 << (b[idx - 1] - rank - 1));
    } else {
      if (b[idx - 1] == t)
        res = r - l - 1;
      else
        res = r - l;
    }
  } else {
    int mid = (l + r) / 2;
    res = min(dfs(l, mid, t, rank + 1) + dfs(mid, r, rank + 1, rank + 1),
              dfs(l, mid, rank + 1, rank + 1) + dfs(mid, r, t, rank + 1));
  }
  if (r - l <= (1 << 18))
    return dp[t][P(l, r)] = res;
  else
    return res;
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, m + 1) scanf("%d", a + i);
  rep(i, m) scanf("%d", b + i);
  printf("%d\\n", dfs(0, 1 << n, 0, 0));
  return 0;
}
Problem p01761 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int N, Q, y, m, d, date[100];
string a, b, c, g, book[100], auth[100];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> book[i] >> auth[i] >> a;
    sscanf(a.c_str(), "%d/%02d/%02d", &y, &m, &d);
    date[i] = y * 10000 + m * 100 + d;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> a >> b >> c >> g;
    if (i)
      cout << endl;
    if (c != "*")
      sscanf(c.c_str(), "%d/%02d/%02d", &y, &m, &d);
    int d1 = y * 10000 + m * 100 + d;
    if (g != "*")
      sscanf(g.c_str(), "%d/%02d/%02d", &y, &m, &d);
    int d2 = y * 10000 + m * 100 + d;
    for (int i = 0; i < N; i++) {
      bool f1 = (a == "*" || book[i].find(a) != string::npos);
      bool f2 = (b == "*" || auth[i].find(b) != string::npos);
      bool f3 = (c == "*" || date[i] >= d1);
      bool f4 = (g == "*" || date[i] <= d2);
      if (f1 && f2 && f3 && f4)
        cout << book[i] << endl;
    }
  }
}

Error: -11

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int N, Q, y, m, d, date[100];
string a, b, c, g, book[100], auth[100];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> book[i] >> auth[i] >> a;
    sscanf(a.c_str(), "%d/%02d/%02d", y, m, d);
    date[i] = y * 10000 + m * 100 + d;
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> a >> b >> c >> g;
    if (i)
      cout << endl;
    if (c != "*")
      sscanf(c.c_str(), "%d/%02d/%02d", y, m, d);
    int d1 = y * 10000 + m * 100 + d;
    if (g != "*")
      sscanf(g.c_str(), "%d/%02d/%02d", y, m, d);
    int d2 = y * 10000 + m * 100 + d;
    for (int i = 0; i < N; i++) {
      bool f1 = (a == "*" || book[i].find(a) != string::npos);
      bool f2 = (b == "*" || auth[i].find(b) != string::npos);
      bool f3 = (c == "*" || date[i] >= d1);
      bool f4 = (g == "*" || date[i] <= d2);
      if (f1 && f2 && f3 && f4)
        cout << book[i] << endl;
    }
  }
}
Problem p01762 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define N_MAX 1001
#define INF (1e9)
using namespace std;
int n, c[N_MAX], cost[N_MAX][N_MAX];
bool visited[N_MAX];

int func(int u) {
  int sum = 0;
  visited[u] = true;
  for (int i = 0; i < n; i++) {
    if (cost[u][i] == INF || visited[i])
      continue;
    if (c[i])
      sum += cost[u][i];
    else
      sum += min(cost[u][i], func(i));
  }
  return sum;
}

int main() {
  int u, v, p;
  cin >> n;
  for (int i = 1; i < n; i++)
    cin >> c[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cost[i][j] = INF;
  for (int i = 1; i < n; i++)
    cin >> u >> v >> p, cost[u][v] = cost[v][u] = p;
  cout << func(0) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define N_MAX 1001
#define INF (1e9)
using namespace std;
int N, inc, c[N_MAX], u, v, p, cost[N_MAX][N_MAX], ans;
bool visited[N_MAX];
void func(int);

int main() {
  cin >> N;
  for (int i = 1; i < N; i++)
    cin >> inc, c[i] = inc;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      cost[i][j] = INF;
  for (int i = 0; i < N - 1; i++)
    cin >> u >> v >> p, cost[u][v] = cost[v][u] = p;
  func(0);
  cout << ans << endl;
  return 0;
}

void func(int u) {
  visited[u] = true;
  for (int i = 0; i < N; i++) {
    if (cost[u][i] == INF)
      continue;
    if (!c[i]) {
      int com = 0;
      for (int j = 0; j < N; j++) {
        if (cost[i][j] == INF)
          continue;
        if (c[j])
          com += cost[i][j];
      }
      if (cost[u][i] <= com)
        ans += cost[u][i];
      else {
        ans += com;
        for (int j = 0; j < N; j++) {
          if (cost[i][j] == INF || visited[j])
            continue;
          if (!c[j])
            func(i);
        }
      }
    } else
      ans += cost[u][i];
  }
}
Problem p01763 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

typedef long long int ll;
const ll INF = 1000000009LL;

vector<ll> comb;
vector<ll> A;
ll N;
ll M, D;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

void makeComb() {
  for (ll i = 0; i <= (1 << M); i++) {
    comb[i] = 1LL;
    for (ll j = 0; j < M; j++) {
      if (comb[i] >= INF)
        break;
      if ((i >> j) & 1)
        comb[i] = lcm(comb[i], A[j]);
    }
  }
  return;
}

int main() {
  cin >> N >> M >> D;
  comb = vector<ll>((1 << M) + 1, 0);
  A = vector<ll>(M);
  for (ll i = 0; i < M; i++)
    cin >> A[i];
  makeComb();
  ll pre = N;

  for (ll i = 0; i < D; i++) {
    ll ret = 0LL;
    vector<ll> R(M);
    ll exist = 0;
    for (ll j = 0; j < M; j++) {
      cin >> R[j];
      if (R[j] != -1)
        exist |= 1 << j;
    }
    bool update = false;

    ll step = 1LL;
    ll use = 0;
    for (ll j = 0; j <= comb[exist]; j += step) {
      ll f = 0;
      for (ll k = 0; k < M; k++) {
        if (R[k] != -1 && (j % A[k]) != R[k])
          f = 1;
        else if (R[k] != -1 && (j % A[k]) == R[k]) {
          if ((use >> k) & 1)
            continue;
          use |= (1 << k);
          step = lcm(step, A[k]);
        }
      }
      if (f)
        continue;
      if (pre < j)
        continue;
      update = true;
      ret = max(ret, ((pre - j) / comb[exist]) * comb[exist] + j);
    }
    if (!update) {
      // if(D != 2) while(1){}
      cout << -1 << endl;
      return 0;
    }
    pre = ret;
  }
  cout << pre << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

typedef long long int ll;
const ll INF = 1000000009LL;

vector<ll> comb;
vector<ll> A;
ll N;
ll M, D;

ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

void makeComb() {
  for (ll i = 0; i <= (1 << M); i++) {
    comb[i] = 1LL;
    for (ll j = 0; j < M; j++) {
      if (comb[i] >= INF)
        break;
      if ((i >> j) & 1)
        comb[i] = lcm(comb[i], A[j]);
    }
  }
  return;
}

int main() {
  cin >> N >> M >> D;
  comb = vector<ll>((1 << M) + 1, 0);
  A = vector<ll>(M);
  for (ll i = 0; i < M; i++)
    cin >> A[i];
  makeComb();
  ll pre = N;

  for (ll i = 0; i < D; i++) {
    ll ret = 0LL;
    vector<ll> R(M);
    ll exist = 0;
    for (ll j = 0; j < M; j++) {
      cin >> R[j];
      if (R[j] != -1)
        exist |= 1 << j;
    }
    bool update = false;
    for (ll j = comb[exist] - 1; j >= 0LL; j--) {
      ll f = 0;
      for (ll k = 0; k < M; k++) {
        if (R[k] != -1 && (j % A[k]) != R[k])
          f = 1;
      }
      if (f)
        continue;
      if (pre < j)
        continue;
      update = true;
      ret = max(ret, ((pre - j) / comb[exist]) * comb[exist] + j);
    }
    if (!update) {
      // if(D != 2) while(1){}
      cout << -1 << endl;
      return 0;
    }
    pre = ret;
  }
  cout << pre << endl;
  return 0;
}
Problem p01763 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> pr;
typedef vector<pr> vpr;
const int INF = 1 << 30;
ll lcm(ll a, ll b) {
  if (a % b == 0)
    return b;
  else
    return lcm(b, a % b);
}
ll GCD(ll a, ll b) { return (a * b) / lcm(a, b); }
int main() {
  ll N, M, D;
  ll divi[10];
  ll dived[100][10], gcd[10] = {};
  cin >> N >> M >> D;
  REP(i, M) { cin >> divi[i]; }
  REP(i, D)
  REP(j, M)
  cin >> dived[i][j];
  ll now = N;
  REP(i, D) {
    ll mn = 0, ngcd = 1;
    bool ans = false;
    REP(j, M) {
      if (dived[i][j] == -1)
        continue;
      if (!ans)
        mn = dived[i][j];
      ans = true;
      while (mn % divi[j] != dived[i][j] && now >= mn)
        mn += ngcd;
      ngcd = GCD(ngcd, divi[j]);
    }
    if (now < mn) {
      now = -1;
      break;
    }
    if (ans) {
      ll alpha = (now - mn) / ngcd;
      now = mn + alpha * ngcd;
    }
  }
  cout << now << endl;
  // your code goes here
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, b)
using namespace std;
typedef pair<int, int> pr;
typedef vector<pr> vpr;
const int INF = 1 << 30;
int lcm(int a, int b) {
  if (a % b == 0)
    return b;
  else
    return lcm(b, a % b);
}
int GCD(int a, int b) { return (a * b) / lcm(a, b); }
int main() {
  int N, M, D;
  int divi[10];
  int dived[100][10], gcd[10] = {};
  cin >> N >> M >> D;
  REP(i, M) {
    cin >> divi[i];
    if (i)
      gcd[i] = GCD(gcd[i - 1], divi[i]);
    else
      gcd[i] = divi[i];
  }
  REP(i, D)
  REP(j, M)
  cin >> dived[i][j];
  int now = N;
  REP(i, D) {
    int mn = 0;
    bool ans = false;
    REP(j, M) {
      if (dived[i][j] == -1)
        continue;
      ans = true;
      if (mn == 0)
        mn = dived[i][j];
      while (mn % divi[j] != dived[i][j] || N > mn)
        mn += gcd[i];
    }
    if (now < mn) {
      now = -1;
      break;
    }
    if (ans) {
      int alpha = (now - mn) / gcd[M - 1];
      now = mn + alpha * gcd[M - 1];
    }
  }
  cout << now << endl;
  // your code goes here
  return 0;
}
Problem p01763 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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, M, D;
vector<int> A;
vector<vector<int>> R;
void input() {
  cin >> N >> M >> D;
  A.resize(M);
  cin >> A;
  R.resize(D);
  for (int i = 0; i < D; i++) {
    R[i].resize(M);
    cin >> R[i];
  }
}

ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; }

bool check(int n, int d) {
  for (int i = 0; i < M; i++) {
    if (R[d][i] < 0)
      continue;
    if (n % A[i] != R[d][i])
      return false;
  }
  return true;
}

void solve() {

  ll x = N;
  for (int d = 0; d < D; d++) {
    int a = 0;
    int m = -1;
    ll L = 1;
    for (int i = 0; i < M; i++) {
      if (R[d][i] < 0)
        continue;
      L = lcm(L, A[i]);
      if (a < A[i]) {
        a = A[i];
        m = i;
      }
    }
    if (m < 0)
      continue;
    int n = R[d][m];
    for (;; n += A[m]) {
      if (n > x) {
        cout << -1 << endl;
        return;
      }
      if (check(n, d)) {
        break;
      }
    }
    x = (x - n) / L * L + n;
  }
  cout << x << endl;
}
} // namespace

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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, M, D;
vector<ll> A;
vector<vector<ll>> R;
void input() {
  cin >> N >> M >> D;
  A.resize(M);
  cin >> A;
  R.resize(D);
  for (int i = 0; i < D; i++) {
    R[i].resize(M);
    cin >> R[i];
  }
}

ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; }

bool check(ll n, int d) {
  for (int i = 0; i < M; i++) {
    if (R[d][i] < 0)
      continue;
    if (n % A[i] != R[d][i])
      return false;
  }
  return true;
}

void solve() {
  ll L = 1;
  for (int i = 0; i < M; i++)
    L = lcm(L, A[i]);

  ll x = N;
  for (int d = 0; d < D; d++) {
    int a = 0;
    int m = -1;
    for (int i = 0; i < M; i++) {
      if (R[d][i] < 0)
        continue;
      if (a < A[i]) {
        a = A[i];
        m = i;
      }
    }
    if (m < 0)
      continue;
    ll n = R[d][m];
    for (;; n += A[m]) {
      if (n > x) {
        cout << -1 << endl;
        return;
      }
      if (check(n, d)) {
        break;
      }
    }
    x = (x - n) / L * L + n;
  }
  cout << x << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}
Problem p01764 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int INF = 1e15;

int N, A[305], sum[305][305], dp[305][305];

void update(int a, int b) {

  for (int i = a; i <= b; i++)
    sum[a][b] += A[i];
}

int cal(int a, int b, int c, int d) {

  int X = sum[a][b], Y = sum[c][d];

  int xprev = 0, yprev = 0, cprev = 0;

  int res = 0, cnt = 15;

  while (cnt--) {

    if (xprev + yprev + cprev < 10)
      cprev = 0;
    else
      cprev = 1;

    int xi = X % 10, yi = Y % 10, ci = cprev;

    X /= 10, Y /= 10;

    res += xi * yi + ci;

    xprev = xi, yprev = yi;
  }

  return res;
}

signed main() {

  cin >> N;

  for (int i = 0; i < N; i++)
    cin >> A[i];

  for (int i = 0; i < N; i++) {

    for (int j = i; j < N; j++) {

      update(i, j);

      if (i != j)
        dp[i][j] = INF;
      else
        dp[i][j] = 0;
    }
  }

  for (int l = 1; l < N; l++) {

    for (int i = 0; i < N; i++) {

      if (i + l >= N)
        continue;

      for (int j = i; j < i + l; j++) {

        int add = cal(i, j, j + 1, i + l);

        dp[i][i + l] = min(dp[i][i + l], dp[i][j] + dp[j + 1][i + l] + add);
      }
    }
  }

  cout << dp[0][N - 1] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int INF = 1e9;

int N, A[305], sum[305][305], dp[305][305];

void update(int a, int b) {

  for (int i = a; i <= b; i++)
    sum[a][b] += A[i];
}

int cal(int a, int b, int c, int d) {

  int X = sum[a][b], Y = sum[c][d];

  int xprev = 0, yprev = 0, cprev = 0;

  int res = 0;

  while (1) {

    if (X == 0 || Y == 0)
      break;

    if (xprev + yprev + cprev < 10)
      cprev = 0;
    else
      cprev = 1;

    int xi = X % 10, yi = Y % 10, ci = cprev;

    X /= 10, Y /= 10;

    res += xi * yi + ci;
  }

  return res;
}

signed main() {

  cin >> N;

  for (int i = 0; i < N; i++)
    cin >> A[i];

  for (int i = 0; i < N; i++) {

    for (int j = i; j < N; j++) {

      update(i, j);

      if (i != j)
        dp[i][j] = INF;
      else
        dp[i][j] = 0;
    }
  }

  for (int l = 1; l < N; l++) {

    for (int i = 0; i < N; i++) {

      for (int j = i; j < i + l; j++) {

        int add = cal(i, j, j + 1, i + l);

        dp[i][i + l] = min(dp[i][i + l], dp[i][j] + dp[j + 1][i + l] + add);
      }
    }
  }

  cout << dp[0][N - 1] << endl;

  return 0;
}
Problem p01764 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#pragma warning(disable : 4996)
using namespace std;
int n, a[300], x[9], y[9], z[9], p[9], sum[300][301], dp[300][301];
int calc(int a, int b) {
  for (int i = 0; i < 9; i++) {
    x[i] = (a / p[i]) % 10;
    y[i] = (b / p[i]) % 10;
  }
  z[0] = 0;
  for (int i = 1; i < 9; i++) {
    if (x[i - 1] + y[i - 1] + z[i - 1] < 10) {
      z[i] = 0;
    } else {
      z[i] = 1;
    }
  }
  int ret = 0;
  for (int i = 0; i < 9; i++) {
    ret += x[i] * y[i] + z[i];
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  p[0] = 1;
  for (int i = 1; i < 9; i++)
    p[i] = p[i - 1] * 10;
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) {
    sum[i][i] = 0;
    for (int j = i + 1; j <= n; j++) {
      sum[i][j] = sum[i][j - 1] + a[j - 1];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i + 1 >= j) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = 999999999;
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j <= n - i; j++) {
      int l = j;
      int r = i + j;
      for (int k = l + 1; k < r; k++) {
        dp[l][r] =
            min(dp[l][r], dp[l][k] + dp[k][r] + calc(sum[l][k], sum[k][r]));
      }
    }
  }
  printf("%d\\n", dp[0][n]);
  return 0;
}

Error: -8

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int n, a[300], x[9], y[9], z[9], p[9], sum[300][301], dp[300][301];
int calc(int a, int b) {
  for (int i = 0; i < 9; i++) {
    x[i] = (a / p[i]) % 10;
    y[i] = (b / p[i]) % 10;
  }
  z[0] = 0;
  for (int i = 1; i < 9; i++) {
    if (x[i - 1] + y[i - 1] + z[i - 1] < 10) {
      z[i] = 1;
    } else {
      z[i] = 0;
    }
  }
  int ret = 0;
  for (int i = 0; i < 9; i++) {
    ret += x[i] * y[i] + z[i];
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  p[0] = 0;
  for (int i = 1; i < 9; i++)
    p[i] = p[i - 1] * 10;
  for (int i = 0; i < n; i++) {
    sum[i][i] = 0;
    for (int j = i + 1; j <= n; j++) {
      sum[i][j] = sum[i][j - 1] + a[j - 1];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) {
        dp[i][j] = 0;
      } else if (i + 1 == j) {
        dp[i][j] = a[i];
      } else {
        dp[i][j] = 999999999;
      }
    }
  }
  for (int i = 2; i < n; i++) {
    for (int j = 0; j <= n - i; j++) {
      int l = j;
      int r = i + j;
      for (int k = l + 1; k < r; k++) {
        dp[l][r] =
            min(dp[l][r], dp[l][k] + dp[k][r] + calc(sum[l][k], sum[k][r]));
      }
    }
  }
  printf("%d\\n", dp[0][n]);
  return 0;
}