Problem:
Problem B: Erratic Sleep Habits

Peter is a person with erratic sleep habits. He goes to sleep at twelve o'lock every midnight. He gets
up just after one hour of sleep on some days; he may even sleep for twenty-three hours on other days.
His sleeping duration changes in a cycle, where he always sleeps for only one hour on the first day of the
cycle.

Unfortunately, he has some job interviews this month. No doubt he wants to be in time for them. He can
take anhydrous caffeine to reset his sleeping cycle to the beginning of the cycle anytime. Then he will
wake up after one hour of sleep when he takes caffeine. But of course he wants to avoid taking caffeine
as possible, since it may affect his health and accordingly his very important job interviews.

Your task is to write a program that reports the minimum amount of caffeine required for Peter to attend
all his interviews without being late, where the information about the cycle and the schedule of his
interviews are given. The time for move to the place of each interview should be considered negligible.

Input

The input consists of multiple datasets. Each dataset is given in the following format:

T

t1 t2 ... tT

N

D1 M1

D2 M2

...

DN MN

T is the length of the cycle (1 ≤ T ≤ 30); ti (for 1 ≤ i ≤ T ) is the amount of sleep on the i-th day of the
cycle, given in hours (1 ≤ ti ≤ 23); N is the number of interviews (1 ≤ N ≤ 100); Dj (for 1 ≤ j ≤ N)
is the day of the j-th interview (1 ≤ Dj ≤ 100); Mj (for 1 ≤ j ≤ N) is the hour when the j-th interview starts (1 ≤ Mj ≤ 23).

The numbers in the input are all integers. t1 is always 1 as stated above. The day indicated by 1 is the
first day in the cycle of Peter's sleep.

The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.

Output

For each dataset, print the minimum number of times Peter needs to take anhydrous caffeine.

Sample Input

2
1 23
3
1 1
2 1
3 1
0

Output for the Sample Input

2

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.*;

import java.lang.*;

import java.math.*;



public class Main {

	Scanner sc = new Scanner(System.in);



	class dm implements Comparable<dm> {

		int d;

		int m;



		dm(int d, int m) {

			this.d = d;

			this.m = m;

		}



		public int compareTo(dm tar) {

			if (d == tar.d)

				return m - tar.m;

			return d - tar.d;

		}

	}



	void run() {

		for (;;) {

			int t = sc.nextInt();

			int r[] = new int[t];



			if (t == 0) {

				break;

			}

			for (int i = 0; i < t; i++) {

				r[i] = sc.nextInt();

			}



			int n = sc.nextInt();

			dm[] data = new dm[n];

			for (int i = 0; i < n; i++) {

				data[i] = new dm(sc.nextInt(), sc.nextInt());

			}



			Arrays.sort(data);



			int[][] dp = new int[n + 1][t];

			for(int i = 0; i < n+1 ; i++){

			Arrays.fill(dp[i], Integer.MAX_VALUE >> 4);

			}

			dp[0][0] = 0;



			int dd[] = new int[n];

			dd[0] = data[0].d-1;

			for (int i = 1; i < n; i++) {

				dd[i] = data[i].d - data[i - 1].d;

			}



			for (int i = 0; i < n; i++) {

				for (int z = 0; z < t; z++) {

					int trr = z + dd[i];

					trr %= t;

					if (data[i].m >= r[trr]) {

						dp[i + 1][trr] = Math.min(dp[i + 1][trr], dp[i][z]);

					}

				}

				

	//			System.out.println(Arrays.toString(dp[i+1]));

				for (int z = 0; z < t; z++) {

					dp[i + 1][0] = Math.min(dp[i][z] + 1, dp[i + 1][0]);

				}

				for (int z = 0; z < dd[i] && z < t; z++) {

					if (data[i].m >= r[z]) {

						dp[i + 1][z] = Math.min(dp[i + 1][z], dp[i + 1][0]);

					}

				}

	//			System.out.println(Arrays.toString(dp[i+1]));



			}

		//	System.out.println(Arrays.toString(dp[0]));

			for(int i =1 ; i < n+1;i++){

		//		System.out.println(data[i-1].d+" "+data[i-1].m+" " +Arrays.toString(dp[i]));

			}

			int ans = 10000;

			for (int i = 0; i < t; i++) {

				ans = Math.min(ans, dp[n][i]);

			}

			System.out.println(ans);

		}



	}



	public static void main(String[] args) {

		Main m = new Main();

		m.run();

	}

}