AOJ 1129

解いた.後ろから,最終的に一番上になるものだけ見るテクニックを使うと計算量が小さい.

while True:
    n,r = map(int,raw_input().split())
    if n == 0 and r == 0 :
        break
    p = [None] * r
    c = [None] * r
    for i in range(r):
        p[i],c[i] = map(int, raw_input().split())
    top_idx = 0;
    for i in range(r):
        j = r-i-1
        if top_idx < c[j]:
            top_idx += p[j] - 1
        elif top_idx < c[j] + p[j] - 1:
            top_idx -= c[j]
    print n - top_idx

受験勉強の進捗は,数学の記述で減点されるのをなんとかしたいなあという感じ