Problem:
Score: 500 points

Problem Statement

N programmers are going to participate in the preliminary stage of DDCC 20XX. Due to the size of the venue, however, at most 9 contestants can participate in the finals.

The preliminary stage consists of several rounds, which will take place as follows:

All the N contestants will participate in the first round.

When X contestants participate in some round, the number of contestants advancing to the next round will be decided as follows:

The organizer will choose two consecutive digits in the decimal notation of X, and replace them with the sum of these digits. The number resulted will be the number of contestants advancing to the next round.

For example, when X = 2378, the number of contestants advancing to the next round will be 578 (if 2 and 3 are chosen), 2108 (if 3 and 7 are chosen), or 2315 (if 7 and 8 are chosen).

When X = 100, the number of contestants advancing to the next round will be 10, no matter which two digits are chosen.

The preliminary stage ends when 9 or fewer contestants remain.

Ringo, the chief organizer, wants to hold as many rounds as possible.
Find the maximum possible number of rounds in the preliminary stage.

Since the number of contestants, N, can be enormous, it is given to you as two integer sequences d_1, \ldots, d_M and c_1, \ldots, c_M, which means the following: the decimal notation of N consists of c_1 + c_2 + \ldots + c_M digits, whose first c_1 digits are all d_1, the following c_2 digits are all d_2, \ldots, and the last c_M digits are all d_M.

Constraints

1 \leq M \leq 200000

0 \leq d_i \leq 9

d_1 \neq 0

d_i \neq d_{i+1}

c_i \geq 1

2 \leq c_1 + \ldots + c_M \leq 10^{15}

Input

Input is given from Standard Input in the following format:

M
d_1 c_1
d_2 c_2
:
d_M c_M

Output

Print the maximum possible number of rounds in the preliminary stage.

Sample Input 1

2
2 2
9 1

Sample Output 1

3

In this case, N = 229 contestants will participate in the first round. One possible progression of the preliminary stage is as follows:

229 contestants participate in Round 1, 49 contestants participate in Round 2, 13 contestants participate in Round 3, and 4 contestants advance to the finals.

Here, three rounds take place in the preliminary stage, which is the maximum possible number.

Sample Input 2

3
1 1
0 8
7 1

Sample Output 2

9

In this case, 1000000007 will participate in the first round.

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Text;

public class Program
{
    private int M;
    private int[] D;
    private long[] C;

    public void Solve()
    {
        var sc = new Scanner();
        M = sc.NextInt();
        D = new int[M];
        C = new long[M];
        for (int i = 0; i < M; i++)
        {
            D[i] = sc.NextInt();
            C[i] = sc.NextLong();
        }

        // どんな順番でやってもmod 9は変わらない

        // 仮説　どんな順番で処理しても良い

        var ds = new Stack<int>();
        var cs = new Stack<long>();

        for (int i = 0; i < M; i++)
        {
            ds.Push(D[i]);
            cs.Push(C[i]);
        }

        long ans = 0;

        var list = new Stack<int>();

        while (ds.Count > 0)
        {
            int dd = ds.Pop();
            long cc = cs.Pop();

            if (dd == 0)
            {
                ans += cc;
            }

            if (dd == 1)
            {
                int m = (int) (cc % 10);
                if (m != 0)
                {
                    list.Push(m);
                    ans += m - 1;
                }

                long d = cc / 10;
                if (d > 0)
                {
                    ds.Push(1);
                    cs.Push(d / 10);
                    ans += d * 10;
                }
            }

            if (dd == 2)
            {
                int m = (int) (cc % 5);
                long c = cc / 5;
                if (m != 0)
                {
                    list.Push(m * 2);
                    ans += m - 1;
                }

                if (c > 0)
                {
                    ds.Push(1);
                    cs.Push(c);
                    ans += 5 * c;
                }
            }

            if (dd == 3)
            {
                int m = (int) (cc % 4);
                if (m != 0)
                {
                    list.Push(m * 3);
                    ans += m - 1;
                }

                long d = cc / 4;
                if (d > 0)
                {
                    ds.Push(3);
                    cs.Push(d);
                    ans += d * 4;
                }
            }

            if (dd == 4)
            {
                int m = (int) (cc % 3);
                if (m != 0)
                {
                    list.Push(m * 4);
                    ans += m - 1;
                }

                long d = cc / 3;
                if (d > 0)
                {
                    ds.Push(3);
                    cs.Push(d);
                    ans += d * 3;
                }
            }

            if (dd == 5)
            {
                int m = (int) (cc % 2);
                if (m != 0)
                {
                    list.Push(m * 5);
                    ans += m - 1;
                }

                long d = cc / 2;
                if (d > 0)
                {
                    ds.Push(1);
                    cs.Push(d);
                    ans += d * 2;
                }
            }

            if (dd == 6)
            {
                int m = (int) (cc % 2);
                if (m != 0)
                {
                    list.Push(m * 6);
                    ans += m - 1;
                }

                long d = cc / 2;
                if (d > 0)
                {
                    ds.Push(3);
                    cs.Push(d);
                    ans += d * 2;
                }
            }

            if (dd == 7)
            {
                int m = (int) (cc % 2);
                if (m != 0)
                {
                    list.Push(m * 7);
                    ans += m - 1;
                }

                long d = cc / 2;
                if (d > 0)
                {
                    ds.Push(5);
                    cs.Push(d);
                    ans += d * 2;
                }
            }

            if (dd == 8)
            {
                int m = (int) (cc % 2);
                if (m != 0)
                {
                    list.Push(m * 8);
                    ans += m - 1;
                }

                long d = cc / 2;
                if (d > 0)
                {
                    ds.Push(7);
                    cs.Push(d);
                    ans += d * 2;
                }
            }

            if (dd == 9)
            {
                int m = (int) (cc % 2);
                if (m != 0)
                {
                    list.Push(m * 9);
                    ans += m - 1;
                }

                long d = cc / 2;
                if (d > 0)
                {
                    ds.Push(9);
                    cs.Push(d);
                    ans += d * 2;
                }
            }
        }

        while (list.Count > 1)
        {
            ans++;
            var a = list.Pop();
            var b = list.Pop();
            int sum = a + b;
            if (sum >= 10)
            {
                int d = sum / 10;
                int m = sum % 10;
                list.Push(m);
                list.Push(d);
            }
            else
            {
                list.Push(sum);
            }
        }

        Console.WriteLine(ans);
    }

    public static void Main(string[] args)
    {
        new Program().Solve();
    }
}

class Scanner
{
    public Scanner()
    {
        _pos = 0;
        _line = new string[0];
    }

    const char Separator = ' ';
    private int _pos;
    private string[] _line;

    #region スペース区切りで取得

    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = Console.ReadLine().Split(Separator);
            _pos = 0;
        }

        return _line[_pos++];
    }

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }

    #endregion

    #region 型変換

    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }

    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }

    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }

    #endregion

    #region 配列取得

    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = Console.ReadLine().Split(Separator);

        _pos = _line.Length;
        return _line;
    }

    public int[] IntArray()
    {
        return ToIntArray(Array());
    }

    public long[] LongArray()
    {
        return ToLongArray(Array());
    }

    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }

    #endregion
}