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.io.*;
import java.util.*;


public class Main {
	FastScanner in = new FastScanner(System.in);
	PrintWriter out = new PrintWriter(System.out);
	
	int MAX = 1001;
	int INF = 1000000000;
	
	public void run() {
		while (true) {
			int T = in.nextInt();
			if (T == 0) break;
			if (T == 1) {
				System.out.println(0);
				continue;
			}
			int[] t = in.nextIntArray(T);
			int N = in.nextInt();
			
			int[] time = new int[MAX+1];
			Arrays.fill(time, -1);
			for (int i = 0; i < N; i++) {
				int d = in.nextInt(), m = in.nextInt();
				time[d] = Math.max(time[d], m);
			}
			
			int[][] dp = new int[MAX+1][T];
			for (int i = 0; i <= MAX; i++) Arrays.fill(dp[i], INF);
			dp[1][1] = 0;
			
			for (int i = 1; i < MAX; i++) {
				for (int j = 0; j < T; j++) {
					int next = (j + 1) % T;
					if (time[i+1] == -1 || t[j] <= time[i+1]) {
						dp[i+1][next] = Math.min(dp[i+1][next], dp[i][j]);
					}
					dp[i+1][1] = Math.min(dp[i][j] + 1, dp[i+1][1]);					
				}
			}
			
			int res = INF;
			for (int i = 0; i < T; i++) 
				res = Math.min(res, dp[MAX][i]);
			System.out.println(res);
		}
		out.close();
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");

		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}

		System.out.println("----------------------------");
		System.out.println();
	}

	public void debug(Object... obj) {
		System.out.println(Arrays.deepToString(obj));
	}

	class FastScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FastScanner(InputStream stream) {
			this.stream = stream;
			//stream = new FileInputStream(new File("dec.in"));

		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

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

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();

			return array;
		}

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

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();

			return array;
		}

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

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();

			return array;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}