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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;

import java.util.PriorityQueue;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class Main {
	
	public static void main(String[] args) throws IOException {	
		try(final Scanner sc = new Scanner(System.in)){
			final int M = sc.nextInt();
			
			long[] ds = new long[M];
			long[] as = new long[M];
			
			
			long[][] cycle_nexts = new long[10][];
			long[][] cycle_ops = new long[10][];
			long[] cycle_ops_sum = new long[10];
			
			for(int d = 0; d <= 9; d++) {
				
				int cycle = 1, current = d;
				while(true) {
					int next = ((current + d) / 10) + ((current + d) % 10);
					
					if(next == d) { break; }
					
					current = next;
					cycle++;
				}
				
				cycle_nexts[d] = new long[cycle];
				cycle_ops[d] = new long[cycle];
				
				cycle_nexts[d][0] = d;
				cycle_ops[d][0] = 0;
				for(int i = 1; i < cycle; i++) {
					final long next = ((cycle_nexts[d][i - 1] + d) / 10) + ((cycle_nexts[d][i - 1] + d) % 10);
					final boolean over = cycle_nexts[d][i - 1] + d >= 10;
					
					cycle_nexts[d][i] = next;
					cycle_ops[d][i] = over ? 2 : 1;
					cycle_ops_sum[d] += cycle_ops[d][i];
				}
				
				/*
				System.out.println(Arrays.toString(cycle_nexts[d]));
				System.out.println(Arrays.toString(cycle_ops[d]));
				*/
				//System.out.println(d + " "  + cycle);
			}
			
			for(int i = 0; i < M; i++) {
				final int d = sc.nextInt();
				final long c = sc.nextLong();
				
				final int cycles = cycle_nexts[d].length;
				final int rem = (int)((c - 1) % cycles);
				//System.out.println(rem + " " + cycle_ops_sum[d]);
				
				ds[i] = cycle_nexts[d][rem];
				as[i] = ((c - 1) / cycles) * Math.max(1, cycle_ops_sum[d]);
				for(int j = 1; j <= rem; j++) {
					as[i] += cycle_ops[d][j];
				}
			}
			
			/*
			System.out.println(Arrays.toString(ds));
			System.out.println(Arrays.toString(as));
			*/
			
			long answer = Arrays.stream(as).sum();
			{
				long current = ds[0];
				for(int i = 1; i < M; i++) {
					long next = ds[i];
					
					answer += current + next >= 10 ? 2 : 1;
					current = ((current + next) / 10) + ((current + next) % 10); 
				}
			}
			
			System.out.println(answer);
		}
	}

	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;

		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}

		private void getLine() throws IOException {
			while(!hasNext()){
				tok = new StringTokenizer(br.readLine());
			}
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
		

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArray(int n) throws IOException {
			final int[] ret = new int[n];
			for(int i = 0; i < n; i++){
				ret[i] = this.nextInt();
			}
			return ret;
		}

		public long[] nextLongArray(int n) throws IOException {
			final long[] ret = new long[n];
			for(int i = 0; i < n; i++){
				ret[i] = this.nextLong();
			}
			return ret;
		}

		public void close() throws IOException {
			br.close();
		}
	}
}