#pragma GCC diagnostic ignored "-Wunused-variable" 
#include <bits/stdc++.h>
using namespace std;

#define BOOST
#ifdef BOOST
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/range/adaptor/reversed.hpp>
using namespace boost;
using ml = boost::multiprecision::cpp_int;
using md = boost::multiprecision::cpp_dec_float_100;
#define forir(i, ...) if(ll i=(ll)v.size())for(__VA_ARGS__)if(i--,1)
#define eachr(i, e, c) forir(i, auto &&e: adaptors::reverse(c))
#endif

/***** type *****/
using ll = long long;
using ld = long double;
using pll = pair<long long, long long>;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;
template <class T> using vvvt = vector<vector<vector<T>>>;
/***** define *****/
#define all(c) (c).begin(), (c).end()            // begin to end
#define coutld cout << fixed << setprecision(10) // cout double
#define output(x) do{cout<<x<<endl;exit(0);}while(0)
#define rep(i, b, e) for (ll i = b; i < e; i++)  // repeat
#define repr(i, b, e) for (ll i = b; e < i; i--) // repeat reverse
#define fori(i, ...) if (ll i = -1) for(__VA_ARGS__) if (i++, 1)
#define each(i, e, c) fori (i, auto&& e: c)      // for each
/***** const value *****/
#define llong_max 9223372036854775807            // 9 * 10^18
#define ldbl_max 1.79769e+308                    // 1.7 * 10^308
#define pi 3.1415926535897932                    // 3.14 ...
/***** lambda *****/
auto Ceil = [] // if (a % b != 0) return a / b + 1;
(auto x) { return (ll)ceil(x); };
auto Count = [] // long long count value
(auto b, auto e, auto x) { return (ll)count(b, e, x); };
auto CtoL = [] // char to number
(auto c) { return (ll)c - (ll)'0'; };
auto LtoC = [] // number to char
(auto n) { return (char)('0' + n); };
auto Pow = [] // long long pow
(auto a, auto b) { return (ll)pow(a, b); };
auto Pow2 = [] // long long pow2
(auto n) { return (1LL << n); };
auto Pow10 = [] // long long pow10
(auto n) { return (ll)pow(10, n); };
auto Size = []  // long long collection size
(auto& c) { return (ll)(c).size(); };
auto Sum = [] // long long accumulate
(auto b, auto e) { return accumulate(b, e, 0LL); };

/***** template *****/
template <class T> void MakeVVT
(ll ys, ll xs, vvt<T>& v, T fill = T())
{	// vector<vector<T>> resize + fill
	v.resize(ys); rep(y, 0, ys) v[y].resize(xs, fill);
}
template <class T> void MakeVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fill = T())
{	// vector<vector<vector<T>>> resize + fill
	v.resize(zs); rep(z, 0, zs) MakeVVT(ys, xs, v[z], fill);
}
template <class T> void InputVT
(ll xs, vt<T>& v, T fix = T())
{	// input vector<T> (T != struct) + fix
	v.resize(xs); rep(i, 0, xs) { cin >> v[i]; v[i] += fix; }
}
template <class T> void InputVVT
(ll ys, ll xs, vvt<T>& v, T fix = T())
{	// input vector<vector<T>> (T != struct) + fix
	MakeVVT(ys, xs, v, fix);
	rep(y, 0, ys) rep(x, 0, xs) { cin >> v[y][x]; v[y][x] += fix; }
}
template <class T> void InputVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fix = T())
{	// input vector<vector<vector<T>>> (T != struct) + fix
	v.resize(zs); rep(z, 0, zs) InputVVT(ys, xs, v[z], fix);
}

/**************************************/
/********** BEGIN OF NYA LIB **********/
/**************************************/

namespace NyaGadget {}

/**************************************/
/*********** END OF NYA LIB ***********/
/**************************************/

using namespace NyaGadget;
//using mll = NT_ModLL< 1000000007 >;
//using mll = ModLL< 998244353 >;

int main(void)
{
	ll M; cin >> M;
	vt<pll> dc(M); each(i, e, dc) cin >> e.first >> e.second;

	ll sum = 0;
	ll keta = 0;
	each(i, e, dc)
	{
		sum += e.first * e.second;
		keta += e.second;
	}
	cout << (sum - 1) / 9 + keta - 1;
	return 0;
}

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>

using namespace std;

#define sz(x) (int) x.size()
#define unique(x) x.erase(unique(x.begin(), x.end()), x.end())
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x)) 
#define y1 aksjdaskdjksjfksdjf
#define left kdnvldvoiwejifejg
#define right lkdsjflksdjfdjfk
#define prev asdasfsadjkjsdfjs
#define tm aklsjdasjdasjdkdjjf
#define lcm hddalkwjeidjjhasda
#define random_shuffle asdasdd

const int MAXN = 2 * 105000;
const long long INF = 1e15;

int n;
int a[MAXN];
// long long dpl[MAXN], dpr[MAXN];
// pair<long long, long long> pl[MAXN], pr[MAXN];
long long sum;
long long total;
multiset <long long> st1, st2;

int main() { 

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    total += a[i];
  }

  for (int i = n - 1; i > 0; i--) {
    sum += a[i];
    st2.insert(sum);
  }

  sum = 0;
  long long ans = INF;
  for (int i = 0; i + 1 < n; i++) {
    sum += a[i];
    if (st2.find(total - sum) != st2.end()) {
      st2.erase(st2.find(total - sum));
    }

    if (sz(st1) && sz(st2)) {
      vector<long long> v1, v2; 
      auto it = st1.lower_bound(sum / 2);
      if (it != st1.end()) {
        v1.push_back(*it);
      }
      if (it != st1.begin()) {
        it--;
        v1.push_back(*it);
      }

      it = st2.lower_bound((total - sum) / 2);
      if (it != st2.end()) {
        v2.push_back(*it);
      }
      if (it != st2.begin()) {
        it--;
        v2.push_back(*it);
      }

      for (auto x : v1) {
        for (auto y : v2) {
          long long mn = min(x, y);
          long long mx = max(x, y);
          mn = min(mn, min(sum - x, total - sum - y));
          mx = max(mx, max(sum - x, total - sum - y));
          ans = min(ans, mx - mn);
        }
      }
    }

    st1.insert(sum);
  }

  // for (int i = 0; i < n; i++) {
  //   sum += a[i];
  //   dpl[i] = INF;
  //   if (sz(st)) {
  //     auto it = st.lower_bound(sum / 2);
  //     if (it != st.end()) {
  //       long long cur = *it;
  //       dpl[i] = min(dpl[i], abs(cur - (sum - cur)));
  //     }
  //     if (it != st.begin()) {
  //       it--;
  //       long long cur = *it;
  //       dpl[i] = min(dpl[i], abs(cur - (sum - cur)));
  //     }
  //   }
  //   st.insert(sum);
  // }

  // st.clear();
  // sum = 0;
  // for (int i = n - 1; i >= 0; i--) {
  //   sum += a[i];
  //   dpr[i] = INF;
  //   if (sz(st)) {
  //     auto it = st.lower_bound(sum / 2);
  //     if (it != st.end()) {
  //       long long cur = *it;
  //       dpr[i] = min(dpr[i], abs(cur - (sum - cur)));
  //     }
  //     if (it != st.begin()) {
  //       it--;
  //       long long cur = *it;
  //       dpr[i] = min(dpr[i], abs(cur - (sum - cur)));
  //     }
  //   }
  //   st.insert(sum);
  // }

  // for (int i = 0; i < n; i++) {
  //   cerr << dpl[i] << " ";
  // }
  // cerr << endl;

  /* long long ans = INF;
  for (int i = 1; i + 2 < n; i++) {
    ans = min(ans, max(dpl[i], dpr[i + 1]));
  } */

  cout << ans << endl;

  return 0;
}
