Train Tickets

시간 제한2초메모리 제한512 MB

요약
연도 구간이 주어질 때 첫 해 1월부터 마지막 해 12월까지 모든 달을 덮는 최소 티켓 비용을 구한다.
난이도

어려움10점 중 9점

유형
동적 계획법, 분할 정복, 행렬
정답자
아직 제출이 없습니다

문제

The Stockholm public transport system offers 1212 kinds of tickets for purchase. The ii'th kind of ticket is valid for exactly ii consecutive months. However, they also change the price of every kind of ticket every year.

Katriel has access to the pricing data for the last NN years, and is currently studying the complexity of purchasing your tickets in order to minimize costs. More precisely he wants to determine, given an interval of years a,a+1,…,ba, a + 1, \dots, b, what the cost is to purchase tickets starting from January in year aa that are valid every month to December in year bb. Can you write a program to determine this?

입력

The first line of input contains the integer 1≤N≤30,0001 \le N \le 30\\,000, the number of years Katriel has pricing data for. The next NN lines contain the pricing data for years 1,2,…,N1, 2, \dots, N, in order. The pricing data for a given year is given as 1212 integers: the prices of the tickets valid for 1,2,…,121, 2, \dots, 12 months, respectively. All prices are between 00 and 10,00010\\,000.

The next line contains the integer 1≤Q≤3,0001 \le Q \le 3\\,000, the number of intervals of years Katriel wishes to compute the price for. The next QQ lines contains each of these intervals, given as two integers 1≤a≤b≤N1 \le a \le b \le N representing the interval a,a+1,…,ba, a + 1, \dots, b.

In a given year, a ticket valid for a longer period of time is never cheaper than a ticket valid for a shorter period of time.

출력

For each query a,ba, b, output a single line containing the minimum cost to purchase tickets valid during the entire interval, given that the first ticket is bought in January in year aa.

예제1

  1. 예제 1

    입력
    3
    1 2 3 4 5 6 7 8 9 10 11 12
    2 3 4 5 6 7 8 9 10 11 12 13
    3 4 5 6 7 8 9 10 11 12 13 14
    6
    1 1
    2 2
    3 3
    1 2
    2 3
    1 3
    
    예상 출력
    12
    13
    14
    25
    27
    39