#P13814. [CERC 2022] Mortgage

[CERC 2022] Mortgage

题目描述

Andrej is a typical modern student, dreaming to buy a house one day. Since buying real property is no piece of cake, he is planning out his life and trying to figure out exactly how and when he will be able to afford one. To buy a house, he aims to take a mortgage loan that will then need to be paid back in multiple payments over the course of several months. For each of the next nn months of his life, he will earn the income aia_i that can be spent on the mortgage (other costs have already been accounted for, hence aia_i can be negative). He is now looking at a list of various properties and mortgage loans and is trying to figure out which of them he can afford.

Suppose that he takes a mortgage that involves paying xx units of money over the course of kk months, starting in month ii, and ending in month i+k1i + k - 1. Each of these months, he needs to be able to pay xx units of money. If he has any leftover income in month ii, i.e. ai>xa_i > x, he can save the rest and use it towards some of the future payments (same for any leftover money in months i+1i + 1 to i+k1i + k - 1). However, he cannot count on saving any money prior to month ii, regardless of the income in those months. He will spend it all on his current rent and avocado toast.

You are given the list of Andrej's income for the next nn months and a list of mm different time intervals. The ii-th time interval is defined by two numbers, sis_i, and kik_i. The mortgage loan starts on the month sis_i and lasts for kik_i months, i.e. the last payment is done on the month si+ki1s_i + k_i - 1. For each of the time intervals, determine what the largest monthly payment that Andrej can afford is.

输入格式

The first line contains two integers, nn and mm, the number of months, and the number of different time intervals, respectively. The second line contains nn space-separated integers, a1,,ana_1, \ldots, a_n, Andrej's income over the next nn months. This is followed by mm lines describing different time intervals, each line containing two space-separated integers sis_i and kik_i.

输出格式

Print out mm lines, one for each time interval. Print out the largest integer amount of monthly payment that Andrej can afford to pay for the ii-th mortgage. If the number is strictly smaller than 0, print "stay with parents" (without quotation marks).

9 5
6 1 10 9 5 -2 3 1 -1
3 6
1 4
3 3
6 1
8 2
4
3
8
stay with parents
0

提示

Comment

For the first interval, a monthly payment of 44 units is the largest Andrej can afford. For a monthly payment of 55, he would run out of money for his final payment. Negative income on month 66 means that Andrej cannot afford any mortgage for interval 44, regardless of its size.

Input limits

  • 1n,m21051 \leq n, m \leq 2 \cdot 10^5
  • 109ai109-10^9 \leq a_i \leq 10^9
  • 1sin;i1 \leq s_i \leq n; \forall i
  • 1ki1 \leq k_i and si+ki1n;is_i + k_i - 1 \leq n; \forall i