Train Tickets
시간 제한2초메모리 제한512 MB
연도 구간이 주어질 때 첫 해 1월부터 마지막 해 12월까지 모든 달을 덮는 최소 티켓 비용을 구한다.
문제
The Stockholm public transport system offers kinds of tickets for purchase. The 'th kind of ticket is valid for exactly 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 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 , what the cost is to purchase tickets starting from January in year that are valid every month to December in year . Can you write a program to determine this?
입력
The first line of input contains the integer , the number of years Katriel has pricing data for. The next lines contain the pricing data for years , in order. The pricing data for a given year is given as integers: the prices of the tickets valid for months, respectively. All prices are between and .
The next line contains the integer , the number of intervals of years Katriel wishes to compute the price for. The next lines contains each of these intervals, given as two integers representing the interval .
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 , 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 .