Problem:
E : Sigma / Σ
Problem
小学生のづあい君は同じ足し算にも得意不得意があり、 0 以上の整数
X
,
Y
に対して、
X
+
Y
を計算するのにかかる時間(秒)が次のように決まる。
1 の位から順番に計算していく。
10 進数表記で 10
i
位の計算には
x
i
×
y
i
+
c
i
秒かかる。
x
i
は
X
の10進数表記で 10
i
の位の数である。
y
i
は
Y
の10進数表記で 10
i
の位の数である。
ただし
i
が
X
の桁数より大きくなる時は
x
i
= 0 とする。
Y
についても同様である。
c
i
は、
i
= 0 または
x
i-1
+
y
i-1
+
c
i-1
< 10 ならば 0、 そうでなければ 1 である。
0 以上の整数 i についての
x
i
×
y
i
+
c
i
の総和が
X
+
Y
の計算にかかる時間である。
ある日、づあい君の先生は長さ
N
の整数列
a
0
, ... ,
a
N-1
の和を計算する宿題を出した。づあい君は数列の順番を並び替えることはできないが、足し算の式に括弧を付けることにより、足し算の順番を自由に変えることができる。例えば
N
= 4 の場合、順番は次の 5 通りある。
((
a
0
+
a
1
) +
a
2
) +
a
3
(
a
0
+
a
1
) + (
a
2
+
a
3
)
(
a
0
+ (
a
1
+
a
2
)) +
a
3
a
0
+ ((
a
1
+
a
2
) +
a
3
)
a
0
+ (
a
1
+ (
a
2
+
a
3
))
それぞれの括弧 (
x
+
y
) の中で上で説明した時間がかかるとすると、総和を求めるのにかかる時間は最小で何秒になるだろうか。
Input
入力は次のような形式で与えられる。
N
a
0
a
1
...
a
N-1
Constraints
入力はすべて整数である。
2 ≦
N
≦ 300
0 ≦
a
i
≦ 1,000,000
Output
答えの秒数を1行で出力せよ。
Samples
Sample Input 1
3
1 2 3
Sample Output 1
11
次のように計算すると最も速い。
2 + 3 = 5 ( 2 × 3 = 6 秒かかる)
1 + 5 = 6 ( 1 × 5 = 5 秒かかる)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
  #include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template<class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template<class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t) fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

int calc(int x, int y){
  string sx = to_string(x), sy = to_string(y);
  reverse(sx.begin(), sx.end());
  reverse(sy.begin(), sy.end());
  int res = 0, nx = sx.size(), ny = sy.size(), c = 0, kuri = 0;
  for(int i = 0; i < max(nx, ny); i++){
    int tx = (nx-1 < i ? 0 : (int)(sx[i] - '0'));
    int ty = (ny-1 < i ? 0 : (int)(sy[i] - '0'));
    c += tx + ty;
    res += tx * ty + kuri;
    if(c >= 10) kuri = 1;
    else kuri = 0;

    c = kuri;
  }
  return res + kuri;
}

int f(int x, int y, int c){
  if(not x and not y) return c;
  int a = x % 10, b = y % 10;
  return a * b + c + f(x/10, y/10, (a+b+c)/10);
}

signed main(){

  int n; cin >> n;
  vector<int> a(n);
  for(int i = 0; i < n; i++){
    cin >> a[i];
  }

  vector<int> imos(n+1, 0);
  for(int i = 0; i < n; i++){
    imos[i+1] = imos[i] + a[i];
  }

  auto dp = make_vec<int>(n, n);
  fill(dp, INF);
  for(int i = 0; i < n; i++) dp[i][i] = 0;
  for(int l = 1; l <= n; l++){
    for(int i = 0; i <= n-l; i++){
      int j = i + l - 1;
      for(int k = i; k < j; k++){
        int x = imos[k+1] - imos[i];
        int y = imos[j+1] - imos[k+1];
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + calc(x, y));
        //dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + f(x, y, 0));
      }
    }
  }
  cout << dp[0][n-1] << endl;

  return 0;
}
accuracy: 1
time_ms: 1000
memory_kb: 3688
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
  #include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template<class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}
template<class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t) fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

int calc(int x, int y){
  string sx = to_string(x), sy = to_string(y);
  int res = 0, nx = sx.size(), ny = sy.size(), c = 0;
  for(int i = 0; i < max(nx, ny); i++){
    int tx = (nx <= i ? 0 : (int)(sx[i] - '0'));
    int ty = (ny <= i ? 0 : (int)(sy[i] - '0'));
    c += tx + ty;
    int t = tx * ty + (c >= 10);
    if(c >= 10) c -= 10;
    res += t;
  }
  return res + (c >= 10);
}

signed main(){

  int n; cin >> n;
  vector<int> a(n);
  for(int i = 0; i < n; i++){
    cin >> a[i];
  }

  vector<int> imos(n+1, 0);
  for(int i = 0; i < n; i++){
    imos[i+1] = imos[i] + a[i];
  }

  auto dp = make_vec<int>(n, n);
  fill(dp, INF);
  for(int i = 0; i < n; i++) dp[i][i] = 0;
  for(int l = 1; l <= n; l++){
    for(int i = 0; i <= n-l; i++){
      int j = i + l - 1;
      for(int k = i; k < j; k++){
        int x = imos[k+1] - imos[i];
        int y = imos[j+1] - imos[k+1];
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + calc(x, y));
      }
    }
  }
  cout << dp[0][n-1] << endl;

  return 0;
}
accuracy: 0.038461538461538464
time_ms: 560
memory_kb: 3408
final_rank: 15

code_snippet: #pragma GCC optimize ("O2")
#pragma GCC optimize ("tree-vectorize")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define FFOR(i, a, b) FOR(i, a, b+1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n+1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr<<__LINE__<<' '<<#x<<"="<<(x)<<endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii =  pair<int, int>;
using pll =  pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b){if(a<b){a = b; return true;} return false;}
template <typename T> inline bool chmin(T &a, T b){if(a>b){a = b; return true;} return false;}

const double PI = acos(-1);
constexpr lint ten(int n) {return n==0 ? 1 : ten(n-1)*10;}

int f(int x, int y, int c){
    if(not x and not y) return c;
    int a = x % 10, b = y % 10;
    return a * b + c + f(x/10, y/10, (a+b+c)/10);
}

int calc(int x, int y){
    string sx = to_string(x), sy = to_string(y);
    int res = 0, nx = sx.size(), ny = sy.size(), c = 0;
    for(int i = 0; i < max(nx, ny); i++){
        int tx = (nx <= i ? 0 : (int)(sx[i] - '0'));
        int ty = (ny <= i ? 0 : (int)(sy[i] - '0'));
        int t = tx * ty + c;
        c = 0;
        if(t >= 10){
            c = 1;
            t -= 10;
        }
        res += t;
    }
    return res;
}

class Task{
public:
    void solve(istream& in, ostream& out){
        int N;
        in>>N;
        vc<int> A(N), pref(N+1, 0);
        REP(i, N){
            in>>A[i];
            pref[i+1] += pref[i] + A[i];
        }
        const int INF = ten(9);
        vvc<int> dp(N, vc<int>(N, INF));
        REP(i, N) dp[i][i] = 0;
        FOR(l, 2, N+1){
            REP(i, N-l+1){
                int j = i + l - 1;
                FOR(k, i, j){
                    int x = pref[k+1] - pref[i];
                    int y = pref[j+1] - pref[k+1];
                    chmin(dp[i][j], dp[i][k]+dp[k+1][j]+f(x, y, 0));
                }
            }
        }
        out<<dp[0][N-1]<<'\n';
#ifdef DEBUG
        out<<f(456, 545, 0)<<'\n';
#endif
    }
};

int main(){
    istream& in(cin);
    ostream& out(cout);
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    Task solver;
    solver.solve(in, out);
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3304
final_rank: 1

code_snippet: #pragma GCC optimize ("O2")
#pragma GCC optimize ("tree-vectorize")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define FFOR(i, a, b) FOR(i, a, b+1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n+1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr<<__LINE__<<' '<<#x<<"="<<(x)<<endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii =  pair<int, int>;
using pll =  pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b){if(a<b){a = b; return true;} return false;}
template <typename T> inline bool chmin(T &a, T b){if(a>b){a = b; return true;} return false;}

const double PI = acos(-1);
constexpr lint ten(int n) {return n==0 ? 1 : ten(n-1)*10;}

int f(int x, int y, int c){
    if(not x or not y) return 0;
    int a = x % 10, b = y % 10;
    return a * b + c + f(x/10, y/10, (a+b+c)/10);
}

int calc(int x, int y){
    string sx = to_string(x), sy = to_string(y);
    int res = 0, nx = sx.size(), ny = sy.size(), c = 0;
    for(int i = 0; i < max(nx, ny); i++){
        int tx = (nx <= i ? 0 : (int)(sx[i] - '0'));
        int ty = (ny <= i ? 0 : (int)(sy[i] - '0'));
        int t = tx * ty + c;
        c = 0;
        if(t >= 10){
            c = 1;
            t -= 10;
        }
        res += t;
    }
    return res;
}

class Task{
public:
    void solve(istream& in, ostream& out){
        int N;
        in>>N;
        vc<int> A(N), pref(N+1, 0);
        REP(i, N){
            in>>A[i];
            pref[i+1] += pref[i] + A[i];
        }
        const int INF = ten(9);
        vvc<int> dp(N, vc<int>(N, INF));
        REP(i, N) dp[i][i] = 0;
        FOR(l, 2, N+1){
            REP(i, N-l+1){
                int j = i + l - 1;
                FOR(k, i, j){
                    int x = pref[k+1] - pref[i];
                    int y = pref[j+1] - pref[k+1];
                    chmin(dp[i][j], dp[i][k]+dp[k+1][j]+f(x, y, 0));
                }
            }
        }
        out<<dp[0][N-1]<<'\n';
#ifdef DEBUG
        out<<f(456, 545, 0)<<'\n';
#endif
    }
};

int main(){
    istream& in(cin);
    ostream& out(cout);
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    Task solver;
    solver.solve(in, out);
    return 0;
}
accuracy: 0.038461538461538464
time_ms: 60
memory_kb: 3352
final_rank: 11

code_snippet: #pragma GCC optimize ("O2")
#pragma GCC optimize ("tree-vectorize")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define FFOR(i, a, b) FOR(i, a, b+1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n+1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr<<__LINE__<<' '<<#x<<"="<<(x)<<endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii =  pair<int, int>;
using pll =  pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b){if(a<b){a = b; return true;} return false;}
template <typename T> inline bool chmin(T &a, T b){if(a>b){a = b; return true;} return false;}

const double PI = acos(-1);
constexpr lint ten(int n) {return n==0 ? 1 : ten(n-1)*10;}

int f(int x, int y, int c){
    if(not x or not y) return 0;
    int a = x % 10, b = y % 10;
    return a * b + c + f(x/10, y/10, (a+b)/10);
}

int calc(int x, int y){
    string sx = to_string(x), sy = to_string(y);
    int res = 0, nx = sx.size(), ny = sy.size(), c = 0;
    for(int i = 0; i < max(nx, ny); i++){
        int tx = (nx <= i ? 0 : (int)(sx[i] - '0'));
        int ty = (ny <= i ? 0 : (int)(sy[i] - '0'));
        int t = tx * ty + c;
        c = 0;
        if(t >= 10){
            c = 1;
            t -= 10;
        }
        res += t;
    }
    return res;
}

class Task{
public:
    void solve(istream& in, ostream& out){
        int N;
        in>>N;
        vc<int> A(N), pref(N+1, 0);
        REP(i, N){
            in>>A[i];
            pref[i+1] += pref[i] + A[i];
        }
        const int INF = ten(9);
        vvc<int> dp(N, vc<int>(N, INF));
        REP(i, N) dp[i][i] = 0;
        FOR(l, 2, N+1){
            REP(i, N-l+1){
                int j = i + l - 1;
                FOR(k, i, j){
                    int x = pref[k+1] - pref[i];
                    int y = pref[j+1] - pref[k+1];
                    chmin(dp[i][j], dp[i][k]+dp[k+1][j]+calc(x, y));
                }
            }
        }
        out<<dp[0][N-1]<<'\n';
#ifdef DEBUG
        out<<f(81, 19, 0)<<'\n';
#endif
    }
};

int main(){
    istream& in(cin);
    ostream& out(cout);
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    Task solver;
    solver.solve(in, out);
    return 0;
}
accuracy: 0.038461538461538464
time_ms: 570
memory_kb: 3360
final_rank: 14

code_snippet: #pragma GCC optimize ("O2")
#pragma GCC optimize ("tree-vectorize")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define FFOR(i, a, b) FOR(i, a, b+1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n+1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr<<__LINE__<<' '<<#x<<"="<<(x)<<endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii =  pair<int, int>;
using pll =  pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b){if(a<b){a = b; return true;} return false;}
template <typename T> inline bool chmin(T &a, T b){if(a>b){a = b; return true;} return false;}

const double PI = acos(-1);
constexpr lint ten(int n) {return n==0 ? 1 : ten(n-1)*10;}

int f(int x, int y, int c){
    if(not x or not y) return 0;
    int a = x % 10, b = y % 10;
    return a * b + c + f(x/10, y/10, (a+b)/10);
}

class Task{
public:
    void solve(istream& in, ostream& out){
        int N;
        in>>N;
        vc<int> A(N), pref(N+1, 0);
        REP(i, N){
            in>>A[i];
            pref[i+1] += pref[i] + A[i];
        }
        const int INF = ten(9);
        vvc<int> dp(N, vc<int>(N, INF));
        REP(i, N) dp[i][i] = 0;
        FOR(l, 2, N+1){
            REP(i, N-l+1){
                int j = i + l - 1;
                FOR(k, i, j){
                    int x = pref[k+1] - pref[i];
                    int y = pref[j+1] - pref[k+1];
                    chmin(dp[i][j], dp[i][k]+dp[k+1][j]+f(x, y, 0));
                }
            }
        }
        out<<dp[0][N-1]<<'\n';
#ifdef DEBUG
        out<<f(999, 999, 0)<<'\n';
#endif
    }
};

int main(){
    istream& in(cin);
    ostream& out(cout);
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    Task solver;
    solver.solve(in, out);
    return 0;
}
accuracy: 0.038461538461538464
time_ms: 60
memory_kb: 3220
final_rank: 10

code_snippet: #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl;

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) {
  oss << "\b\b \b";
}

template<typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template<typename... T>
string _tostr(T &&... args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1) oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1) oss << "}";
  return oss.str();
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 410

int N, A[SIZE];
ll dp[SIZE][SIZE], sum[SIZE];

ll calc(ll A, ll B) {
  ll res = 0, c = 0;

  while (A || B || c) {
    res += (A % 10) * (B % 10) + c;
    c = ((A % 10) + (B % 10) + c) / 10;
    A /= 10;
    B /= 10;
  }

  return res;
}

int main() {
  scanf("%d", &N);

  for (int i = 0; i < N; i++) {
    scanf("%d", A + i);
    sum[i + 1] = sum[i] + A[i];
  }

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

    dp[i][i] = 0;
  }

  for (int w = 2; w <= N; w++) {
    for (int l = 0; l + w <= N; l++) {
      int r = l + w - 1;
      for (int c = l; c < r; c++) {
        ll A = sum[c + 1] - sum[l];
        ll B = sum[r + 1] - sum[c + 1];
        dp[l][r] = min(dp[l][r], dp[l][c] + dp[c + 1][r] + calc(A, B));
      }
    }
  }

  printf("%lld\n", dp[0][N - 1]);

  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 4172
final_rank: 5

code_snippet: #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl;

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) {
  oss << "\b\b \b";
}

template<typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template<typename... T>
string _tostr(T &&... args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1) oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1) oss << "}";
  return oss.str();
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 410

int N, A[SIZE];
ll dp[SIZE][SIZE], sum[SIZE];

ll calc(ll A, ll B) {
  ll res = 0, c = 0;

  while (A || B || c) {
    res += (A % 10) * (B % 10) + c;
    c = ((A % 10) + (B % 10)) / 10;
    A /= 10;
    B /= 10;
  }

  return res;
}

int main() {
  scanf("%d", &N);

  for (int i = 0; i < N; i++) {
    scanf("%d", A + i);
    sum[i + 1] = sum[i] + A[i];
  }

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

    dp[i][i] = 0;
  }

  for (int w = 2; w <= N; w++) {
    for (int l = 0; l + w <= N; l++) {
      int r = l + w - 1;
      for (int c = l; c < r; c++) {
        ll A = sum[c + 1] - sum[l];
        ll B = sum[r + 1] - sum[c + 1];
        dp[l][r] = min(dp[l][r], dp[l][c] + dp[c + 1][r] + calc(A, B));
      }
    }
  }

  printf("%lld\n", dp[0][N - 1]);

  return 0;
}
accuracy: 0.038461538461538464
time_ms: 70
memory_kb: 3944
final_rank: 12

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n; cin >> n;
  vector<int> a(n); REP(i, n) cin >> a[i];
  vector<int> sum(n + 1, 0);
  REP(i, n) sum[i + 1] = sum[i] + a[i];
  vector<vector<int>> dp(n, vector<int>(n, INF));
  REP(i, n) dp[i][i] = 0;
  FOR(len, 2, n + 1) for (int i = 0; i + len - 1 < n; ++i) {
    int j = i + len - 1;
    FOR(k, i, j) {
      string l = to_string(sum[k + 1] - sum[i]), r = to_string(sum[j + 1] - sum[k + 1]);
      reverse(ALL(l));
      reverse(ALL(r));
      int l_len = l.length(), r_len = r.length(), t = dp[i][k] + dp[k + 1][j];
      bool carry = false;
      for (int idx = 0; idx < l_len || idx < r_len; ++idx) {
        int x = (idx < l_len ? l[idx] : '0') - '0', y = (idx < r_len ? r[idx] : '0') - '0';
        t += x * y + carry;
        carry = x + y + carry >= 10;
      }
      chmin(dp[i][j], t + carry);
    }
  }
  cout << dp[0][n - 1] << '\n';
  return 0;
}
accuracy: 1
time_ms: 940
memory_kb: 3428
final_rank: 6

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n; cin >> n;
  vector<int> a(n); REP(i, n) cin >> a[i];
  vector<int> sum(n + 1, 0);
  REP(i, n) sum[i + 1] = sum[i] + a[i];
  vector<vector<int>> dp(n, vector<int>(n, INF));
  REP(i, n) dp[i][i] = 0;
  FOR(len, 2, n + 1) REP(i, n) {
    int j = i + len - 1;
    if (j >= n) break;
    FOR(k, i, j) {
      string l = to_string(sum[k + 1] - sum[i]), r = to_string(sum[j + 1] - sum[k + 1]);
      reverse(ALL(l));
      reverse(ALL(r));
      int t = dp[i][k] + dp[k + 1][j], m = l.length();
      bool carry = false;
      for (int idx = 0; idx < l.length() && idx < r.length(); ++idx) {
        int x = l[idx] - '0', y = r[idx] - '0';
        t += x * y + carry;
        carry = x + y + carry >= 10;
      }
      chmin(dp[i][j], t + carry);
    }
  }
  cout << dp[0][n - 1] << '\n';
  return 0;
}
accuracy: 0.038461538461538464
time_ms: 530
memory_kb: 3400
final_rank: 13

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n; cin >> n;
  vector<int> a(n); REP(i, n) cin >> a[i];
  vector<int> sum(n + 1, 0);
  REP(i, n) sum[i + 1] = sum[i] + a[i];
  vector<vector<ll>> dp(n, vector<ll>(n, LINF));
  REP(i, n) dp[i][i] = 0;
  FOR(len, 2, n + 1) REP(i, n) {
    int j = i + len - 1;
    if (j >= n) break;
    FOR(k, i, j) {
      string l = to_string(sum[k + 1] - sum[i]), r = to_string(sum[j + 1] - sum[k + 1]);
      reverse(ALL(l));
      reverse(ALL(r));
      while (l.length() < r.length()) l += '0';
      while (r.length() < l.length()) r += '0';
      l += '0';
      r += '0';
      reverse(ALL(l));
      reverse(ALL(r));
      ll t = dp[i][k] + dp[k + 1][j];
      int m = l.length();
      bool carry = false;
      REP(idx, m) {
        int x = l[idx] - '0', y = r[idx] - '0';
        t += x * y + carry;
        carry = x + y + carry >= 10;
      }
      chmin(dp[i][j], t);
    }
  }
  cout << dp[0][n - 1] << '\n';
  return 0;
}
accuracy: 0.038461538461538464
time_ms: 760
memory_kb: 3404
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int N = 334;
const ll INF = LLONG_MAX/3;

int n,a[N],s[N];

ll f(ll x, ll y){
    int c = 0;
    ll ret = 0;
    while(x>0 || y>0 || c>0){
        int xx = x%10, yy = y%10;
        ret += xx*yy + c;

        x /= 10;
        y /= 10;
        c = (xx+yy+c>=10);
    }
    return ret;
}

ll dp[N][N];
ll dfs(int l, int r){
    if(dp[l][r]>=0) return dp[l][r];
    if(r-l == 1) return 0;

    ll ret = INF;
    for(int i=l+1; i<r; ++i){
        ll X = s[i]-s[l];
        ll Y = s[r]-s[i];
        ret = min(ret, dfs(l,i)+dfs(i,r)+f(X,Y));
    }
    return dp[l][r] = ret;
}

int main(){
    scanf(" %d", &n);
    rep(i,n) scanf(" %d", &a[i]);

    rep(i,n) s[i+1] += s[i]+a[i];
    memset(dp, -1, sizeof(dp));
    printf("%lld\n", dfs(0,n));
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 4116
final_rank: 4

code_snippet: #include <string>
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
using int64 = long long int;

const int64 INF = 1LL << 60;
int64 dp[310][310];
int main() {
  int N; cin >> N;
  vector<int> A(N), S(N+1);
  for(int i=0; i<N; i++) {
    cin >> A[i];
    S[i+1] = S[i] + A[i];
  }

  fill(dp[0], dp[N+1], INF);
  for(int i=0; i<N; i++) {
    dp[i][i] = dp[i][i+1] = 0;
  }
  for(int d=2; d<=N; d++) {
    for(int l=0; l<N-d+1; l++) {
      int r = l + d;
      for(int k=l+1; k<r; k++) {
        int vl = S[k] - S[l];
        int vr = S[r] - S[k];

        int64 c = 0, cost = 0;
        while(vl > 0 or vr > 0 or c > 0) {
          int x = vl % 10, y = vr % 10;
          int nc = (x + y + c >= 10);
          cost += x * y + c;
          c = nc;
          vl /= 10, vr /= 10;
        }

        dp[l][r] = min(dp[l][r], dp[l][k] + dp[k][r] + cost);
      }
    }
  }
  cout << dp[0][N] << endl;
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3820
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
const Int MAX = 333;
Int dp[333][333];
const Int INF = 1e15;

void chmin(Int &x,Int y){if(x>y)x=y;};
void chmax(Int &x,Int y){if(x<y)x=y;};

signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n;
  cin>>n;
  vector<Int> a(n);
  for(Int i=0;i<n;i++) cin>>a[i];
  vector<Int> s(n+1);
  for(Int i=0;i<n;i++) s[i+1]=s[i]+a[i];
  
  for(Int i=0;i<MAX;i++)
    for(Int j=0;j<MAX;j++)
      dp[i][j]=INF;

  for(Int i=0;i<n;i++) dp[i][i+1]=0;


  auto calc=[&](Int x,Int y){
    Int res=0,c=0;
    while(x||y||c){
      res+=(x%10)*(y%10)+c;
      c=(x%10)+(y%10)+c>=10;
      x/=10;y/=10;
    }
    return res;
  };
  
  for(Int l=2;l<=n;l++){
    for(Int i=0;i+l<=n;i++){
      Int k=i+l;
      for(Int j=i+1;j<k;j++){
	chmin(dp[i][k],dp[i][j]+dp[j][k]+calc(s[j]-s[i],s[k]-s[j]));
      }
    }    
  }

  cout<<dp[0][n]<<endl;
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 4040
final_rank: 3

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

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=0;
  while(1){

    if(X==0&&Y==0) cnt++;

    if( xprev + yprev + cprev < 10 ) cprev = 0;
    else cprev = 1;
    
    int xi = X % 10, yi = Y % 10, ci = cprev;
    
    xprev = xi, yprev = yi;
    
    X /= 10, Y /= 10;
    
    res += xi * yi + ci;

    if(cnt)break;
    
  }
  
  return res;
}

int dfs(int l,int r){
  if(l==r)return 0;
  if(dp[l][r]!=-1)return dp[l][r];
  int res=1e9;
  for(int i=l+1;i<=r;i++){
    res=min(res,dfs(l,i-1)+dfs(i,r)+cal(l,i-1,i,r));
  }
  return dp[l][r]=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 );
      
    }
    
  }
  memset(dp,-1,sizeof(dp));
  cout<<dfs(0,N-1)<<endl;
}
accuracy: 1
time_ms: 180
memory_kb: 4564
final_rank: 7

code_snippet: #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;
    
    xprev = xi, yprev = yi;
    
    X /= 10, Y /= 10;
    
    res += xi * yi + ci;
    
  }
  
  return res;
}

int dfs(int l,int r){
  if(l==r)return 0;
  if(dp[l][r]!=-1)return dp[l][r];
  int res=1e15;
  for(int i=l+1;i<=r;i++){
    res=min(res,dfs(l,i-1)+dfs(i,r)+cal(l,i-1,i,r));
  }
  return dp[l][r]=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 );
      
    }
    
  }
  memset(dp,-1,sizeof(dp));
  
  /*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<<dfs(0,N-1)<<endl;
  
  return 0;
}
accuracy: 0.038461538461538464
time_ms: 60
memory_kb: 4444
final_rank: 20

code_snippet: #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;
}
accuracy: 1
time_ms: 190
memory_kb: 4560
final_rank: 8

code_snippet: #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;
    
    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;
}
accuracy: 0.038461538461538464
time_ms: 60
memory_kb: 4292
final_rank: 18

code_snippet: #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;
    
    xprev = xi, yprev = yi;
    
    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++){
      
      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;
}
accuracy: 0.038461538461538464
time_ms: 60
memory_kb: 4180
final_rank: 16

code_snippet: #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++){
      
      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;
}
accuracy: 0.038461538461538464
time_ms: 50
memory_kb: 4292
final_rank: 17