#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }
inline long long tonum(string s) {long long v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string tostr(T x) {ostringstream sout;sout<<x;return sout.str();}



const int MAX = 320;
const ll INF = 1LL<<60;
long long dp[MAX][MAX];
int sum[MAX][MAX];

//int time(int a, int b) {
//    while (a.size() > b.size()) b += "0" + b;
//    while (a.size() < b.size()) a += "0" + a;
//    reverse(ALL(a));
//    reverse(ALL(b));
//    
//    int res = 0;
//    int c = 0;
//    for (int i = 0; i < a.size(); ++i) {
//        int aa = a[i] - '0';
//        int bb = b[i] - '0';
//        res += aa*bb + c;
//        if (aa + bb + c >= 10) c = 1;
//    }
//    
//    return res;
//}

int time(int a, int b) {
    int res = 0;
    int c = 0;
    for (int i = 0; i < 10; ++i) {
        int ar = a % 10;
        int br = b % 10;
        if (ar + br + c >= 10) c = 1;
        else c = 0;
        res += ar * br + c;
        a /= 10;
        b /= 10;
    }
    
    return res;
}


int N;
int a[MAX];

int main() {
    while (cin >> N) {
        for (int i = 0; i < N; ++i) cin >> 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 < MAX; ++i) dp[i][i] = dp[i][i+1] = 0;
        
        for (int i = 0; i < N; ++i) {
            for (int j = i; j <= N; ++j) {
                int num = 0;
                for (int k = i; k < j; ++k) num += a[k];
                sum[i][j] = num;
            }
        }
        
        for (int bet = 2; bet <= N; ++bet) {
            for (int i = 0; i + bet <= N; ++i) {
                int j = i + bet;
                
                for (int k = i+1; k <= j-1; ++k) {
                    int na = sum[i][k], nb = sum[k][j];
                    int tt = time(na, nb);
                    
                    chmin(dp[i][j], dp[i][k] + dp[k][j] + tt);
                }
                
                //cout << i << ", " << j << " : " << dp[i][j] << endl;
            }
        }
         
        cout << dp[0][N] << endl;
    }
    return 0;
}


Measured memory_bytes: 2336